🚧 attachment
This commit is contained in:
parent
8f66556f5a
commit
293894a4f9
@ -1,35 +1,45 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import multer from "multer";
|
import multer from "multer";
|
||||||
import { Snowflake } from "@fosscord/server-util";
|
import { Config, Snowflake } from "@fosscord/server-util";
|
||||||
import { storage } from "../util/Storage";
|
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();
|
const router = Router();
|
||||||
|
|
||||||
type Attachment = {
|
router.post("/:channel_id", multer_.single("attachment"), async (req, res) => {
|
||||||
filename: string;
|
const { buffer, mimetype, stream, size, originalname, fieldname } = req.file;
|
||||||
file: string;
|
const { channel_id } = req.params;
|
||||||
id: string;
|
const filename = originalname.replaceAll(" ", "_").replace(/\W+/g, "");
|
||||||
type: string;
|
t;
|
||||||
};
|
const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
|
||||||
router.post("/:filename", multer_.single("attachment"), async (req, res) => {
|
|
||||||
const { buffer, mimetype } = req.file;
|
|
||||||
const { filename } = req.params;
|
|
||||||
|
|
||||||
// storage.set(filename, );
|
await storage.set(originalname, buffer);
|
||||||
|
|
||||||
const File: Attachment = {
|
const id = Snowflake.generate();
|
||||||
filename,
|
|
||||||
file: buffer.toString("base64"),
|
const file = {
|
||||||
id: Snowflake.generate(),
|
id,
|
||||||
type: mimetype,
|
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) => {
|
router.get("/:hash/:filename", async (req, res) => {
|
||||||
const { hash, filename } = req.params;
|
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);
|
res.set("Content-Type", File.type);
|
||||||
return res.send(Buffer.from(File.file, "base64"));
|
return res.send(Buffer.from(File.file, "base64"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user