summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-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"); + } +}