🏗 Refactoring the import process WIP

This commit is contained in:
2021-11-30 22:44:06 -08:00
parent e8d21d4292
commit 1fd5f5b6cf
12 changed files with 380 additions and 1046 deletions

View File

@@ -9,7 +9,6 @@ import {
IExtractionOptions,
IFolderData,
} from "threetwo-ui-typings";
import { logger } from "./logger.utils";
import { includes, remove, indexOf } from "lodash";
const ALLOWED_IMAGE_FILE_FORMATS = [".jpg", ".jpeg", ".png"];
@@ -29,7 +28,7 @@ export const walkFolder = async (folder: string, formats: string[]): Promise<IFo
const walk = Walk.create({ sort: filterOutDotFiles });
await walk(folder, async (err, pathname, dirent) => {
if (err) {
logger.error("Failed to lstat directory", { error: err });
console.log("Failed to lstat directory", { error: err });
return false;
}
if ([...formats].includes(path.extname(dirent.name))) {
@@ -42,7 +41,7 @@ export const walkFolder = async (folder: string, formats: string[]): Promise<IFo
isFile: dirent.isFile(),
isLink: dirent.isSymbolicLink(),
};
logger.info(
console.log(
`Scanned ${dirent.name} contained in ${path.dirname(pathname)}`
);
result.push(walkResult);

View File

@@ -1,5 +1,4 @@
const sharp = require("sharp");
import { logger } from "./logger.utils";
import { ISharpResizedImageStats } from "threetwo-ui-typings";
const imghash = require("imghash");
const leven = require("leven");
@@ -30,13 +29,12 @@ export const resizeImage = async (
.toBuffer();
return await sharp(buffer).toFile(`${outputPath}`, (err, info) => {
if (err) {
logger.error("Failed to resize image:");
logger.error(err);
console.log("Failed to resize image:");
console.log(err);
return err;
}
logger.info("Image file resized with the following parameters:");
logger.info(info);
console.log("Image file %s resized with the following parameters: %o", imageFile, info);
return info;
});
};

View File

@@ -1,19 +0,0 @@
const Pino = require("pino");
export const logger = Pino({
name: "Threetwo!",
prettyPrint: { colorize: true },
crlf: false,
errorLikeObjectKeys: ["err", "error"],
// errorProps: "",
levelFirst: false,
messageKey: "msg", // --messageKey
levelKey: "level", // --levelKey
// messageFormat: false, // --messageFormat
// timestampKey: "time", // --timestampKey
translateTime: false, // --translateTime
// search: "foo == `bar`", // --search
// ignore: "pid,hostname", // --ignore
hideObject: false, // --hideObject
// singleLine: false,
});

View File

@@ -41,13 +41,15 @@ import {
IFolderData,
ISharpResizedImageStats,
} from "threetwo-ui-typings";
import { logger } from "./logger.utils";
import { constructPaths, explodePath, walkFolder } from "../utils/file.utils";
import { resizeImage } from "./imagetransformation.utils";
import { isNil } from "lodash";
const sevenZip = require("7zip-min");
const unrar = require("node-unrar-js");
const { Calibre } = require("node-calibre");
console.info("COMICS_DIRECTORY", process.env.COMICS_DIRECTORY);
console.info("USERDATA_DIRECTORY", process.env.USERDATA_DIRECTORY);
export const extractCoverFromFile = async (
extractionOptions: IExtractionOptions,
@@ -75,9 +77,9 @@ export const extractCoverFromFile = async (
constructedPaths.targetPath,
directoryOptions
);
logger.info(`${constructedPaths.targetPath} was created.`);
console.info(`${constructedPaths.targetPath} was created.`);
} catch (error) {
logger.error(`${error}: Couldn't create directory.`);
console.error(`${error}: Couldn't create directory.`);
}
// extract the cover
@@ -125,11 +127,87 @@ export const extractCoverFromFile = async (
},
});
} catch (error) {
console.log(error);
console.info(error);
}
});
};
export const extractCoverFromFile2 = async (
fileObject: any
): Promise<any> => {
try {
const { filePath, size} = fileObject;
const calibre = new Calibre();
console.info(`Initiating extraction process for path ${filePath}`);
// 1. Check for process.env.COMICS_DIRECTORY and process.env.USERDATA_DIRECTORY
if (!isNil(process.env.USERDATA_DIRECTORY)) {
// 2. Create the directory to which the cover image will be extracted
console.info("Attempting to create target directory for cover extraction...");
const directoryOptions = {
mode: 0o2775,
};
const fileNameWithExtension = path.basename(filePath);
const fileNameWithoutExtension = path.basename(filePath, path.extname(filePath));
const targetDirectory = `${process.env.USERDATA_DIRECTORY}/covers/${fileNameWithoutExtension}`;
await fse.ensureDir(targetDirectory, directoryOptions);
console.info(`%s was created.`, targetDirectory);
// 3. extract the cover
console.info(`Starting cover extraction...`);
let result: string;
const targetCoverImageFilePath = path.resolve(
targetDirectory +
"/" +
fileNameWithoutExtension +
"_cover.jpg"
);
const ebookMetaPath = process.env.CALIBRE_EBOOK_META_PATH
? `${process.env.CALIBRE_EBOOK_META_PATH}`
: `ebook-meta`;
result = await calibre.run(
ebookMetaPath,
[filePath],
{
getCover: targetCoverImageFilePath,
}
);
console.info(`ebook-meta ran with the following result: %o`, result)
// 4. create rendition path
const renditionPath =
targetDirectory+
"/" +
fileNameWithoutExtension +
"_200px.jpg";
// 5. resize image
await resizeImage(
targetCoverImageFilePath,
path.resolve(renditionPath),
200
);
return {
name: fileNameWithoutExtension,
path: filePath,
fileSize: size,
extension: path.extname(filePath),
cover: {
filePath: path.relative(process.cwd(),renditionPath),
},
containedIn: path.dirname(fileNameWithExtension),
calibreMetadata: {
coverWriteResult: result,
},
};
}
} catch (error) {
console.error(error);
}
};
export const unrarArchive = async (
filePath: string,
options: IExtractionOptions
@@ -143,9 +221,9 @@ export const unrarArchive = async (
.readFile(filePath)
.catch((err) => console.error("Failed to read file", err));
try {
logger.info("Unrar initiating.");
console.info("Unrar initiating.");
await fse.ensureDir(options.targetExtractionFolder, directoryOptions);
logger.info(`${options.targetExtractionFolder} was created.`);
console.info(`${options.targetExtractionFolder} was created.`);
const extractor = await unrar.createExtractorFromData({
data: fileBuffer,
@@ -153,7 +231,7 @@ export const unrarArchive = async (
const files = extractor.extract({});
const extractedFiles = [...files.files];
for (const file of extractedFiles) {
logger.info(`Attempting to write ${file.fileHeader.name}`);
console.info(`Attempting to write ${file.fileHeader.name}`);
const fileBuffer = file.extraction;
const fileName = explodePath(file.fileHeader.name).fileName;
// resize image
@@ -170,6 +248,6 @@ export const unrarArchive = async (
".jpeg",
]);
} catch (error) {
logger.error(`${error}`);
console.info(`${error}`);
}
};

View File

@@ -6,7 +6,6 @@ import {
IFolderData,
} from "threetwo-ui-typings";
const Validator = require("fastest-validator");
import { logger } from "./logger.utils";
export const validateComicBookMetadata = (
comicBookMetadataObject: IExtractedComicBookCoverFile
@@ -19,9 +18,9 @@ export const validateComicBookMetadata = (
};
const check = validator.compile(sch);
if (check(comicBookMetadataObject)) {
logger.info(`Valid comic book metadata: ${comicBookMetadataObject}`);
console.log(`Valid comic book metadata: ${comicBookMetadataObject}`);
} else {
logger.error(
console.log(
`Comic book metadata was invalid:
${comicBookMetadataObject}`
);

View File

@@ -28,7 +28,6 @@ SOFTWARE.
import xml2js from "xml2js";
import fs from "fs";
import { logger } from "../utils/logger.utils";
export const convertXMLToJSON = (xmlPayload) => {
const parser = new xml2js.Parser({
@@ -43,6 +42,6 @@ export const convertXMLToJSON = (xmlPayload) => {
return result;
})
.catch((error) => {
logger.error(error);
console.log(error);
});
};