This commit is contained in:
2021-08-10 11:42:39 -07:00
9 changed files with 119 additions and 126 deletions

View File

@@ -1,9 +1,6 @@
const sharp = require("sharp");
const imghash = require("imghash");
const leven = require("leven");
import path from "path";
import { isNull, reject } from "lodash";
import { logger } from "./logger.utils";
import { ISharpResizedImageStats } from "threetwo-ui-typings";
export const extractMetadataFromImage = async (
imageFilePath: string
@@ -21,35 +18,20 @@ export const resizeImage = async (
outputPath: string,
newWidth: number,
newHeight?: number
): Promise<unknown> => {
return await sharp(imageFile)
.resize(newWidth)
.toFile(`${outputPath}`, (err, info) => {
if (err) {
logger.error("Failed to resize image:");
logger.error(err);
return err;
}
): Promise<ISharpResizedImageStats> => {
return new Promise((resolve, reject) => {
sharp(imageFile)
.resize(newWidth)
.toFile(`${outputPath}`, (err, info) => {
if (err) {
logger.error("Failed to resize image:");
logger.error(err);
reject(err);
}
logger.info("Image file resized with the following parameters:");
logger.info(info);
return info;
});
};
export const calculateLevenshteinDistance = async (
imagePath1: string,
imagePath2: string
): Promise<Record<string, unknown>> => {
console.log("AGANTUK", imagePath1)
const hash1 = await imghash.hash(imagePath1);
const hash2 = await imghash.hash(imagePath2);
console.log("HASHISH", hash1)
if (!isNull(hash1) && !isNull(hash2)) {
return new Promise((resolve, reject) => {
resolve({ levenshteinDistance: leven(hash1, hash2) });
});
} else {
reject("Can't calculate the Levenshtein distance")
}
logger.info("Image file resized with the following parameters:");
logger.info(info);
resolve(info);
});
});
};

View File

@@ -31,7 +31,7 @@ SOFTWARE.
* Initial: 2021/05/04 Rishi Ghan
*/
import { createReadStream, createWriteStream, readFileSync } from "fs";
import { createReadStream, createWriteStream, readFileSync, stat } from "fs";
const fse = require("fs-extra");
import path from "path";
import { each, isEmpty, map, flatten } from "lodash";
@@ -42,6 +42,7 @@ import {
IExtractedComicBookCoverFile,
IExtractionOptions,
IFolderData,
ISharpResizedImageStats,
} from "threetwo-ui-typings";
import { logger } from "./logger.utils";
import { validateComicBookMetadata } from "../utils/validation.utils";
@@ -84,6 +85,7 @@ export const extractCoverFromFile = async (
} catch (error) {
logger.error(`${error}: Couldn't create directory.`);
}
// extract the cover
let result: string;
const targetCoverImageFilePath = path.resolve(constructedPaths.targetPath + "/" + walkedFolder.name + "_cover.jpg")
result = await calibre.run(
@@ -95,15 +97,17 @@ export const extractCoverFromFile = async (
);
// create renditions
const renditionPath = constructedPaths.targetPath + "/" + walkedFolder.name + "_200px.jpg";
await resizeImage(targetCoverImageFilePath, path.resolve(renditionPath), 200);
const stats:ISharpResizedImageStats = await resizeImage(targetCoverImageFilePath, path.resolve(renditionPath), 200);
resolve({
name: walkedFolder.name,
path: constructedPaths.targetPath + "/" + walkedFolder.name + "_200px.jpg", //renditionPath
fileSize: 0,
path: renditionPath,
fileSize: stats.size,
extension: path.extname(constructedPaths.inputFilePath),
containedIn: walkedFolder.containedIn,
//originalPath:
//extension:
calibreMetadata: {
coverWriteResult: result,
}
});
} catch (error) {
console.log(error);
@@ -200,6 +204,7 @@ export const unrar = async (
mode: 0o2775,
};
try {
// read the file into a buffer
const fileBuffer = await readFile(
paths.inputFilePath
).catch((err) => console.error("Failed to read file", err));
@@ -238,8 +243,12 @@ export const unrar = async (
resolve({
name: `${extractedFiles[0].fileHeader.name}`,
path: paths.targetPath,
extension: path.extname(extractedFiles[0].fileHeader.name),
fileSize: extractedFiles[0].fileHeader.packSize,
containedIn: walkedFolder.containedIn,
calibreMetadata: {
coverWriteResult: "",
}
});
} catch (error) {
logger.error(`${error}: Couldn't write file.`);