🔧 Added MIMEtype method to the uncompression method

This commit is contained in:
2023-03-20 11:31:42 -04:00
parent 725f948f01
commit d54ddca335
3 changed files with 24 additions and 9 deletions

View File

@@ -62,7 +62,7 @@
"leven": "^3.1.0", "leven": "^3.1.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mkdirp": "^0.5.5", "mkdirp": "^0.5.5",
"moleculer": "^0.14.28", "moleculer": "^0.14.29",
"moleculer-bull": "github:rishighan/moleculer-bull#1.0.0", "moleculer-bull": "github:rishighan/moleculer-bull#1.0.0",
"moleculer-db": "^0.8.23", "moleculer-db": "^0.8.23",
"moleculer-db-adapter-mongo": "^0.4.17", "moleculer-db-adapter-mongo": "^0.4.17",

View File

@@ -121,6 +121,12 @@ export const constructPaths = (
walkedFolder.extension, 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) => { export const getFileConstituents = (filePath: string) => {
const extension = path.extname(filePath); const extension = path.extname(filePath);
const fileNameWithExtension = path.basename(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) => { export const getMimeType = async (filePath: string) => {
return await FileMagic.getInstance().then((magic: FileMagic) => { return await FileMagic.getInstance().then((magic: FileMagic) => {
return magic.detect( return magic.detect(

View File

@@ -88,13 +88,13 @@ export const extractComicInfoXMLFromRar = async (
const archive = new Unrar({ const archive = new Unrar({
path: path.resolve(filePath), path: path.resolve(filePath),
bin: `${UNRAR_BIN_PATH}`, // this will change depending on Docker base OS bin: `${UNRAR_BIN_PATH}`, // this will change depending on Docker base OS
arguments: ["-v"] arguments: ["-v"],
}); });
const filesInArchive: [RarFile] = await new Promise( const filesInArchive: [RarFile] = await new Promise(
(resolve, reject) => { (resolve, reject) => {
return archive.list((err, entries) => { return archive.list((err, entries) => {
if (err) { if (err) {
console.log(`DEBUG: ${JSON.stringify(err, null, 2)}` ) console.log(`DEBUG: ${JSON.stringify(err, null, 2)}`);
reject(err); reject(err);
} }
resolve(entries); resolve(entries);
@@ -152,7 +152,9 @@ export const extractComicInfoXMLFromRar = async (
const comicInfoJSON = await convertXMLToJSON( const comicInfoJSON = await convertXMLToJSON(
comicinfostring.toString() comicinfostring.toString()
); );
console.log(`comicInfo.xml successfully written: ${comicInfoJSON.comicinfo}`) console.log(
`comicInfo.xml successfully written: ${comicInfoJSON.comicinfo}`
);
resolve({ comicInfoJSON: comicInfoJSON.comicinfo }); resolve({ comicInfoJSON: comicInfoJSON.comicinfo });
} }
}); });
@@ -248,7 +250,9 @@ export const extractComicInfoXMLFromZip = async (
// Push the first file (cover) to our extraction target // Push the first file (cover) to our extraction target
extractionTargets.push(files[0].name); extractionTargets.push(files[0].name);
filesToWriteToDisk.coverFile = path.basename(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)) { if (!isEmpty(comicInfoXMLFileObject)) {
filesToWriteToDisk.comicInfoXML = comicInfoXMLFileObject[0].name; filesToWriteToDisk.comicInfoXML = comicInfoXMLFileObject[0].name;
extractionTargets.push(filesToWriteToDisk.comicInfoXML); extractionTargets.push(filesToWriteToDisk.comicInfoXML);
@@ -349,13 +353,13 @@ export const extractFromArchive = async (filePath: string) => {
); );
const mimeType = await getMimeType(filePath); const mimeType = await getMimeType(filePath);
console.log(`File has the following mime-type: ${mimeType}`); console.log(`File has the following mime-type: ${mimeType}`);
switch (extension) { switch (mimeType) {
case ".cbz": case "application/x-7z-compressed; charset=binary":
case ".cb7": case "application/zip; charset=binary":
const cbzResult = await extractComicInfoXMLFromZip(filePath); const cbzResult = await extractComicInfoXMLFromZip(filePath);
return Object.assign({}, ...cbzResult); return Object.assign({}, ...cbzResult);
case ".cbr": case "application/x-rar; charset=binary":
const cbrResult = await extractComicInfoXMLFromRar(filePath); const cbrResult = await extractComicInfoXMLFromRar(filePath);
return Object.assign({}, ...cbrResult); return Object.assign({}, ...cbrResult);