From c396fac204c6ba9e433d5fcee5d39d03d5985a89 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 7 Aug 2021 19:15:48 +0200 Subject: :art: add handleFile() --- src/util/cdn.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/util') diff --git a/src/util/cdn.ts b/src/util/cdn.ts index a66e2215..fb0cfb53 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,17 @@ 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 { + 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}/${guild_id}`, { buffer, mimetype, originalname: "banner" }); + return id; + } catch (error) { + throw new HTTPError("Invalid " + path); + } +} -- cgit 1.5.1