summary refs log tree commit diff
path: root/src/util/cdn.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-07 19:21:25 +0200
committerGitHub <noreply@github.com>2021-08-07 19:21:25 +0200
commit31df0bfe7b42646dcf5affbc772ab0869da99df7 (patch)
treeadc1d81e11882924a0f401834e282b55a6c29cbd /src/util/cdn.ts
parent:bug: fix gateway endpoint (diff)
parent:bug: fix handleFile() (diff)
downloadserver-31df0bfe7b42646dcf5affbc772ab0869da99df7.tar.xz
Merge pull request #188 from BanTheNons/guild-icons-banners
Implemented guild icons and banners
Diffstat (limited to 'src/util/cdn.ts')
-rw-r--r--src/util/cdn.ts16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/util/cdn.ts b/src/util/cdn.ts

index a66e2215..b0d0f62a 100644 --- a/src/util/cdn.ts +++ b/src/util/cdn.ts
@@ -1,5 +1,6 @@ import { Config } from "@fosscord/server-util"; import FormData from "form-data"; +import { HTTPError } from "lambert-server"; import fetch from "node-fetch"; export async function uploadFile(path: string, file: Express.Multer.File) { @@ -22,3 +23,18 @@ export async function uploadFile(path: string, file: Express.Multer.File) { if (response.status !== 200) throw result; return result; } + +export async function handleFile(path: string, body?: string): Promise<string | undefined> { + if (!body || !body.startsWith("data:")) return body; + try { + const mimetype = body.split(":")[1].split(";")[0]; + const buffer = Buffer.from(body.split(",")[1], "base64"); + + // @ts-ignore + const { id } = await uploadFile(path, { buffer, mimetype, originalname: "banner" }); + return id; + } catch (error) { + console.error(error); + throw new HTTPError("Invalid icon"); + } +}