Delete attachments of deleted messages, fix #273

This commit is contained in:
AlTech98 committed 2021-09-13 17:32:31 +02:00
1 parent 12c92a2950
commit aa1f1a22a4
6 files changed
+36 -4

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
import { Attachment } from "@fosscord/util";
import { deleteFile } from "@fosscord/api";
import { URL } from "url";
export async function deleteMessageAttachments(messageId: string, keep?: Attachment[]) {
let attachments = await Attachment.find({ message_id: messageId });
if (keep)
attachments = attachments.filter(x => !keep.map(k => k.id).includes(x.id));
await Promise.all(attachments.map(a => a.remove()));
attachments.forEach(a => deleteFile((new URL(a.url)).pathname)); //We don't need to await since this is done on the cdn
}
+13
View File
@@ -38,3 +38,16 @@ export async function handleFile(path: string, body?: string): Promise<string |
throw new HTTPError("Invalid " + path);
}
}
export async function deleteFile(path: string) {
const response = await fetch(`${Config.get().cdn.endpoint || "http://localhost:3003"}${path}`, {
headers: {
signature: Config.get().security.requestSignature,
},
method: "DELETE",
});
const result = await response.json();
if (response.status !== 200) throw result;
return result;
}