diff --git a/src/routes/guilds/#guild_id/index.ts b/src/routes/guilds/#guild_id/index.ts
index 3af49106..8e052f6d 100644
--- a/src/routes/guilds/#guild_id/index.ts
+++ b/src/routes/guilds/#guild_id/index.ts
@@ -17,6 +17,7 @@ import { HTTPError } from "lambert-server";
import { GuildUpdateSchema } from "../../../schema/Guild";
import { emitEvent } from "../../../util/Event";
import { check } from "../../../util/instanceOf";
+import { handleFile } from "../../../util/cdn";
import "missing-native-js-functions";
const router = Router();
@@ -42,6 +43,9 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_GUILD");
+ body.icon = await handleFile(`/icons/${guild_id}`, body.icon);
+ body.banner = await handleFile(`/banners/${guild_id}`, body.banner);
+
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
.populate({ path: "joined_at", match: { id: req.user_id } })
.exec();
@@ -50,7 +54,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent);
- return res.send(data);
+ return res.json(data);
});
export default router;
diff --git a/src/routes/users/@me/index.ts b/src/routes/users/@me/index.ts
index 68196afe..185e44d4 100644
--- a/src/routes/users/@me/index.ts
+++ b/src/routes/users/@me/index.ts
@@ -4,7 +4,7 @@ import { HTTPError } from "lambert-server";
import { getPublicUser } from "../../../util/User";
import { UserModifySchema } from "../../../schema/User";
import { check } from "../../../util/instanceOf";
-import { uploadFile } from "../../../util/cdn";
+import { handleFile } from "../../../util/cdn";
const router: Router = Router();
@@ -14,19 +14,7 @@ router.get("/", async (req: Request, res: Response) => {
router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => {
const body = req.body as UserModifySchema;
-
- if (body.avatar) {
- try {
- const mimetype = body.avatar.split(":")[1].split(";")[0];
- const buffer = Buffer.from(body.avatar.split(",")[1], "base64");
-
- // @ts-ignore
- const { id } = await uploadFile(`/avatars/${req.user_id}`, { buffer, mimetype, originalname: "avatar" });
- body.avatar = id;
- } catch (error) {
- throw new HTTPError("Invalid avatar");
- }
- }
+ body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: PublicUserProjection }).exec();
// TODO: dispatch user update event
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");
+ }
+}
|