summary refs log tree commit diff
path: root/api/src/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-14 13:18:32 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-14 13:18:32 +0200
commit37533e28fdf6380efa06b13e0a09a3b2350115e2 (patch)
treec458ca4571ff9ff0bb56538bd9479868e72a4456 /api/src/util
parent:bug: fix fosscord.js (diff)
parentMerge pull request #360 from AlTech98/invite (diff)
downloadserver-37533e28fdf6380efa06b13e0a09a3b2350115e2.tar.xz
Merge branch 'master' of https://github.com/fosscord/fosscord-api
Diffstat (limited to 'api/src/util')
-rw-r--r--api/src/util/Attachments.ts12
-rw-r--r--api/src/util/cdn.ts13
2 files changed, 25 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 +} diff --git a/api/src/util/cdn.ts b/api/src/util/cdn.ts
index 3c71d980..88b0ea0d 100644 --- a/api/src/util/cdn.ts +++ b/api/src/util/cdn.ts
@@ -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; +}