From 3217998db27e7824182aea043a00474269391628 Mon Sep 17 00:00:00 2001 From: Skyler Grey Date: Tue, 7 Mar 2023 23:59:06 +0000 Subject: [PATCH] Downgrade nsfwjs and return saveAttachment as it is elsewhere used --- package.json | 4 +-- src/premium/attachmentLogs.ts | 6 ++-- src/reflex/scanners.ts | 64 +++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 9d3df42..629c6dd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "dependencies": { "@hokify/agenda": "^6.2.12", - "@tensorflow/tfjs": "^4.0.0", + "@tensorflow/tfjs": "^3.18.0", "@tensorflow/tfjs-node": "^4.2.0", "@total-typescript/ts-reset": "^0.3.7", "@tsconfig/node18-strictest-esm": "^1.0.0", @@ -21,7 +21,7 @@ "mongodb": "^4.7.0", "node-fetch": "^3.3.0", "node-tesseract-ocr": "^2.2.1", - "nsfwjs": "https://github.com/infinitered/nsfwjs", + "nsfwjs": "^2.4.2", "seedrandom": "^3.0.5", "structured-clone": "^0.2.2", "systeminformation": "^5.17.3" diff --git a/src/premium/attachmentLogs.ts b/src/premium/attachmentLogs.ts index ca60b2c..e719f22 100644 --- a/src/premium/attachmentLogs.ts +++ b/src/premium/attachmentLogs.ts @@ -2,7 +2,7 @@ import { getCommandMentionByName } from "./../utils/getCommandDataByName.js"; import client from "../utils/client.js"; import keyValueList from "../utils/generateKeyValueList.js"; import singleNotify from "../utils/singleNotify.js"; -import { streamAttachment } from "../reflex/scanners.js"; +import { saveAttachment } from "../reflex/scanners.js"; import EmojiEmbed from "../utils/generateEmojiEmbed.js"; import addPlural from "../utils/plurals.js"; import type { GuildTextBasedChannel, Message } from "discord.js"; @@ -14,7 +14,7 @@ export default async function logAttachment(message: Message): Promise { const alreadyHaveCheck = await client.database.scanCache.read(hash); if (alreadyHaveCheck?.nsfw) return { nsfw: alreadyHaveCheck.nsfw }; - const image = tf.node.decodeImage(new Uint8Array(fileStream), 3) as tf.Tensor3D; + const image = tf.tensor3d(new Uint8Array(fileStream)); const predictions = (await nsfw_model.classify(image, 1))[0]!; image.dispose(); @@ -37,32 +37,32 @@ export async function testNSFW(link: string): Promise { } export async function testMalware(link: string): Promise { - const [p, hash] = await streamAttachment(link); + const [_, hash] = await saveAttachment(link); const alreadyHaveCheck = await client.database.scanCache.read(hash); if (alreadyHaveCheck?.malware) return { safe: alreadyHaveCheck.malware }; return { safe: true }; - const data = new URLSearchParams(); - // const f = createReadStream(p); - data.append("file", f.read(fs.statSync(p).size)); - const result = await fetch("https://unscan.p.rapidapi.com/malware", { - method: "POST", - headers: { - "X-RapidAPI-Key": client.config.rapidApiKey, - "X-RapidAPI-Host": "unscan.p.rapidapi.com" - }, - body: data - }) - .then((response) => - response.status === 200 ? (response.json() as Promise) : { safe: true, errored: true } - ) - .catch((err) => { - console.error(err); - return { safe: true, errored: true }; - }); - if (!result.errored) { - client.database.scanCache.write(hash, "malware", result.safe); - } - return { safe: result.safe }; + // const data = new URLSearchParams(); + // // const f = createReadStream(p); + // data.append("file", f.read(fs.statSync(p).size)); + // const result = await fetch("https://unscan.p.rapidapi.com/malware", { + // method: "POST", + // headers: { + // "X-RapidAPI-Key": client.config.rapidApiKey, + // "X-RapidAPI-Host": "unscan.p.rapidapi.com" + // }, + // body: data + // }) + // .then((response) => + // response.status === 200 ? (response.json() as Promise) : { safe: true, errored: true } + // ) + // .catch((err) => { + // console.error(err); + // return { safe: true, errored: true }; + // }); + // if (!result.errored) { + // client.database.scanCache.write(hash, "malware", result.safe); + // } + // return { safe: result.safe }; } export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> { @@ -89,11 +89,17 @@ export async function testLink(link: string): Promise<{ safe: boolean; tags: str } export async function streamAttachment(link: string): Promise<[ArrayBuffer, string]> { + const image = await (await fetch(link)).arrayBuffer(); + const enc = new TextDecoder("utf-8"); + return [image, createHash("sha512").update(enc.decode(image), "base64").digest("base64")]; +} + +export async function saveAttachment(link: string): Promise<[string, string]> { const image = await (await fetch(link)).arrayBuffer(); const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!); const enc = new TextDecoder("utf-8"); writeFileSync(fileName, new DataView(image), "base64"); - return [image, createHash("sha512").update(enc.decode(image), "base64").digest("base64")]; + return [fileName, createHash("sha512").update(enc.decode(image), "base64").digest("base64")]; } const linkTypes = {