diff --git a/cdn/src/routes/attachments.ts b/cdn/src/routes/attachments.ts
new file mode 100644
index 00000000..c387aa37
--- /dev/null
+++ b/cdn/src/routes/attachments.ts
@@ -0,0 +1,73 @@
+import { Router, Response, Request } 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 "../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) return;
+
+ 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);
+});
+
+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);
+
+ res.set("Content-Type", type?.mime);
+ res.set("Cache-Control", "public, max-age=31536000");
+
+ 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");
+
+ const { channel_id, id, filename } = req.params;
+ const path = `attachments/${channel_id}/${id}/${filename}`;
+
+ await storage.delete(path);
+
+ return res.send({ success: true });
+});
+
+export default router;
diff --git a/cdn/src/routes/avatars.ts b/cdn/src/routes/avatars.ts
new file mode 100644
index 00000000..60befe2c
--- /dev/null
+++ b/cdn/src/routes/avatars.ts
@@ -0,0 +1,72 @@
+import { Router, Response, Request } 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 "../util/multer";
+import crypto from "crypto";
+
+// TODO: check premium and animated pfp are allowed in the config
+// TODO: generate different sizes of icon
+// TODO: generate different image types of icon
+// 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 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.get("/:user_id/:hash", async (req: Request, res: Response) => {
+ var { user_id, hash } = req.params;
+ hash = hash.split(".")[0]; // remove .file extension
+ const path = `avatars/${user_id}/${hash}`;
+
+ 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);
+ res.set("Cache-Control", "public, max-age=31536000");
+
+ return res.send(file);
+});
+
+router.delete("/:user_id/:id", async (req: Request, res: Response) => {
+ if (req.headers.signature !== Config.get().security.requestSignature)
+ throw new HTTPError("Invalid request signature");
+ const { user_id, id } = req.params;
+ const path = `avatars/${user_id}/${id}`;
+
+ await storage.delete(path);
+
+ return res.send({ success: true });
+});
+
+export default router;
diff --git a/cdn/src/routes/external.ts b/cdn/src/routes/external.ts
new file mode 100644
index 00000000..3abe9c22
--- /dev/null
+++ b/cdn/src/routes/external.ts
@@ -0,0 +1,65 @@
+// @ts-nocheck
+import bodyParser from "body-parser";
+import { Router, Response, Request } from "express";
+import fetch from "node-fetch";
+import crypto from "crypto";
+import { HTTPError } from "lambert-server";
+import { Snowflake } from "@fosscord/server-util";
+import { storage } from "../util/Storage";
+
+const router = Router();
+
+type crawled = {
+ id: string;
+ ogTitle: string;
+ ogType: string;
+ ogDescription: string;
+ ogUrl: string;
+ cachedImage: string;
+};
+
+const DEFAULT_FETCH_OPTIONS: any = {
+ redirect: "follow",
+ follow: 1,
+ headers: {
+ "user-agent": "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)",
+ },
+ size: 1024 * 1024 * 8,
+ compress: true,
+ method: "GET",
+};
+
+router.post("/", bodyParser.json(), async (req: Request, res: Response) => {
+ if (req.headers.signature !== Config.get().security.requestSignature)
+ throw new HTTPError("Invalid request signature");
+ if (!req.body) throw new HTTPError("Invalid Body");
+ const { url } = req.body;
+ if (!url || typeof url !== "string") throw new HTTPError("Invalid url");
+
+ const id = Snowflake.generate();
+
+ try {
+ const response = await fetch(ogImage, DEFAULT_FETCH_OPTIONS);
+ const buffer = await response.buffer();
+
+ await storage.set(`/external/${id}`, buffer);
+
+ res.send({ id });
+ } catch (error) {
+ throw new HTTPError("Couldn't fetch website");
+ }
+});
+
+router.get("/:id/", async (req: Request, res: Response) => {
+ const { id } = req.params;
+
+ const file = await storage.get(`/external/${id}`);
+ if (!file) throw new HTTPError("File not found");
+ const result = await FileType.fromBuffer(file);
+
+ res.set("Content-Type", result?.mime);
+
+ return res.send(file);
+});
+
+export default router;
diff --git a/cdn/src/routes/ping.ts b/cdn/src/routes/ping.ts
new file mode 100644
index 00000000..38daf81e
--- /dev/null
+++ b/cdn/src/routes/ping.ts
@@ -0,0 +1,9 @@
+import { Router, Response, Request } from "express";
+
+const router = Router();
+
+router.get("/", (req: Request, res: Response) => {
+ res.send("pong");
+});
+
+export default router;
|