From abc579a8fbfb3b3195f279f2a787c22cc6a2c0d5 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Wed, 6 Apr 2022 23:08:21 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=9E=F0=9F=8F=BDUsed=20a=20promisified?= =?UTF-8?q?=20file=20exists=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/file.utils.ts | 9 +++++++++ utils/uncompression.utils.ts | 7 ++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/utils/file.utils.ts b/utils/file.utils.ts index f25f177..6cb1f65 100644 --- a/utils/file.utils.ts +++ b/utils/file.utils.ts @@ -67,6 +67,15 @@ export const explodePath = (filePath: string): IExplodedPathResponse => { }; }; +// returns a promise which resolves true if file exists: +export const checkFileExists = (filepath) => { + return new Promise((resolve, reject) => { + fs.access(filepath, fs.constants.F_OK, error => { + resolve(!error); + }); + }); + } + export const getSizeOfDirectory = async ( directoryPath: string, extensions: string[] diff --git a/utils/uncompression.utils.ts b/utils/uncompression.utils.ts index c6a3a67..fd3ee24 100644 --- a/utils/uncompression.utils.ts +++ b/utils/uncompression.utils.ts @@ -44,7 +44,7 @@ import path, { parse } from "path"; import * as p7zip from "p7zip-threetwo"; import { IExtractedComicBookCoverFile } from "threetwo-ui-typings"; import sharp from "sharp"; -import { getFileConstituents } from "../utils/file.utils"; +import { getFileConstituents, checkFileExists } from "../utils/file.utils"; import { flatten, isEmpty, isNil, isUndefined, remove } from "lodash"; import { convertXMLToJSON } from "./xml.utils"; import { USERDATA_DIRECTORY, COMICS_DIRECTORY } from "../constants/directories"; @@ -153,7 +153,8 @@ export const extractComicInfoXMLFromRar = async ( if (err) { reject(err); } - if (existsSync(`${targetDirectory}/${coverFile}`)) { + checkFileExists(`${targetDirectory}/${coverFile}`).then((bool) => { + console.log(`${coverFile} exists: ${bool}`); // orchestrate result resolve({ filePath, @@ -167,7 +168,7 @@ export const extractComicInfoXMLFromRar = async ( ), }, }); - } + }); }); });