From 7343f5df83da0c4f185059bf9528d9737648a293 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Thu, 20 Jul 2023 13:25:42 +0000 Subject: [PATCH] handle error on `flagNsfwByAttachments` --- .../src/utils/indecent-prediction/index.js | 77 ++++++++++--------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/packages/server/src/utils/indecent-prediction/index.js b/packages/server/src/utils/indecent-prediction/index.js index a22ade17..1749c440 100755 --- a/packages/server/src/utils/indecent-prediction/index.js +++ b/packages/server/src/utils/indecent-prediction/index.js @@ -23,46 +23,51 @@ if (global.isProduction) { } export default async (payload) => { - let { url, image, channels = 3 } = payload + try { + let { url, image, channels = 3 } = payload - let file = null - const model = await nsfwjs.load() + let file = null + const model = await nsfwjs.load() - if (!image && url) { - file = await downloadFile({ url }) + if (!image && url) { + file = await downloadFile({ url }) - image = file.destination - } - - // check if image is not a jpg - if (image.indexOf(".jpg") === -1) { - // convert image to jpg - const converted = await sharp(image) - .jpeg() - .toBuffer() - - // write converted image to disk (use cache) - const destination = path.resolve(global.uploadCachePath, `${Date.now()}.jpg`) - - fs.writeFileSync(destination, converted) - - // set image to the converted image - file = { - destination, - delete: () => fs.unlinkSync(destination), + image = file.destination } - image = destination + // check if image is not a jpg + if (image.indexOf(".jpg") === -1) { + // convert image to jpg + const converted = await sharp(image) + .jpeg() + .toBuffer() + + // write converted image to disk (use cache) + const destination = path.resolve(global.uploadCachePath, `${Date.now()}.jpg`) + + fs.writeFileSync(destination, converted) + + // set image to the converted image + file = { + destination, + delete: () => fs.unlinkSync(destination), + } + + image = destination + } + + const logo = readImage(image) + const input = imageToInput(logo, channels) + + const predictions = await model.classify(input) + + if (typeof file.delete === "function") { + await file.delete() + } + + return predictions + } catch (error) { + console.error(`Failed to process image >`, error) + console.trace() } - - const logo = readImage(image) - const input = imageToInput(logo, channels) - - const predictions = await model.classify(input) - - if (typeof file.delete === "function") { - await file.delete() - } - - return predictions } \ No newline at end of file