fix format

This commit is contained in:
CGsama 2023-07-16 01:47:06 -04:00
parent 809d418018
commit 0c9ab9fdfa
1 changed files with 28 additions and 21 deletions

View File

@ -4,7 +4,7 @@ import { createTemp, createTempDir } from "./create-temp.js";
import { downloadUrl } from "./download-url.js"; import { downloadUrl } from "./download-url.js";
import { addFile } from "@/services/drive/add-file.js"; import { addFile } from "@/services/drive/add-file.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import * as tar from 'tar-stream'; import * as tar from "tar-stream";
import gunzip from "gunzip-maybe"; import gunzip from "gunzip-maybe";
const logger = new Logger("process-masto-notes"); const logger = new Logger("process-masto-notes");
@ -33,7 +33,7 @@ export async function processMastoNotes(
function processMastoFile(fn: string, dir: string, uid: string) { function processMastoFile(fn: string, dir: string, uid: string) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const user = await Users.findOneBy({ id: uid }); const user = await Users.findOneBy({ id: uid });
try{ try {
logger.info(`Start unzip ${fn}`); logger.info(`Start unzip ${fn}`);
await unzipTarGz(fn, dir); await unzipTarGz(fn, dir);
logger.info(`Unzip to ${dir}`); logger.info(`Unzip to ${dir}`);
@ -51,49 +51,56 @@ function processMastoFile(fn: string, dir: string, uid: string) {
} }
} }
resolve(outbox); resolve(outbox);
}catch(e){ } catch (e) {
logger.error(`Error on extract masto note package: ${fn}`); logger.error(`Error on extract masto note package: ${fn}`);
reject(e); reject(e);
} }
}); });
} }
function createFileDir(fn: string){ function createFileDir(fn: string) {
if(!fs.existsSync(fn)){ if (!fs.existsSync(fn)) {
fs.mkdirSync(fn, {recursive: true}); fs.mkdirSync(fn, { recursive: true });
fs.rmdirSync(fn); fs.rmdirSync(fn);
} }
} }
function unzipTarGz(fn: string, dir: string){ function unzipTarGz(fn: string, dir: string) {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const onErr = (err: any) => { const onErr = (err: any) => {
logger.error(`pipe broken: ${err}`); logger.error(`pipe broken: ${err}`);
reject(); reject();
} };
try{ try {
const extract = tar.extract().on('error', onErr); const extract = tar.extract().on("error", onErr);
dir = dir.endsWith("/") ? dir : dir + "/"; dir = dir.endsWith("/") ? dir : dir + "/";
const ls: string[] = []; const ls: string[] = [];
extract.on('entry', function (header: any, stream: any, next: any) { extract.on("entry", function (header: any, stream: any, next: any) {
try{ try {
ls.push(dir + header.name); ls.push(dir + header.name);
createFileDir(dir + header.name); createFileDir(dir + header.name);
stream.on('error', onErr).pipe(fs.createWriteStream(dir + header.name)).on('error', onErr); stream
.on("error", onErr)
.pipe(fs.createWriteStream(dir + header.name))
.on("error", onErr);
next(); next();
}catch(e){ } catch (e) {
logger.error(`create dir error:${e}`); logger.error(`create dir error:${e}`);
reject(); reject();
} }
}); });
extract.on('finish', function () { extract.on("finish", function () {
resolve(ls); resolve(ls);
}); });
fs.createReadStream(fn).on('error', onErr).pipe(gunzip()).on('error', onErr).pipe(extract).on('error', onErr); fs.createReadStream(fn)
.on("error", onErr)
}catch(e){ .pipe(gunzip())
.on("error", onErr)
.pipe(extract)
.on("error", onErr);
} catch (e) {
logger.error(`unzipTarGz error: ${e}`); logger.error(`unzipTarGz error: ${e}`);
reject(); reject();
} }