🚧 attachment

This commit is contained in:
Flam3rboy 2021-05-27 20:01:38 +02:00
parent 8f66556f5a
commit 293894a4f9

View File

@ -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;
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";
// storage.set(filename, );
await storage.set(originalname, buffer);
const File: Attachment = {
filename,
file: buffer.toString("base64"),
id: Snowflake.generate(),
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"));