diff --git a/package.json b/package.json index 09a08a3..2645518 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "leven": "^3.1.0", "lodash": "^4.17.21", "mkdirp": "^0.5.5", - "moleculer": "^0.14.28", + "moleculer": "^0.14.29", "moleculer-bull": "github:rishighan/moleculer-bull#1.0.0", "moleculer-db": "^0.8.23", "moleculer-db-adapter-mongo": "^0.4.17", diff --git a/utils/file.utils.ts b/utils/file.utils.ts index b456723..7196667 100644 --- a/utils/file.utils.ts +++ b/utils/file.utils.ts @@ -121,6 +121,12 @@ export const constructPaths = ( walkedFolder.extension, }); +/** + * Method that gets file metadata from a filepath. + * Extracts the file extension, file name with and without the extension + * @param {string} filePath + * @returns {Object} object + */ export const getFileConstituents = (filePath: string) => { const extension = path.extname(filePath); const fileNameWithExtension = path.basename(filePath); @@ -135,6 +141,11 @@ export const getFileConstituents = (filePath: string) => { }; }; +/** + * Method that infers MIME type from a filepath + * @param {string} filePath + * @returns {Promise} string + */ export const getMimeType = async (filePath: string) => { return await FileMagic.getInstance().then((magic: FileMagic) => { return magic.detect( diff --git a/utils/uncompression.utils.ts b/utils/uncompression.utils.ts index 127acf9..6af8a81 100644 --- a/utils/uncompression.utils.ts +++ b/utils/uncompression.utils.ts @@ -88,13 +88,13 @@ export const extractComicInfoXMLFromRar = async ( const archive = new Unrar({ path: path.resolve(filePath), bin: `${UNRAR_BIN_PATH}`, // this will change depending on Docker base OS - arguments: ["-v"] + arguments: ["-v"], }); const filesInArchive: [RarFile] = await new Promise( (resolve, reject) => { return archive.list((err, entries) => { if (err) { - console.log(`DEBUG: ${JSON.stringify(err, null, 2)}` ) + console.log(`DEBUG: ${JSON.stringify(err, null, 2)}`); reject(err); } resolve(entries); @@ -152,7 +152,9 @@ export const extractComicInfoXMLFromRar = async ( const comicInfoJSON = await convertXMLToJSON( comicinfostring.toString() ); - console.log(`comicInfo.xml successfully written: ${comicInfoJSON.comicinfo}`) + console.log( + `comicInfo.xml successfully written: ${comicInfoJSON.comicinfo}` + ); resolve({ comicInfoJSON: comicInfoJSON.comicinfo }); } }); @@ -248,7 +250,9 @@ export const extractComicInfoXMLFromZip = async ( // Push the first file (cover) to our extraction target extractionTargets.push(files[0].name); filesToWriteToDisk.coverFile = path.basename(files[0].name); - console.log(`sanitized or not, here I am: ${filesToWriteToDisk.coverFile}`); + console.log( + `sanitized or not, here I am: ${filesToWriteToDisk.coverFile}` + ); if (!isEmpty(comicInfoXMLFileObject)) { filesToWriteToDisk.comicInfoXML = comicInfoXMLFileObject[0].name; extractionTargets.push(filesToWriteToDisk.comicInfoXML); @@ -349,13 +353,13 @@ export const extractFromArchive = async (filePath: string) => { ); const mimeType = await getMimeType(filePath); console.log(`File has the following mime-type: ${mimeType}`); - switch (extension) { - case ".cbz": - case ".cb7": + switch (mimeType) { + case "application/x-7z-compressed; charset=binary": + case "application/zip; charset=binary": const cbzResult = await extractComicInfoXMLFromZip(filePath); return Object.assign({}, ...cbzResult); - case ".cbr": + case "application/x-rar; charset=binary": const cbrResult = await extractComicInfoXMLFromRar(filePath); return Object.assign({}, ...cbrResult);