Downgrade nsfwjs and return saveAttachment as it is elsewhere used
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 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 @@
const attachments = [];
for (const attachment of message.attachments.values()) {
attachments.push({
- local: (await streamAttachment(attachment.url))[0],
+ local: (await saveAttachment(attachment.url))[0],
url: attachment.url,
height: attachment.height,
width: attachment.width,
@@ -25,7 +25,7 @@
for (const link of links) {
if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) {
attachments.push({
- local: (await streamAttachment(link))[0],
+ local: (await saveAttachment(link))[0],
url: link,
height: null,
width: null
diff --git a/src/reflex/scanners.ts b/src/reflex/scanners.ts
index fb1a436..53c8c9b 100644
--- a/src/reflex/scanners.ts
+++ b/src/reflex/scanners.ts
@@ -1,13 +1,13 @@
import fetch from "node-fetch";
-import fs, { writeFileSync, createReadStream } from "fs";
+import { writeFileSync } from "fs";
import generateFileName from "../utils/temp/generateFileName.js";
import Tesseract from "node-tesseract-ocr";
import type Discord from "discord.js";
import client from "../utils/client.js";
import { createHash } from "crypto";
-import * as nsfwjs from "nsfwjs/src";
-import * as clamscan from "clamscan";
-import * as tf from "@tensorflow/tfjs-node";
+import * as nsfwjs from "nsfwjs";
+// import * as clamscan from "clamscan";
+import * as tf from "@tensorflow/tfjs";
import EmojiEmbed from "../utils/generateEmojiEmbed.js";
import getEmojiByName from "../utils/getEmojiByName.js";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
@@ -28,7 +28,7 @@
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 testMalware(link: string): Promise<MalwareSchema> {
- 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<MalwareSchema>) : { 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<MalwareSchema>) : { 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[] }> {
@@ -90,10 +90,16 @@
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 = {