diff --git "a/src/routes/guilds/\043guild_id/index.ts" "b/src/routes/guilds/\043guild_id/index.ts" index 3af4910..051b44c 100644 --- "a/src/routes/guilds/\043guild_id/index.ts" +++ "b/src/routes/guilds/\043guild_id/index.ts" @@ -17,6 +17,7 @@ import { GuildUpdateSchema } from "../../../schema/Guild"; import { emitEvent } from "../../../util/Event"; import { check } from "../../../util/instanceOf"; +import { uploadFile } from "../../../util/cdn"; import "missing-native-js-functions"; const router = Router(); @@ -42,6 +43,32 @@ const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); + if (body.icon && body.icon.startsWith('data')) { + try { + const mimetype = body.icon.split(":")[1].split(";")[0]; + const buffer = Buffer.from(body.icon.split(",")[1], "base64"); + + // @ts-ignore + const { id } = await uploadFile(`/icons/${guild_id}`, { buffer, mimetype, originalname: "icon" }); + body.icon = id; + } catch (error) { + throw new HTTPError("Invalid icon"); + } + } + + if (body.banner && body.banner.startsWith('data')) { + try { + const mimetype = body.banner.split(":")[1].split(";")[0]; + const buffer = Buffer.from(body.banner.split(",")[1], "base64"); + + // @ts-ignore + const { id } = await uploadFile(`/banners/${guild_id}`, { buffer, mimetype, originalname: "banner" }); + body.banner = id; + } catch (error) { + throw new HTTPError("Invalid banner"); + } + } + const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body) .populate({ path: "joined_at", match: { id: req.user_id } }) .exec(); @@ -50,7 +77,7 @@ emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent); - return res.send(data); + return res.json(data); }); export default router;