⌨️ Added a type for image stats
This commit is contained in:
@@ -8,12 +8,7 @@ import {
|
|||||||
} from "moleculer";
|
} from "moleculer";
|
||||||
import {
|
import {
|
||||||
resizeImage,
|
resizeImage,
|
||||||
calculateLevenshteinDistance,
|
|
||||||
} from "../utils/imagetransformation.utils";
|
} from "../utils/imagetransformation.utils";
|
||||||
import https from "https";
|
|
||||||
import fs from "fs";
|
|
||||||
import path from "path";
|
|
||||||
|
|
||||||
export default class ProductsService extends Service {
|
export default class ProductsService extends Service {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
public constructor(
|
public constructor(
|
||||||
@@ -58,44 +53,6 @@ export default class ProductsService extends Service {
|
|||||||
return { resizeOperationStatus: resizeResult };
|
return { resizeOperationStatus: resizeResult };
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
calculateLevenshteinDistance: {
|
|
||||||
rest: "POST /calculateLevenshteinDistance",
|
|
||||||
params: {},
|
|
||||||
async handler(
|
|
||||||
ctx: Context<{
|
|
||||||
imagePath: string;
|
|
||||||
imagePath2: string;
|
|
||||||
options: {
|
|
||||||
match_id: string,
|
|
||||||
};
|
|
||||||
}>
|
|
||||||
) {
|
|
||||||
const fileName = ctx.params.options.match_id + "_" + path.basename(
|
|
||||||
ctx.params.imagePath
|
|
||||||
);
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
https.get(ctx.params.imagePath2, (response) => {
|
|
||||||
const fileStream = response.pipe(
|
|
||||||
fs.createWriteStream(
|
|
||||||
`./userdata/temporary/${fileName}`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
fileStream.on("finish", async () => {
|
|
||||||
const levenshteinDistance = await calculateLevenshteinDistance(
|
|
||||||
ctx.params.imagePath,
|
|
||||||
path.resolve(
|
|
||||||
`./userdata/temporary/${fileName}`
|
|
||||||
)
|
|
||||||
);
|
|
||||||
resolve(levenshteinDistance);
|
|
||||||
});
|
|
||||||
|
|
||||||
}).end();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
methods: {},
|
methods: {},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ const leven = require("leven");
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { isNull, reject } from "lodash";
|
import { isNull, reject } from "lodash";
|
||||||
import { logger } from "./logger.utils";
|
import { logger } from "./logger.utils";
|
||||||
|
import { ISharpResizedImageStats } from "threetwo-ui-typings";
|
||||||
|
|
||||||
export const extractMetadataFromImage = async (
|
export const extractMetadataFromImage = async (
|
||||||
imageFilePath: string
|
imageFilePath: string
|
||||||
@@ -21,35 +22,20 @@ export const resizeImage = async (
|
|||||||
outputPath: string,
|
outputPath: string,
|
||||||
newWidth: number,
|
newWidth: number,
|
||||||
newHeight?: number
|
newHeight?: number
|
||||||
): Promise<unknown> => {
|
): Promise<ISharpResizedImageStats> => {
|
||||||
return await sharp(imageFile)
|
return new Promise((resolve, reject) => {
|
||||||
|
sharp(imageFile)
|
||||||
.resize(newWidth)
|
.resize(newWidth)
|
||||||
.toFile(`${outputPath}`, (err, info) => {
|
.toFile(`${outputPath}`, (err, info) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error("Failed to resize image:");
|
logger.error("Failed to resize image:");
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
return err;
|
reject(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Image file resized with the following parameters:");
|
logger.info("Image file resized with the following parameters:");
|
||||||
logger.info(info);
|
logger.info(info);
|
||||||
return info;
|
resolve(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")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ SOFTWARE.
|
|||||||
* Initial: 2021/05/04 Rishi Ghan
|
* Initial: 2021/05/04 Rishi Ghan
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createReadStream, createWriteStream, readFileSync } from "fs";
|
import { createReadStream, createWriteStream, readFileSync, stat } from "fs";
|
||||||
const fse = require("fs-extra");
|
const fse = require("fs-extra");
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { each, isEmpty, map, flatten } from "lodash";
|
import { each, isEmpty, map, flatten } from "lodash";
|
||||||
@@ -42,6 +42,7 @@ import {
|
|||||||
IExtractedComicBookCoverFile,
|
IExtractedComicBookCoverFile,
|
||||||
IExtractionOptions,
|
IExtractionOptions,
|
||||||
IFolderData,
|
IFolderData,
|
||||||
|
ISharpResizedImageStats,
|
||||||
} from "threetwo-ui-typings";
|
} from "threetwo-ui-typings";
|
||||||
import { logger } from "./logger.utils";
|
import { logger } from "./logger.utils";
|
||||||
import { validateComicBookMetadata } from "../utils/validation.utils";
|
import { validateComicBookMetadata } from "../utils/validation.utils";
|
||||||
@@ -95,12 +96,12 @@ export const extractCoverFromFile = async (
|
|||||||
);
|
);
|
||||||
// create renditions
|
// create renditions
|
||||||
const renditionPath = constructedPaths.targetPath + "/" + walkedFolder.name + "_200px.jpg";
|
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({
|
resolve({
|
||||||
name: walkedFolder.name,
|
name: walkedFolder.name,
|
||||||
path: constructedPaths.targetPath + "/" + walkedFolder.name + "_200px.jpg", //renditionPath
|
path: constructedPaths.targetPath + "/" + walkedFolder.name + "_200px.jpg", //renditionPath
|
||||||
fileSize: 0,
|
fileSize: stats.size,
|
||||||
containedIn: walkedFolder.containedIn,
|
containedIn: walkedFolder.containedIn,
|
||||||
//originalPath:
|
//originalPath:
|
||||||
//extension:
|
//extension:
|
||||||
|
|||||||
Reference in New Issue
Block a user