summary refs log tree commit diff
path: root/api/src/util/Attachments.ts
diff options
context:
space:
mode:
authorAlTech98 <altech123159@gmail.com>2021-09-13 17:32:31 +0200
committerAlTech98 <altech123159@gmail.com>2021-09-13 17:32:31 +0200
commitb438a21b9c252ace62f9ce803bae939cbc1677e3 (patch)
tree89367fdebb4f2592b5183047b04ec4259daeaaf3 /api/src/util/Attachments.ts
parentFix attachments not being saved to db (diff)
downloadserver-b438a21b9c252ace62f9ce803bae939cbc1677e3.tar.xz
Delete attachments of deleted messages, fix #273
Diffstat (limited to '')
-rw-r--r--api/src/util/Attachments.ts12
1 files changed, 12 insertions, 0 deletions
diff --git a/api/src/util/Attachments.ts b/api/src/util/Attachments.ts
new file mode 100644

index 00000000..addda97f --- /dev/null +++ b/api/src/util/Attachments.ts
@@ -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 +}