⌨️ Added a type for image stats

This commit is contained in:
2021-08-03 22:11:58 -07:00
parent 51d8f94068
commit 29851c5736
3 changed files with 21 additions and 77 deletions

View File

@@ -4,6 +4,7 @@ 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 +22,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";
@@ -95,12 +96,12 @@ 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,
fileSize: stats.size,
containedIn: walkedFolder.containedIn,
//originalPath:
//extension: