🔧 Refactoring the unrar method to use streams to sharp for image transformation

This commit is contained in:
2022-03-11 18:18:37 -08:00
parent cceb0da33f
commit 31f307a753

View File

@@ -42,7 +42,7 @@ import {
IFolderData, IFolderData,
ISharpResizedImageStats, ISharpResizedImageStats,
} from "threetwo-ui-typings"; } from "threetwo-ui-typings";
import sharp from "sharp";
import { import {
explodePath, explodePath,
getFileConstituents, getFileConstituents,
@@ -153,30 +153,50 @@ export const extractComicInfoXMLFromRar = async (
// arguments: ['-pPassword'], // arguments: ['-pPassword'],
// bin: pathToUnrarBin // Default: unrar // bin: pathToUnrarBin // Default: unrar
// }); // });
const loo = [];
archive.list(function (err, entries) { archive.list(function (err, entries) {
remove(entries, ({ type }) => type === "Directory"); remove(entries, ({ type }) => type === "Directory");
const comicInfoXML = remove(entries, ({ name }) => name.toLowerCase() === "comicinfo.xml"); const comicInfoXML = remove(
const foo = entries.sort((a, b) => { entries,
({ name }) => name.toLowerCase() === "comicinfo.xml"
);
const files = entries.sort((a, b) => {
if (!isUndefined(a) && !isUndefined(b)) { if (!isUndefined(a) && !isUndefined(b)) {
return a.name return a.name
.toLowerCase() .toLowerCase()
.localeCompare(b.name.toLowerCase()); .localeCompare(b.name.toLowerCase());
} }
}); });
loo.push(foo) const sharpStream = sharp().resize(200, 200);
console.log(loo[0][0]); const coverImageStream = archive
var stream = archive.stream(loo[0][0].name); // name of entry .stream(files[0].name)
stream.on('error', console.error); .on("error", console.error)
stream.pipe(require('fs').createWriteStream(`${targetDirectory}/0.jpg`)); // .pipe(
if(!isUndefined(comicInfoXML[0]) ) { // require("fs").createWriteStream(`${targetDirectory}/0.jpg`)
// );
.pipe(sharpStream)
.toFile(`${targetDirectory}/0.jpg`, (err, info) => {
if (err) {
console.log("Failed to resize image:");
console.log(err);
return err;
}
console.log(
"Image file resized with the following parameters: %o",
info
);
return info;
});
if (!isUndefined(comicInfoXML[0])) {
console.log(comicInfoXML); console.log(comicInfoXML);
const comicinfoStream = archive.stream(comicInfoXML[0]["name"]); const comicinfoStream = archive.stream(comicInfoXML[0]["name"]);
comicinfoStream.on('error', console.error); comicinfoStream.on("error", console.error);
comicinfoStream.pipe(require('fs').createWriteStream(`${targetDirectory}/dhanda.xml`)); comicinfoStream.pipe(
require("fs").createWriteStream(
`${targetDirectory}/dhanda.xml`
)
);
} }
}); });
// const extracted = extractor.extract({ // const extracted = extractor.extract({