mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-11 13:14:06 +01:00
✨ avatars
This commit is contained in:
parent
21e659fce5
commit
3b03266785
@ -1,6 +1,7 @@
|
|||||||
import { Server, ServerOptions } from "lambert-server";
|
import { Server, ServerOptions } from "lambert-server";
|
||||||
import { Config, db } from "@fosscord/server-util";
|
import { Config, db } from "@fosscord/server-util";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import multerConfig from "multer";
|
||||||
|
|
||||||
export interface CDNServerOptions extends ServerOptions {}
|
export interface CDNServerOptions extends ServerOptions {}
|
||||||
|
|
||||||
@ -26,3 +27,12 @@ export class CDNServer extends Server {
|
|||||||
return super.stop();
|
return super.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const multer = multerConfig({
|
||||||
|
storage: multerConfig.memoryStorage(),
|
||||||
|
limits: {
|
||||||
|
fields: 0,
|
||||||
|
files: 1,
|
||||||
|
fileSize: 1024 * 1024 * 100, // 100 mb
|
||||||
|
},
|
||||||
|
});
|
||||||
|
@ -1,21 +1,13 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import multer from "multer";
|
|
||||||
import { Config, Snowflake } from "@fosscord/server-util";
|
import { Config, Snowflake } from "@fosscord/server-util";
|
||||||
import { storage } from "../util/Storage";
|
import { storage } from "../util/Storage";
|
||||||
import FileType from "file-type";
|
import FileType from "file-type";
|
||||||
import { HTTPError } from "lambert-server";
|
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();
|
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 { buffer, mimetype, size, originalname, fieldname } = req.file;
|
||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, "");
|
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,
|
content_type: mimetype,
|
||||||
filename: filename,
|
filename: filename,
|
||||||
size,
|
size,
|
||||||
url: `${endpoint}/attachments/${channel_id}/${id}/${filename}`,
|
url: `${endpoint}/${path}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.json(file);
|
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}`);
|
const file = await storage.get(`attachments/${channel_id}/${id}/${filename}`);
|
||||||
if (!file) throw new HTTPError("File not found");
|
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);
|
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 { channel_id, id, filename } = req.params;
|
||||||
const path = `attachments/${channel_id}/${id}/${filename}`;
|
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;
|
export default router;
|
||||||
|
63
src/routes/avatars.ts
Normal file
63
src/routes/avatars.ts
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user