From 0c9ab9fdfa5ea1e0be71b6abb680e5c7ef97c116 Mon Sep 17 00:00:00 2001 From: CGsama Date: Sun, 16 Jul 2023 01:47:06 -0400 Subject: [PATCH] fix format --- .../backend/src/misc/process-masto-notes.ts | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/packages/backend/src/misc/process-masto-notes.ts b/packages/backend/src/misc/process-masto-notes.ts index 2fe7c7a58..f51764ae8 100644 --- a/packages/backend/src/misc/process-masto-notes.ts +++ b/packages/backend/src/misc/process-masto-notes.ts @@ -4,7 +4,7 @@ import { createTemp, createTempDir } from "./create-temp.js"; import { downloadUrl } from "./download-url.js"; import { addFile } from "@/services/drive/add-file.js"; import { Users } from "@/models/index.js"; -import * as tar from 'tar-stream'; +import * as tar from "tar-stream"; import gunzip from "gunzip-maybe"; const logger = new Logger("process-masto-notes"); @@ -33,7 +33,7 @@ export async function processMastoNotes( function processMastoFile(fn: string, dir: string, uid: string) { return new Promise(async (resolve, reject) => { const user = await Users.findOneBy({ id: uid }); - try{ + try { logger.info(`Start unzip ${fn}`); await unzipTarGz(fn, dir); logger.info(`Unzip to ${dir}`); @@ -51,51 +51,58 @@ function processMastoFile(fn: string, dir: string, uid: string) { } } resolve(outbox); - }catch(e){ + } catch (e) { logger.error(`Error on extract masto note package: ${fn}`); reject(e); } }); } -function createFileDir(fn: string){ - if(!fs.existsSync(fn)){ - fs.mkdirSync(fn, {recursive: true}); +function createFileDir(fn: string) { + if (!fs.existsSync(fn)) { + fs.mkdirSync(fn, { recursive: true }); fs.rmdirSync(fn); } } -function unzipTarGz(fn: string, dir: string){ +function unzipTarGz(fn: string, dir: string) { return new Promise(async (resolve, reject) => { const onErr = (err: any) => { logger.error(`pipe broken: ${err}`); reject(); - } - try{ - const extract = tar.extract().on('error', onErr); + }; + try { + const extract = tar.extract().on("error", onErr); dir = dir.endsWith("/") ? dir : dir + "/"; const ls: string[] = []; - extract.on('entry', function (header: any, stream: any, next: any) { - try{ + extract.on("entry", function (header: any, stream: any, next: any) { + try { ls.push(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(); - }catch(e){ + } catch (e) { logger.error(`create dir error:${e}`); reject(); } }); - extract.on('finish', function () { - resolve(ls); + extract.on("finish", function () { + resolve(ls); }); - - fs.createReadStream(fn).on('error', onErr).pipe(gunzip()).on('error', onErr).pipe(extract).on('error', onErr); - - }catch(e){ + + fs.createReadStream(fn) + .on("error", onErr) + .pipe(gunzip()) + .on("error", onErr) + .pipe(extract) + .on("error", onErr); + } catch (e) { logger.error(`unzipTarGz error: ${e}`); reject(); } }); -} \ No newline at end of file +}