diff --git a/src/events/handle-message.ts b/src/events/handle-message.ts index d44de59..d1661f1 100644 --- a/src/events/handle-message.ts +++ b/src/events/handle-message.ts @@ -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; + } } } diff --git a/src/utils/file-utils.ts b/src/utils/file-utils.ts index 7201701..64da845 100644 --- a/src/utils/file-utils.ts +++ b/src/utils/file-utils.ts @@ -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 { +export async function downloadFile(url: string): Promise { try { const res = await fetch(url); @@ -44,18 +44,22 @@ export async function downloadFile(url: string): Promise { 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 { +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;