✅ Added fastest-validator
This commit is contained in:
@@ -44,6 +44,7 @@ import {
|
|||||||
IFolderData,
|
IFolderData,
|
||||||
} from "../interfaces/folder.interface";
|
} from "../interfaces/folder.interface";
|
||||||
import { logger } from "./logger.utils";
|
import { logger } from "./logger.utils";
|
||||||
|
import { validateComicBookMetadata } from "../utils/validation.utils";
|
||||||
const { writeFile, readFile } = require("fs").promises;
|
const { writeFile, readFile } = require("fs").promises;
|
||||||
const sharp = require("sharp");
|
const sharp = require("sharp");
|
||||||
const unrarer = require("node-unrar-js");
|
const unrarer = require("node-unrar-js");
|
||||||
@@ -103,11 +104,14 @@ export const unrar = async (
|
|||||||
paths.targetPath + "/" + fileName,
|
paths.targetPath + "/" + fileName,
|
||||||
fileArrayBuffer
|
fileArrayBuffer
|
||||||
);
|
);
|
||||||
resolve({
|
let comicBookMetadata = {
|
||||||
name: `${fileName}`,
|
name: `${fileName}`,
|
||||||
path: paths.targetPath,
|
path: paths.targetPath,
|
||||||
fileSize: fileHeader.packSize,
|
fileSize: fileHeader.packSize,
|
||||||
});
|
};
|
||||||
|
if (validateComicBookMetadata(comicBookMetadata)) {
|
||||||
|
resolve(comicBookMetadata);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -178,6 +182,10 @@ export const unzip = async (
|
|||||||
mode: 0o2775,
|
mode: 0o2775,
|
||||||
};
|
};
|
||||||
const paths = constructPaths(extractionOptions, walkedFolder);
|
const paths = constructPaths(extractionOptions, walkedFolder);
|
||||||
|
const extractedFiles: IExtractedComicBookCoverFile[] = [];
|
||||||
|
const isCover =
|
||||||
|
extractedFiles.length === 1 &&
|
||||||
|
extractionOptions.extractTarget === "cover";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fse.ensureDir(paths.targetPath, directoryOptions);
|
await fse.ensureDir(paths.targetPath, directoryOptions);
|
||||||
@@ -186,17 +194,13 @@ export const unzip = async (
|
|||||||
logger.error(`${error} Couldn't create directory.`);
|
logger.error(`${error} Couldn't create directory.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const extractedFiles: IExtractedComicBookCoverFile[] = [];
|
|
||||||
const zip = createReadStream(paths.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) {
|
||||||
const fileName = explodePath(entry.path).fileName;
|
const fileName = explodePath(entry.path).fileName;
|
||||||
const size = entry.vars.uncompressedSize;
|
const size = entry.vars.uncompressedSize;
|
||||||
if (
|
if (isCover) {
|
||||||
extractedFiles.length === 1 &&
|
|
||||||
extractionOptions.extractTarget === "cover"
|
|
||||||
) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (fileName !== "" && entry.type !== "Directory") {
|
if (fileName !== "" && entry.type !== "Directory") {
|
||||||
@@ -213,7 +217,11 @@ export const unzip = async (
|
|||||||
|
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
logger.info("");
|
logger.info("");
|
||||||
resolve(extractedFiles[0]);
|
if (isCover) {
|
||||||
|
resolve(extractedFiles[0]);
|
||||||
|
} else {
|
||||||
|
resolve(extractedFiles);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
22
utils/validation.utils.ts
Normal file
22
utils/validation.utils.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import {
|
||||||
|
IExplodedPathResponse,
|
||||||
|
IExtractComicBookCoverErrorResponse,
|
||||||
|
IExtractedComicBookCoverFile,
|
||||||
|
IExtractionOptions,
|
||||||
|
IFolderData,
|
||||||
|
} from "../interfaces/folder.interface";
|
||||||
|
const Validator = require("fastest-validator");
|
||||||
|
|
||||||
|
export const validateComicBookMetadata = (
|
||||||
|
comicBookMetadataObject: IExtractedComicBookCoverFile
|
||||||
|
): boolean => {
|
||||||
|
console.log(comicBookMetadataObject);
|
||||||
|
const validator = new Validator();
|
||||||
|
const sch = {
|
||||||
|
name: { type: "string" },
|
||||||
|
fileSize: { type: "number", positive: true, integer: true },
|
||||||
|
path: { type: "string" },
|
||||||
|
};
|
||||||
|
const check = validator.compile(sch);
|
||||||
|
return check(comicBookMetadataObject);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user