diff --git a/cdn/src/routes/attachments.ts b/cdn/src/routes/attachments.ts
index 59845c94..7c55998b 100644
--- a/cdn/src/routes/attachments.ts
+++ b/cdn/src/routes/attachments.ts
@@ -1,73 +1,87 @@
import { Router, Response, Request } from "express";
import { Config, Snowflake } from "@fosscord/util";
-import { storage } from "../util/Storage";
+import { storage } from "@fosscord/cdn/util/Storage";
import FileType from "file-type";
import { HTTPError } from "lambert-server";
-import { multer } from "../util/multer";
+import { multer } from "@fosscord/cdn/util/multer";
import imageSize from "image-size";
const router = Router();
-router.post("/:channel_id", multer.single("file"), async (req: Request, res: Response) => {
- if (req.headers.signature !== Config.get().security.requestSignature)
- throw new HTTPError("Invalid request signature");
- if (!req.file) throw new HTTPError("file missing");
-
- const { buffer, mimetype, size, originalname, fieldname } = req.file;
- const { channel_id } = req.params;
- const filename = originalname.replaceAll(" ", "_").replace(/[^a-zA-Z0-9._]+/g, "");
- const id = Snowflake.generate();
- const path = `attachments/${channel_id}/${id}/${filename}`;
-
- const endpoint = Config.get()?.cdn.endpoint || "http://localhost:3003";
-
- await storage.set(path, buffer);
- var width;
- var height;
- if (mimetype.includes("image")) {
- const dimensions = imageSize(buffer);
- if (dimensions) {
- width = dimensions.width;
- height = dimensions.height;
+router.post(
+ "/:channel_id",
+ multer.single("file"),
+ async (req: Request, res: Response) => {
+ if (req.headers.signature !== Config.get().security.requestSignature)
+ throw new HTTPError("Invalid request signature");
+ if (!req.file) throw new HTTPError("file missing");
+
+ const { buffer, mimetype, size, originalname, fieldname } = req.file;
+ const { channel_id } = req.params;
+ const filename = originalname
+ .replaceAll(" ", "_")
+ .replace(/[^a-zA-Z0-9._]+/g, "");
+ const id = Snowflake.generate();
+ const path = `attachments/${channel_id}/${id}/${filename}`;
+
+ const endpoint = Config.get()?.cdn.endpoint || "http://localhost:3003";
+
+ await storage.set(path, buffer);
+ var width;
+ var height;
+ if (mimetype.includes("image")) {
+ const dimensions = imageSize(buffer);
+ if (dimensions) {
+ width = dimensions.width;
+ height = dimensions.height;
+ }
}
- }
-
- const file = {
- id,
- content_type: mimetype,
- filename: filename,
- size,
- url: `${endpoint}/${path}`,
- width,
- height,
- };
- return res.json(file);
-});
+ const file = {
+ id,
+ content_type: mimetype,
+ filename: filename,
+ size,
+ url: `${endpoint}/${path}`,
+ width,
+ height,
+ };
+
+ return res.json(file);
+ }
+);
-router.get("/:channel_id/:id/:filename", async (req: Request, res: Response) => {
- const { channel_id, id, filename } = req.params;
+router.get(
+ "/:channel_id/:id/:filename",
+ async (req: Request, res: Response) => {
+ 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 type = await FileType.fromBuffer(file);
+ const file = await storage.get(
+ `attachments/${channel_id}/${id}/${filename}`
+ );
+ if (!file) throw new HTTPError("File not found");
+ const type = await FileType.fromBuffer(file);
- res.set("Content-Type", type?.mime);
- res.set("Cache-Control", "public, max-age=31536000");
+ res.set("Content-Type", type?.mime);
+ res.set("Cache-Control", "public, max-age=31536000");
- return res.send(file);
-});
+ return res.send(file);
+ }
+);
-router.delete("/:channel_id/:id/:filename", async (req: Request, res: Response) => {
- if (req.headers.signature !== Config.get().security.requestSignature)
- throw new HTTPError("Invalid request signature");
+router.delete(
+ "/:channel_id/:id/:filename",
+ async (req: Request, res: Response) => {
+ if (req.headers.signature !== Config.get().security.requestSignature)
+ throw new HTTPError("Invalid request signature");
- const { channel_id, id, filename } = req.params;
- const path = `attachments/${channel_id}/${id}/${filename}`;
+ const { channel_id, id, filename } = req.params;
+ const path = `attachments/${channel_id}/${id}/${filename}`;
- await storage.delete(path);
+ await storage.delete(path);
- return res.send({ success: true });
-});
+ return res.send({ success: true });
+ }
+);
export default router;
diff --git a/cdn/src/routes/avatars.ts b/cdn/src/routes/avatars.ts
index 03388afc..3d745f90 100644
--- a/cdn/src/routes/avatars.ts
+++ b/cdn/src/routes/avatars.ts
@@ -1,9 +1,9 @@
import { Router, Response, Request } from "express";
import { Config, Snowflake } from "@fosscord/util";
-import { storage } from "../util/Storage";
+import { storage } from "@fosscord/cdn/util/Storage";
import FileType from "file-type";
import { HTTPError } from "lambert-server";
-import { multer } from "../util/multer";
+import { multer } from "@fosscord/cdn/util/multer";
import crypto from "crypto";
// TODO: check premium and animated pfp are allowed in the config
@@ -12,36 +12,50 @@ import crypto from "crypto";
// TODO: delete old icons
const ANIMATED_MIME_TYPES = ["image/apng", "image/gif", "image/gifv"];
-const STATIC_MIME_TYPES = ["image/png", "image/jpeg", "image/webp", "image/svg+xml", "image/svg"];
+const STATIC_MIME_TYPES = [
+ "image/png",
+ "image/jpeg",
+ "image/webp",
+ "image/svg+xml",
+ "image/svg",
+];
const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES];
const router = Router();
-router.post("/:user_id", multer.single("file"), async (req: Request, res: Response) => {
- if (req.headers.signature !== Config.get().security.requestSignature)
- throw new HTTPError("Invalid request signature");
- if (!req.file) throw new HTTPError("Missing file");
- const { buffer, mimetype, size, originalname, fieldname } = req.file;
- const { user_id } = req.params;
-
- var hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex");
-
- const type = await FileType.fromBuffer(buffer);
- if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type");
- if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash
-
- const path = `avatars/${user_id}/${hash}`;
- const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
-
- await storage.set(path, buffer);
-
- return res.json({
- id: hash,
- content_type: type.mime,
- size,
- url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
- });
-});
+router.post(
+ "/:user_id",
+ multer.single("file"),
+ async (req: Request, res: Response) => {
+ if (req.headers.signature !== Config.get().security.requestSignature)
+ throw new HTTPError("Invalid request signature");
+ if (!req.file) throw new HTTPError("Missing file");
+ const { buffer, mimetype, size, originalname, fieldname } = req.file;
+ const { user_id } = req.params;
+
+ var hash = crypto
+ .createHash("md5")
+ .update(Snowflake.generate())
+ .digest("hex");
+
+ const type = await FileType.fromBuffer(buffer);
+ if (!type || !ALLOWED_MIME_TYPES.includes(type.mime))
+ throw new HTTPError("Invalid file type");
+ if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash
+
+ const path = `avatars/${user_id}/${hash}`;
+ const endpoint = Config.get().cdn.endpoint || "http://localhost:3003";
+
+ await storage.set(path, buffer);
+
+ return res.json({
+ id: hash,
+ content_type: type.mime,
+ size,
+ url: `${endpoint}${req.baseUrl}/${user_id}/${hash}`,
+ });
+ }
+);
router.get("/:user_id/:hash", async (req: Request, res: Response) => {
var { user_id, hash } = req.params;
diff --git a/cdn/src/routes/external.ts b/cdn/src/routes/external.ts
index 10bb0f7d..e7783ec3 100644
--- a/cdn/src/routes/external.ts
+++ b/cdn/src/routes/external.ts
@@ -2,7 +2,7 @@ import { Router, Response, Request } from "express";
import fetch from "node-fetch";
import { HTTPError } from "lambert-server";
import { Snowflake } from "@fosscord/util";
-import { storage } from "../util/Storage";
+import { storage } from "@fosscord/cdn/util/Storage";
import FileType from "file-type";
import { Config } from "@fosscord/util";
@@ -13,7 +13,8 @@ const DEFAULT_FETCH_OPTIONS: any = {
redirect: "follow",
follow: 1,
headers: {
- "user-agent": "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)",
+ "user-agent":
+ "Mozilla/5.0 (compatible Fosscordbot/0.1; +https://fosscord.com)",
},
size: 1024 * 1024 * 8,
compress: true,
|