🔧 Orchestrating return values from uncompression methods

This commit is contained in:
2022-03-13 22:15:23 -07:00
parent 454e2882e0
commit 9969eb3ea2
2 changed files with 40 additions and 22 deletions

View File

@@ -80,6 +80,7 @@ export default class QueueService extends Service {
} = getFileConstituents(job.data.fileObject.filePath); } = getFileConstituents(job.data.fileObject.filePath);
const targetDirectory = `${USERDATA_DIRECTORY}/covers/${fileNameWithoutExtension}`; const targetDirectory = `${USERDATA_DIRECTORY}/covers/${fileNameWithoutExtension}`;
const foo = await extractFromArchive(job.data.fileObject.filePath, targetDirectory, extension ); const foo = await extractFromArchive(job.data.fileObject.filePath, targetDirectory, extension );
console.log("JAADASD!@#!@#@!", foo);
// infer any issue-related metadata from the filename // infer any issue-related metadata from the filename
// const { inferredIssueDetails } = refineQuery(result.name); // const { inferredIssueDetails } = refineQuery(result.name);

View File

@@ -137,12 +137,15 @@ export const extractComicInfoXMLFromRar = async (
filePath: string, filePath: string,
fileToExtract: string fileToExtract: string
) => { ) => {
try { const result = {
filePath,
};
// Create the target directory // Create the target directory
const directoryOptions = { const directoryOptions = {
mode: 0o2775, mode: 0o2775,
}; };
const { fileNameWithoutExtension } = getFileConstituents(filePath); const { fileNameWithoutExtension, extension, fileNameWithExtension } =
getFileConstituents(filePath);
const targetDirectory = `${USERDATA_DIRECTORY}/covers/${fileNameWithoutExtension}`; const targetDirectory = `${USERDATA_DIRECTORY}/covers/${fileNameWithoutExtension}`;
await fse.ensureDir(targetDirectory, directoryOptions); await fse.ensureDir(targetDirectory, directoryOptions);
console.info(`%s was created.`, targetDirectory); console.info(`%s was created.`, targetDirectory);
@@ -151,6 +154,7 @@ export const extractComicInfoXMLFromRar = async (
path: path.resolve(filePath), path: path.resolve(filePath),
bin: `/usr/local/bin/unrar`, // this will change depending on Docker base OS bin: `/usr/local/bin/unrar`, // this will change depending on Docker base OS
}); });
return new Promise((resolve, reject) => {
archive.list(async (err, entries) => { archive.list(async (err, entries) => {
remove(entries, ({ type }) => type === "Directory"); remove(entries, ({ type }) => type === "Directory");
const comicInfoXML = remove( const comicInfoXML = remove(
@@ -175,13 +179,25 @@ export const extractComicInfoXMLFromRar = async (
if (err) { if (err) {
console.log("Failed to resize image:"); console.log("Failed to resize image:");
console.log(err); console.log(err);
return err; reject(err);
} }
console.log( console.log(
"Image file resized with the following parameters: %o", "Image file resized with the following parameters: %o",
info info
); );
return info; // orchestrate result
Object.assign(result, {
name: fileNameWithoutExtension,
extension,
containedIn: path.resolve(fileNameWithExtension),
cover: {
filePath: path.relative(
process.cwd(),
`${targetDirectory}/${files[0].name}`
),
},
});
resolve(result);
}); });
// ComicInfo.xml extraction and parsing to JSON // ComicInfo.xml extraction and parsing to JSON
if (!isUndefined(comicInfoXML[0])) { if (!isUndefined(comicInfoXML[0])) {
@@ -200,12 +216,11 @@ export const extractComicInfoXMLFromRar = async (
comicInfoString comicInfoString
); );
console.log(comicInfoJSON); console.log(comicInfoJSON);
Object.assign(result, { comicInfo: comicInfoJSON });
}); });
} }
}); });
} catch (error) { });
throw new Error(error);
}
}; };
export const extractComicInfoXMLFromZip = async ( export const extractComicInfoXMLFromZip = async (
@@ -277,13 +292,15 @@ export const extractComicInfoXMLFromZip = async (
); );
comicInfoExtractionStream.on("end", async (data) => { comicInfoExtractionStream.on("end", async (data) => {
console.log(`${comicInfoXML[0].file} was extracted.`); console.log(`${comicInfoXML[0].file} was extracted.`);
const xml = await fs.readFile(`${targetDirectory}/${comicInfoXML[0].file}`); const xml = await fs.readFile(
const comicInfoJSON = await convertXMLToJSON(xml.toString()); `${targetDirectory}/${comicInfoXML[0].file}`
);
const comicInfoJSON = await convertXMLToJSON(
xml.toString()
);
console.log(comicInfoJSON); console.log(comicInfoJSON);
}); });
} }
}); });
} catch (error) { } catch (error) {
throw new Error(error); throw new Error(error);