Handle corrected and original MD5s
This commit is contained in:
@@ -70,21 +70,27 @@ export async function handleMessageCreate(message: Message) {
|
|||||||
const match = md5Regex.exec(attachment.name);
|
const match = md5Regex.exec(attachment.name);
|
||||||
md5Regex.lastIndex = 0;
|
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!)) {
|
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 (post) {
|
||||||
if (await blacklistIfNecessary(message, [post])) return;
|
if (await blacklistIfNecessary(message, [post])) return;
|
||||||
|
|
||||||
responses.push(`<${getPostUrl(post)}>`);
|
responses.push(`<${getPostUrl(post)}>`);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-6
@@ -14,7 +14,7 @@ export function calculateMD5(data: Buffer): string {
|
|||||||
return crypto.createHash('md5').update(data).digest('hex');
|
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 {
|
try {
|
||||||
const res = await fetch(url);
|
const res = await fetch(url);
|
||||||
|
|
||||||
@@ -44,18 +44,22 @@ export async function downloadFile(url: string): Promise<Buffer | null> {
|
|||||||
finalData = Buffer.from(data);
|
finalData = Buffer.from(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return finalData;
|
return [finalData, Buffer.from(data)];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function calculateMD5FromURL(url: string): Promise<string | null> {
|
export async function calculateMD5FromURL(url: string): Promise<{ correctedFileMD5: string, originalFileMD5: string } | null> {
|
||||||
try {
|
try {
|
||||||
const file = await downloadFile(url);
|
const files = await downloadFile(url);
|
||||||
if (!file) return null;
|
if (!files) return null;
|
||||||
return calculateMD5(file);
|
|
||||||
|
return {
|
||||||
|
correctedFileMD5: calculateMD5(files[0]),
|
||||||
|
originalFileMD5: calculateMD5(files[1])
|
||||||
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user