diff --git a/models/comic.model.ts b/models/comic.model.ts index ea4982e..ce52894 100644 --- a/models/comic.model.ts +++ b/models/comic.model.ts @@ -25,15 +25,13 @@ const RawFileDetailsSchema = mongoose.Schema({ filePath: String, fileSize: Number, extension: String, + mimeType: String, containedIn: String, pageCount: Number, cover: { filePath: String, stats: Object, }, - calibreMetadata: { - coverWriteResult: String, - }, }); const LOCGSchema = mongoose.Schema({ diff --git a/services/importqueue.service.ts b/services/importqueue.service.ts index 2fe1506..74720ec 100644 --- a/services/importqueue.service.ts +++ b/services/importqueue.service.ts @@ -80,6 +80,7 @@ export default class QueueService extends Service { filePath, fileSize, extension, + mimeType, cover, containedIn, comicInfoJSON, @@ -113,6 +114,7 @@ export default class QueueService extends Service { filePath, fileSize, extension, + mimeType, containedIn, cover, }, diff --git a/utils/uncompression.utils.ts b/utils/uncompression.utils.ts index 6af8a81..d29058c 100644 --- a/utils/uncompression.utils.ts +++ b/utils/uncompression.utils.ts @@ -63,7 +63,8 @@ interface RarFile { } const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar"; - +// errors array +const errors = []; /** * Method that extracts comicInfo.xml file from a .rar archive, if one exists. * Also extracts the first image in the listing, which is assumed to be the cover. @@ -71,7 +72,8 @@ const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar"; * @returns {any} */ export const extractComicInfoXMLFromRar = async ( - filePath: string + filePath: string, + mimeType: string, ): Promise => { try { // Create the target directory @@ -185,6 +187,7 @@ export const extractComicInfoXMLFromRar = async ( extension, containedIn: targetDirectory, fileSize: fse.statSync(filePath).size, + mimeType, cover: { filePath: path.relative( process.cwd(), @@ -205,7 +208,8 @@ export const extractComicInfoXMLFromRar = async ( }; export const extractComicInfoXMLFromZip = async ( - filePath: string + filePath: string, + mimeType: string, ): Promise => { try { // Create the target directory @@ -250,9 +254,7 @@ 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}` - ); + if (!isEmpty(comicInfoXMLFileObject)) { filesToWriteToDisk.comicInfoXML = comicInfoXMLFileObject[0].name; extractionTargets.push(filesToWriteToDisk.comicInfoXML); @@ -323,6 +325,7 @@ export const extractComicInfoXMLFromZip = async ( filePath, name: fileNameWithoutExtension, extension, + mimeType, containedIn: targetDirectory, fileSize: fse.statSync(filePath).size, cover: { @@ -347,20 +350,17 @@ export const extractComicInfoXMLFromZip = async ( export const extractFromArchive = async (filePath: string) => { console.info(`Unrar is located at: ${UNRAR_BIN_PATH}`); console.info(`p7zip is located at: ${process.env.SEVENZ_BINARY_PATH}`); - const { extension } = getFileConstituents(filePath); - console.log( - `Detected file type is ${extension}, looking for comicinfo.xml...` - ); + const mimeType = await getMimeType(filePath); console.log(`File has the following mime-type: ${mimeType}`); switch (mimeType) { case "application/x-7z-compressed; charset=binary": case "application/zip; charset=binary": - const cbzResult = await extractComicInfoXMLFromZip(filePath); + const cbzResult = await extractComicInfoXMLFromZip(filePath, mimeType); return Object.assign({}, ...cbzResult); case "application/x-rar; charset=binary": - const cbrResult = await extractComicInfoXMLFromRar(filePath); + const cbrResult = await extractComicInfoXMLFromRar(filePath, mimeType); return Object.assign({}, ...cbrResult); default: @@ -381,13 +381,20 @@ export const uncompressEntireArchive = async ( filePath: string, options: any ) => { - const { extension } = getFileConstituents(filePath); - switch (extension) { - case ".cbz": - case ".cb7": - return await uncompressZipArchive(filePath, options); - case ".cbr": - return await uncompressRarArchive(filePath, options); + const mimeType = await getMimeType(filePath); + console.log(`File has the following mime-type: ${mimeType}`); + switch (mimeType) { + case "application/x-7z-compressed; charset=binary": + case "application/zip; charset=binary": + return await uncompressZipArchive(filePath, { + ...options, + mimeType, + }); + case "application/x-rar; charset=binary": + return await uncompressRarArchive(filePath, { + ...options, + mimeType, + }); } };