From f9bad43aabac0a24ef17254bb8b90410bbbd0d2d Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Tue, 4 May 2021 15:48:14 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=F0=9F=8F=BB=E2=80=8D=F0=9F=94=A7Ha?= =?UTF-8?q?rdening=20unrar=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/workers/extractCovers.worker.ts | 7 +- .../route/routes/importComics.routes.ts | 10 ++- src/server/utils/fs.utils.ts | 68 +++++-------------- 3 files changed, 26 insertions(+), 59 deletions(-) diff --git a/src/client/workers/extractCovers.worker.ts b/src/client/workers/extractCovers.worker.ts index 77f4271..faa745a 100644 --- a/src/client/workers/extractCovers.worker.ts +++ b/src/client/workers/extractCovers.worker.ts @@ -21,13 +21,16 @@ export const greet = async ( }; const fileObjects = await walkFolder("./comics"); - _.map(fileObjects, async (fileObject) => { + const extractedDataPromises = _.map(fileObjects, async (fileObject) => { if (SUPPORTED_COMIC_ARCHIVES.includes(fileObject.extension)) { - await extractCoverFromComicBookArchive({ + return await extractCoverFromComicBookArchive({ ...targetOptions, paginationOptions: pagingConfig, folderDetails: fileObject, }); } }); + Promise.all(extractedDataPromises).then((data) => { + console.log(data.data); + }); }; diff --git a/src/server/route/routes/importComics.routes.ts b/src/server/route/routes/importComics.routes.ts index ef80b31..b760300 100644 --- a/src/server/route/routes/importComics.routes.ts +++ b/src/server/route/routes/importComics.routes.ts @@ -17,7 +17,10 @@ router.route("/getComicCovers").post(async (req: Request, res: Response) => { ? req.body.extractionOptions : {}; const extractedData = await extractArchive(req.body); - if (_.isArray(extractedData)) { + if ( + _.isArray(extractedData) && + !_.isUndefined(req.body.paginationOptions.pageLimit) + ) { const pageCount = Math.ceil( extractedData.length / req.body.paginationOptions.pageLimit, ); @@ -27,11 +30,6 @@ router.route("/getComicCovers").post(async (req: Request, res: Response) => { has_more: paginate.hasNextPages(req)(pageCount), pageCount, itemCount: extractedData.length, - pages: paginate.getArrayPages(req)( - 3, - pageCount, - req.body.paginationOptions.page, - ), extractedData, }); } diff --git a/src/server/utils/fs.utils.ts b/src/server/utils/fs.utils.ts index c402378..862bd4c 100644 --- a/src/server/utils/fs.utils.ts +++ b/src/server/utils/fs.utils.ts @@ -1,39 +1,6 @@ -/* - * MIT License - * - * Copyright (c) 2021 Rishi Ghan - * - MIT License - -Copyright (c) Facebook, Inc. and its affiliates. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - - */ - -/* - * Revision History: - * Initial: 2021/05/04 Rishi Ghan - */ - const sharp = require("sharp"); const unrarer = require("node-unrar-js"); +import { UnrarError } from "node-unrar-js"; const Walk = require("@root/walk"); const fse = require("fs-extra"); @@ -111,16 +78,16 @@ export const unrar = async ( }); case "all": - const files = extractor.extract({}); - const extractedFiles = [...files.files]; - return new Promise(async (resolve, reject) => { - const comicBookCoverFiles: IExtractedComicBookCoverFile[] = []; - for (const file of extractedFiles) { - logger.info(`Attempting to write ${file.fileHeader.name}`); - const fileBuffer = file.extraction; - const fileName = explodePath(file.fileHeader.name).fileName; - try { + try { + const files = extractor.extract({}); + const extractedFiles = [...files.files]; + const comicBookCoverFiles: IExtractedComicBookCoverFile[] = []; + for (const file of extractedFiles) { + logger.info(`Attempting to write ${file.fileHeader.name}`); + const fileBuffer = file.extraction; + const fileName = explodePath(file.fileHeader.name).fileName; + if (fileName !== "" && file.fileHeader.flags.directory === false) { await writeFile(paths.targetPath + "/" + fileName, fileBuffer); } @@ -129,16 +96,15 @@ export const unrar = async ( path: paths.targetPath, fileSize: file.fileHeader.packSize, }); - } catch (error) { - logger.error(error); - reject({ - message: `${error}`, - errorCode: "500", - data: error, - }); } + resolve(comicBookCoverFiles); + } catch (error) { + resolve({ + message: `${error}`, + errorCode: "500", + data: extractionOptions.folderDetails.name, + }); } - resolve(comicBookCoverFiles); }); default: