🛣 Levenshtein distance between images WIP
This commit is contained in:
@@ -6,7 +6,13 @@ import {
|
||||
ServiceSchema,
|
||||
Errors,
|
||||
} from "moleculer";
|
||||
import { resizeImage } from "../utils/imagetransformation.utils";
|
||||
import {
|
||||
resizeImage,
|
||||
calculateLevenshteinDistance,
|
||||
} from "../utils/imagetransformation.utils";
|
||||
import https from "https";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default class ProductsService extends Service {
|
||||
// @ts-ignore
|
||||
@@ -52,6 +58,44 @@ export default class ProductsService extends Service {
|
||||
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: {},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user