🔧 Fix for a gnarly extension check in both unrar and unzip methods

This commit is contained in:
2022-05-31 22:35:53 -07:00
parent c4b421bc28
commit 6109dc8802

View File

@@ -32,7 +32,7 @@ SOFTWARE.
*/
import { createReadStream, createWriteStream, existsSync, statSync } from "fs";
import { isEmpty, isNil, isUndefined, remove, each, map } from "lodash";
import { isEmpty, isNil, isUndefined, remove, each, map, reject } from "lodash";
import * as p7zip from "p7zip-threetwo";
import path from "path";
import sharp from "sharp";
@@ -65,6 +65,7 @@ const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar";
export const extractComicInfoXMLFromRar = async (
filePath: string
): Promise<any> => {
try {
const result = {
filePath,
};
@@ -82,20 +83,27 @@ export const extractComicInfoXMLFromRar = async (
path: path.resolve(filePath),
bin: `${UNRAR_BIN_PATH}`, // this will change depending on Docker base OS
});
const filesInArchive: [RarFile] = await new Promise((resolve, reject) => {
const filesInArchive: [RarFile] = await new Promise(
(resolve, reject) => {
return archive.list((err, entries) => {
if(err) {
reject(err);
}
resolve(entries);
});
});
}
);
remove(filesInArchive, ({ type }) => type === "Directory");
const comicInfoXML = remove(
filesInArchive,
({ name }) => path.basename(name).toLowerCase() === "comicinfo.xml"
);
remove(
filesInArchive,
({ name }) => !IMPORT_IMAGE_FILE_FORMATS.includes(path.extname(name))
({ name }) => !IMPORT_IMAGE_FILE_FORMATS.includes(path.extname(name).toLowerCase())
);
const files = filesInArchive.sort((a, b) => {
if (!isUndefined(a) && !isUndefined(b)) {
@@ -109,7 +117,9 @@ export const extractComicInfoXMLFromRar = async (
let comicinfostring = "";
if (!isUndefined(comicInfoXML[0])) {
console.log(path.basename(comicInfoXML[0].name));
const comicInfoXMLFileName = path.basename(comicInfoXML[0].name);
const comicInfoXMLFileName = path.basename(
comicInfoXML[0].name
);
const writeStream = createWriteStream(
`${targetDirectory}/${comicInfoXMLFileName}`
);
@@ -125,7 +135,9 @@ export const extractComicInfoXMLFromRar = async (
readStream.on("error", (error) => reject(error));
readStream.on("end", async () => {
if (
existsSync(`${targetDirectory}/${comicInfoXMLFileName}`)
existsSync(
`${targetDirectory}/${comicInfoXMLFileName}`
)
) {
const comicInfoJSON = await convertXMLToJSON(
comicinfostring.toString()
@@ -146,11 +158,14 @@ export const extractComicInfoXMLFromRar = async (
const coverExtractionStream = archive.stream(files[0].name);
const resizeStream = coverExtractionStream.pipe(sharpStream);
resizeStream.toFile(`${targetDirectory}/${coverFile}`, (err, info) => {
resizeStream.toFile(
`${targetDirectory}/${coverFile}`,
(err, info) => {
if (err) {
reject(err);
}
checkFileExists(`${targetDirectory}/${coverFile}`).then((bool) => {
checkFileExists(`${targetDirectory}/${coverFile}`).then(
(bool) => {
console.log(`${coverFile} exists: ${bool}`);
// orchestrate result
resolve({
@@ -166,16 +181,22 @@ export const extractComicInfoXMLFromRar = async (
),
},
});
});
});
}
);
}
);
});
return Promise.all([comicInfoXMLFilePromise, coverFilePromise]);
} catch (err) {
reject(err);
}
};
export const extractComicInfoXMLFromZip = async (
filePath: string
): Promise<any> => {
try {
// Create the target directory
const directoryOptions = {
mode: 0o2775,
@@ -194,7 +215,8 @@ export const extractComicInfoXMLFromZip = async (
// only allow allowed image formats
remove(
filesFromArchive.files,
({ name }) => !IMPORT_IMAGE_FILE_FORMATS.includes(path.extname(name))
({ name }) =>
!IMPORT_IMAGE_FILE_FORMATS.includes(path.extname(name).toLowerCase())
);
// detect ComicInfo.xml
@@ -246,7 +268,10 @@ export const extractComicInfoXMLFromZip = async (
filesToWriteToDisk.comicInfoXML
)}`
);
comicInfoXMLStream.on("data", (data) => (comicinfoString += data));
comicInfoXMLStream.on(
"data",
(data) => (comicinfoString += data)
);
comicInfoXMLStream.on("end", async () => {
const comicInfoJSON = await convertXMLToJSON(
comicinfoString.toString()
@@ -265,7 +290,9 @@ export const extractComicInfoXMLFromZip = async (
const coverFilePromise = new Promise((resolve, reject) => {
const sharpStream = sharp().resize(275).toFormat("png");
const coverStream = createReadStream(
`${targetDirectory}/${path.basename(filesToWriteToDisk.coverFile)}`
`${targetDirectory}/${path.basename(
filesToWriteToDisk.coverFile
)}`
);
coverStream
.pipe(sharpStream)
@@ -298,6 +325,9 @@ export const extractComicInfoXMLFromZip = async (
});
return Promise.all([comicInfoXMLPromise, coverFilePromise]);
} catch (err) {
reject(err);
}
};
export const extractFromArchive = async (filePath: string) => {