summary refs log tree commit diff
path: root/src/routes/attachments.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-28 21:19:19 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-28 21:19:19 +0200
commit614d8f14fa2bac5c4f4357d51b4df527bea93c19 (patch)
tree31c97a8c2bc2c73a9cf35a2fe75a15535bde86e7 /src/routes/attachments.ts
parentUpdate attachments.ts (diff)
downloadserver-614d8f14fa2bac5c4f4357d51b4df527bea93c19.tar.xz
:sparkles: attachments
Diffstat (limited to '')
-rw-r--r--src/routes/attachments.ts42
1 files changed, 24 insertions, 18 deletions
diff --git a/src/routes/attachments.ts b/src/routes/attachments.ts

index 730d2bb1..f477d1b2 100644 --- a/src/routes/attachments.ts +++ b/src/routes/attachments.ts
@@ -2,6 +2,8 @@ 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"; const multer_ = multer({ storage: multer.memoryStorage(), @@ -13,42 +15,46 @@ const multer_ = multer({ }); const router = Router(); -router.post("/:channel_id", multer_.single("attachment"), async (req, res) => { - const { buffer, mimetype, stream, size, originalname, fieldname } = req.file; +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(/\W+/g, ""); - - const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; + const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, ""); + const id = Snowflake.generate(); + const path = `attachments/${channel_id}/${id}/${filename}`; - await storage.set(originalname, buffer); + const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; - const id = Snowflake.generate(); + await storage.set(path, buffer); const file = { id, - type: mimetype, content_type: mimetype, - filename: originalname, + filename: filename, size, - url: `${endpoint}/attachments/${channel_id}/${id}/`, + url: `${endpoint}/attachments/${channel_id}/${id}/${filename}`, }; return res.json(file); }); -router.get("/:hash/:filename", async (req, res) => { - const { hash, filename } = req.params; +router.get("/:channel_id/:id/:filename", async (req, res) => { + const { channel_id, id, filename } = req.params; + + 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 File = await db.data.attachments({ id: hash, filename: filename }).get(); + res.set("Content-Type", result?.mime); - res.set("Content-Type", File.type); - return res.send(Buffer.from(File.file, "base64")); + return res.send(file); }); -router.delete("/:hash/:filename", async (req, res) => { - const { hash, filename } = req.params; +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 db.data.attachments({ id: hash, filename: filename }).delete(); return res.send({ success: true, message: "attachment deleted" }); });