Handle corrected and original MD5s

This commit is contained in:
Tarrgon
2025-07-19 13:40:10 -04:00
parent 9fdecf8afc
commit 04982a06d6
2 changed files with 24 additions and 14 deletions
+14 -8
View File
@@ -70,21 +70,27 @@ export async function handleMessageCreate(message: Message) {
const match = md5Regex.exec(attachment.name);
md5Regex.lastIndex = 0;
let md5;
const md5s: string[] = [];
if (match) md5 = match[1];
if (match) md5s.push(match[1]);
else if (ALLOWED_MIMETYPES.includes(attachment.contentType!)) {
md5 = await calculateMD5FromURL(attachment.url);
const md5Data = await calculateMD5FromURL(attachment.url);
if (!md5Data) continue;
md5s.push(md5Data.correctedFileMD5, md5Data.originalFileMD5);
}
if (!md5) continue;
if (md5s.length == 0) continue;
const post = await getE621PostByMd5(md5);
for (const md5 of md5s) {
const post = await getE621PostByMd5(md5);
if (post) {
if (await blacklistIfNecessary(message, [post])) return;
if (post) {
if (await blacklistIfNecessary(message, [post])) return;
responses.push(`<${getPostUrl(post)}>`);
responses.push(`<${getPostUrl(post)}>`);
continue;
}
}
}
+10 -6
View File
@@ -14,7 +14,7 @@ export function calculateMD5(data: Buffer): string {
return crypto.createHash('md5').update(data).digest('hex');
}
export async function downloadFile(url: string): Promise<Buffer | null> {
export async function downloadFile(url: string): Promise<Buffer[] | null> {
try {
const res = await fetch(url);
@@ -44,18 +44,22 @@ export async function downloadFile(url: string): Promise<Buffer | null> {
finalData = Buffer.from(data);
}
return finalData;
return [finalData, Buffer.from(data)];
} catch (e) {
console.error(e);
return null;
}
}
export async function calculateMD5FromURL(url: string): Promise<string | null> {
export async function calculateMD5FromURL(url: string): Promise<{ correctedFileMD5: string, originalFileMD5: string } | null> {
try {
const file = await downloadFile(url);
if (!file) return null;
return calculateMD5(file);
const files = await downloadFile(url);
if (!files) return null;
return {
correctedFileMD5: calculateMD5(files[0]),
originalFileMD5: calculateMD5(files[1])
};
} catch (e) {
console.error(e);
return null;