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