summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-27 20:01:38 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-27 20:01:38 +0200
commit293894a4f9df62438f092398bd55959e2f23c23e (patch)
tree1415444d7d9bb3c8181d54376da248eb54b503b3
parent:construction: WIP rewrite (diff)
downloadserver-293894a4f9df62438f092398bd55959e2f23c23e.tar.xz
:construction: attachment
-rw-r--r--src/routes/attachments.ts48
1 files changed, 29 insertions, 19 deletions
diff --git a/src/routes/attachments.ts b/src/routes/attachments.ts
index 87368e48..fb0f0c32 100644
--- a/src/routes/attachments.ts
+++ b/src/routes/attachments.ts
@@ -1,35 +1,45 @@
 import { Router } from "express";
 import multer from "multer";
-import { Snowflake } from "@fosscord/server-util";
+import { Config, Snowflake } from "@fosscord/server-util";
 import { storage } from "../util/Storage";
 
-const multer_ = multer();
+const multer_ = multer({
+	storage: multer.memoryStorage(),
+	limits: {
+		fields: 0,
+		files: 1,
+		fileSize: 1024 * 1024 * 100, // 100 mb
+	},
+});
 const router = Router();
 
-type Attachment = {
-	filename: string;
-	file: string;
-	id: string;
-	type: string;
-};
-router.post("/:filename", multer_.single("attachment"), async (req, res) => {
-	const { buffer, mimetype } = req.file;
-	const { filename } = req.params;
-
-	// storage.set(filename, );
-
-	const File: Attachment = {
-		filename,
-		file: buffer.toString("base64"),
-		id: Snowflake.generate(),
+router.post("/:channel_id", multer_.single("attachment"), async (req, res) => {
+	const { buffer, mimetype, stream, size, originalname, fieldname } = req.file;
+	const { channel_id } = req.params;
+	const filename = originalname.replaceAll(" ", "_").replace(/\W+/g, "");
+	t;
+	const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
+
+	await storage.set(originalname, buffer);
+
+	const id = Snowflake.generate();
+
+	const file = {
+		id,
 		type: mimetype,
+		content_type: mimetype,
+		filename: originalname,
+		size,
+		url: `${endpoint}/attachments/${channel_id}/${id}/`,
 	};
+
+	return res.json(file);
 });
 
 router.get("/:hash/:filename", async (req, res) => {
 	const { hash, filename } = req.params;
 
-	const File: Attachment = await db.data.attachments({ id: hash, filename: filename }).get();
+	const File = await db.data.attachments({ id: hash, filename: filename }).get();
 
 	res.set("Content-Type", File.type);
 	return res.send(Buffer.from(File.file, "base64"));