From 3b032667851905eeec5c61d50be1e242e1aa739c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 29 May 2021 18:23:16 +0200 Subject: [PATCH] :sparkles: avatars --- src/Server.ts | 10 +++++++ src/routes/attachments.ts | 22 +++++--------- src/routes/avatars.ts | 63 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 src/routes/avatars.ts diff --git a/src/Server.ts b/src/Server.ts index de02a585..15868129 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -1,6 +1,7 @@ import { Server, ServerOptions } from "lambert-server"; import { Config, db } from "@fosscord/server-util"; import path from "path"; +import multerConfig from "multer"; export interface CDNServerOptions extends ServerOptions {} @@ -26,3 +27,12 @@ export class CDNServer extends Server { return super.stop(); } } + +export const multer = multerConfig({ + storage: multerConfig.memoryStorage(), + limits: { + fields: 0, + files: 1, + fileSize: 1024 * 1024 * 100, // 100 mb + }, +}); diff --git a/src/routes/attachments.ts b/src/routes/attachments.ts index f477d1b2..3bbced31 100644 --- a/src/routes/attachments.ts +++ b/src/routes/attachments.ts @@ -1,21 +1,13 @@ import { Router } from "express"; -import multer from "multer"; import { Config, Snowflake } from "@fosscord/server-util"; import { storage } from "../util/Storage"; import FileType from "file-type"; import { HTTPError } from "lambert-server"; +import { multer } from "../Server"; -const multer_ = multer({ - storage: multer.memoryStorage(), - limits: { - fields: 0, - files: 1, - fileSize: 1024 * 1024 * 100, // 100 mb - }, -}); const router = Router(); -router.post("/:channel_id", multer_.single("file"), async (req, res) => { +router.post("/:channel_id", multer.single("file"), async (req, res) => { const { buffer, mimetype, size, originalname, fieldname } = req.file; const { channel_id } = req.params; const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, ""); @@ -31,7 +23,7 @@ router.post("/:channel_id", multer_.single("file"), async (req, res) => { content_type: mimetype, filename: filename, size, - url: `${endpoint}/attachments/${channel_id}/${id}/${filename}`, + url: `${endpoint}/${path}`, }; return res.json(file); @@ -42,9 +34,9 @@ router.get("/:channel_id/:id/:filename", async (req, res) => { const file = await storage.get(`attachments/${channel_id}/${id}/${filename}`); if (!file) throw new HTTPError("File not found"); - const result = await FileType.fromBuffer(file); + const type = await FileType.fromBuffer(file); - res.set("Content-Type", result?.mime); + res.set("Content-Type", type?.mime); return res.send(file); }); @@ -53,9 +45,9 @@ router.delete("/:channel_id/:id/:filename", async (req, res) => { const { channel_id, id, filename } = req.params; const path = `attachments/${channel_id}/${id}/${filename}`; - storage.delete(path); + await storage.delete(path); - return res.send({ success: true, message: "attachment deleted" }); + return res.send({ success: true }); }); export default router; diff --git a/src/routes/avatars.ts b/src/routes/avatars.ts new file mode 100644 index 00000000..c447db9f --- /dev/null +++ b/src/routes/avatars.ts @@ -0,0 +1,63 @@ +import { Router } from "express"; +import { Config, Snowflake } from "@fosscord/server-util"; +import { storage } from "../util/Storage"; +import FileType from "file-type"; +import { HTTPError } from "lambert-server"; +import { multer } from "../Server"; + +// TODO: check premium and animated pfp are allowed in the config +// TODO: generate different sizes of avatar +// TODO: generate different image types of avatar +// TODO: delete old avatars +// TODO: check request signature for modify methods + +const ANIMATED_MIME_TYPES = ["image/apng", "image/gif", "image/gifv"]; +const STATIC_MIME_TYPES = ["image/png", "image/jpeg", "image/webp"]; +const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES]; + +const router = Router(); + +router.post("/:user_id", multer.single("file"), async (req, res) => { + const { buffer, mimetype, size, originalname, fieldname } = req.file; + const { user_id } = req.params; + + const id = Snowflake.generate(); + + const type = await FileType.fromBuffer(buffer); + if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type"); + const path = `avatars/${user_id}/${id}`; + const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; + + await storage.set(path, buffer); + + return res.json({ + id, + content_type: type.mime, + size, + url: `${endpoint}/path`, + }); +}); + +router.get("/:user_id/:id", async (req, res) => { + const { user_id, id } = req.params; + const path = `avatars/${user_id}/${id}`; + + const file = await storage.get(path); + if (!file) throw new HTTPError("not found", 404); + const type = await FileType.fromBuffer(file); + + res.set("Content-Type", type?.mime); + + return res.send(file); +}); + +router.delete("/:user_id/:id", async (req, res) => { + const { user_id, id } = req.params; + const path = `avatars/${user_id}/${id}`; + + await storage.delete(path); + + return res.send({ success: true }); +}); + +export default router;