🪛 Refactored methods
This commit is contained in:
@@ -11,8 +11,8 @@ import { Request, Response } from "express";
|
|||||||
|
|
||||||
router.route("/getComicCovers").post(async (req: Request, res: Response) => {
|
router.route("/getComicCovers").post(async (req: Request, res: Response) => {
|
||||||
const options = {
|
const options = {
|
||||||
name: "Fatale 004 (2012) (Digital) (Zone-Empire)",
|
name: "30 Days of Night # 30 äíåé íî÷è # 01-001",
|
||||||
extension: ".cbr",
|
extension: ".cbz",
|
||||||
containedIn: "comics",
|
containedIn: "comics",
|
||||||
isFile: true,
|
isFile: true,
|
||||||
isLink: false,
|
isLink: false,
|
||||||
|
|||||||
@@ -24,29 +24,17 @@ export const unrar = async (
|
|||||||
| IExtractedComicBookCoverFile[]
|
| IExtractedComicBookCoverFile[]
|
||||||
| IExtractComicBookCoverErrorResponse
|
| IExtractComicBookCoverErrorResponse
|
||||||
> => {
|
> => {
|
||||||
const targetPath =
|
const paths = constructPaths(extractionOptions);
|
||||||
extractionOptions.sourceFolder +
|
|
||||||
"/" +
|
|
||||||
extractionOptions.targetExtractionFolder +
|
|
||||||
"/" +
|
|
||||||
extractionOptions.folderDetails.name;
|
|
||||||
|
|
||||||
const inputFilePath =
|
|
||||||
extractionOptions.folderDetails.containedIn +
|
|
||||||
"/" +
|
|
||||||
extractionOptions.folderDetails.name +
|
|
||||||
extractionOptions.folderDetails.extension;
|
|
||||||
|
|
||||||
const directoryOptions = {
|
const directoryOptions = {
|
||||||
mode: 0o2775,
|
mode: 0o2775,
|
||||||
};
|
};
|
||||||
const fileBuffer = await readFile(inputFilePath).catch((err) =>
|
const fileBuffer = await readFile(paths.inputFilePath).catch((err) =>
|
||||||
console.error("Failed to read file", err),
|
console.error("Failed to read file", err),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fse.ensureDir(targetPath, directoryOptions);
|
await fse.ensureDir(paths.targetPath, directoryOptions);
|
||||||
logger.info(`${targetPath} was created or already exists.`);
|
logger.info(`${paths.targetPath} was created or already exists.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`${error}: Couldn't create directory.`);
|
logger.error(`${error}: Couldn't create directory.`);
|
||||||
}
|
}
|
||||||
@@ -69,11 +57,11 @@ export const unrar = async (
|
|||||||
fileName !== "" &&
|
fileName !== "" &&
|
||||||
extractedFile.fileHeader.flags.directory === false
|
extractedFile.fileHeader.flags.directory === false
|
||||||
) {
|
) {
|
||||||
await writeFile(targetPath + "/" + fileName, fileArrayBuffer);
|
await writeFile(paths.targetPath + "/" + fileName, fileArrayBuffer);
|
||||||
}
|
}
|
||||||
resolve({
|
resolve({
|
||||||
name: `${extractedFile.fileHeader.name}`,
|
name: `${extractedFile.fileHeader.name}`,
|
||||||
path: targetPath,
|
path: paths.targetPath,
|
||||||
fileSize: extractedFile.fileHeader.packSize,
|
fileSize: extractedFile.fileHeader.packSize,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -93,11 +81,11 @@ export const unrar = async (
|
|||||||
const fileName = explodePath(file.fileHeader.name).fileName;
|
const fileName = explodePath(file.fileHeader.name).fileName;
|
||||||
try {
|
try {
|
||||||
if (fileName !== "" && file.fileHeader.flags.directory === false) {
|
if (fileName !== "" && file.fileHeader.flags.directory === false) {
|
||||||
await writeFile(targetPath + "/" + fileName, fileBuffer);
|
await writeFile(paths.targetPath + "/" + fileName, fileBuffer);
|
||||||
}
|
}
|
||||||
comicBookCoverFiles.push({
|
comicBookCoverFiles.push({
|
||||||
name: `${file.fileHeader.name}`,
|
name: `${file.fileHeader.name}`,
|
||||||
path: targetPath,
|
path: paths.targetPath,
|
||||||
fileSize: file.fileHeader.packSize,
|
fileSize: file.fileHeader.packSize,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -138,28 +126,17 @@ export const unzip = async (
|
|||||||
const directoryOptions = {
|
const directoryOptions = {
|
||||||
mode: 0o2775,
|
mode: 0o2775,
|
||||||
};
|
};
|
||||||
const targetPath =
|
const paths = constructPaths(extractionOptions);
|
||||||
extractionOptions.sourceFolder +
|
|
||||||
"/" +
|
|
||||||
extractionOptions.targetExtractionFolder +
|
|
||||||
"/" +
|
|
||||||
extractionOptions.folderDetails.name;
|
|
||||||
|
|
||||||
const inputFilePath =
|
|
||||||
extractionOptions.folderDetails.containedIn +
|
|
||||||
"/" +
|
|
||||||
extractionOptions.folderDetails.name +
|
|
||||||
extractionOptions.folderDetails.extension;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fse.ensureDir(targetPath, directoryOptions);
|
await fse.ensureDir(paths.targetPath, directoryOptions);
|
||||||
logger.info(`${targetPath} was created or already exists.`);
|
logger.info(`${paths.targetPath} was created or already exists.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`${error} Couldn't create directory.`);
|
logger.error(`${error} Couldn't create directory.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const extractedFiles: IExtractedComicBookCoverFile[] = [];
|
const extractedFiles: IExtractedComicBookCoverFile[] = [];
|
||||||
const zip = createReadStream(inputFilePath).pipe(
|
const zip = createReadStream(paths.inputFilePath).pipe(
|
||||||
unzipper.Parse({ forceStream: true }),
|
unzipper.Parse({ forceStream: true }),
|
||||||
);
|
);
|
||||||
for await (const entry of zip) {
|
for await (const entry of zip) {
|
||||||
@@ -172,11 +149,11 @@ export const unzip = async (
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (fileName !== "" && entry.type !== "Directory") {
|
if (fileName !== "" && entry.type !== "Directory") {
|
||||||
entry.pipe(createWriteStream(targetPath + "/" + fileName));
|
entry.pipe(createWriteStream(paths.targetPath + "/" + fileName));
|
||||||
extractedFiles.push({
|
extractedFiles.push({
|
||||||
name: fileName,
|
name: fileName,
|
||||||
fileSize: size,
|
fileSize: size,
|
||||||
path: targetPath,
|
path: paths.targetPath,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
entry.autodrain();
|
entry.autodrain();
|
||||||
@@ -250,3 +227,19 @@ export const explodePath = (filePath: string): IExplodedPathResponse => {
|
|||||||
fileName,
|
fileName,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const constructPaths = (extractionOptions: IExtractionOptions) => {
|
||||||
|
return {
|
||||||
|
targetPath:
|
||||||
|
extractionOptions.sourceFolder +
|
||||||
|
"/" +
|
||||||
|
extractionOptions.targetExtractionFolder +
|
||||||
|
"/" +
|
||||||
|
extractionOptions.folderDetails.name,
|
||||||
|
inputFilePath:
|
||||||
|
extractionOptions.folderDetails.containedIn +
|
||||||
|
"/" +
|
||||||
|
extractionOptions.folderDetails.name +
|
||||||
|
extractionOptions.folderDetails.extension,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user