diff --git a/src/cdn/Server.ts b/src/cdn/Server.ts
index 45d3abd9..1fd9ca38 100644
--- a/src/cdn/Server.ts
+++ b/src/cdn/Server.ts
@@ -2,6 +2,7 @@ import { Server, ServerOptions } from "lambert-server";
import { Config, initDatabase, registerRoutes } from "@fosscord/util";
import path from "path";
import avatarsRoute from "./routes/avatars";
+import guildProfilesRoute from "./routes/guild-profiles";
import iconsRoute from "./routes/role-icons";
import bodyParser from "body-parser";
@@ -74,6 +75,12 @@ export class CDNServer extends Server {
this.app.use("/channel-icons/", avatarsRoute);
this.log("verbose", "[Server] Route /channel-icons registered");
+ this.app.use("/guilds/:guild_id/users/:user_id/avatars", guildProfilesRoute);
+ this.log("verbose", "[Server] Route /guilds/avatars registered");
+
+ this.app.use("/guilds/:guild_id/users/:user_id/banners", guildProfilesRoute);
+ this.log("verbose", "[Server] Route /guilds/banners registered");
+
return super.start();
}
diff --git a/src/cdn/routes/guild-profiles.ts b/src/cdn/routes/guild-profiles.ts
new file mode 100644
index 00000000..98af7f69
--- /dev/null
+++ b/src/cdn/routes/guild-profiles.ts
@@ -0,0 +1,85 @@
+import { Config, Snowflake } from "@fosscord/util";
+import crypto from "crypto";
+import { Request, Response, Router } from "express";
+import FileType from "file-type";
+import { HTTPError } from "lambert-server";
+import { multer } from "../util/multer";
+import { storage } from "../util/Storage";
+
+// 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("/", 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 { guild_id, user_id } = req.params;
+
+ let 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 = `guilds/${guild_id}/users/${user_id}/avatars/${hash}`;
+ const endpoint = Config.get().cdn.endpointPublic || "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("/", async (req: Request, res: Response) => {
+ let { guild_id, user_id } = req.params;
+ user_id = user_id.split(".")[0]; // remove .file extension
+ const path = `guilds/${guild_id}/users/${user_id}/avatars`;
+
+ 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.get("/:hash", async (req: Request, res: Response) => {
+ let { guild_id, user_id, hash } = req.params;
+ hash = hash.split(".")[0]; // remove .file extension
+ const path = `guilds/${guild_id}/users/${user_id}/avatars/${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("/:id", async (req: Request, res: Response) => {
+ if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature");
+ const { guild_id, user_id, id } = req.params;
+ const path = `guilds/${guild_id}/users/${user_id}/avatars/${id}`;
+
+ await storage.delete(path);
+
+ return res.send({ success: true });
+});
+
+export default router;
|