summary refs log tree commit diff
path: root/src/cdn/routes/avatars.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/cdn/routes/avatars.ts')
-rw-r--r--src/cdn/routes/avatars.ts78
1 files changed, 30 insertions, 48 deletions
diff --git a/src/cdn/routes/avatars.ts b/src/cdn/routes/avatars.ts
index 40705b2e..fa26938f 100644
--- a/src/cdn/routes/avatars.ts
+++ b/src/cdn/routes/avatars.ts
@@ -1,10 +1,9 @@
-import { Router, Response, Request } from "express";
-import { Config, Snowflake } from "@fosscord/util";
-import { storage } from "../util/Storage";
-import FileType from "file-type";
-import { HTTPError } from "@fosscord/util";
+import { Config, HTTPError, Snowflake } from "@fosscord/util";
 import crypto from "crypto";
+import { Request, Response, Router } from "express";
+import FileType from "file-type";
 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
@@ -12,51 +11,35 @@ import { multer } from "../util/multer";
 // 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;
-
-		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 = `avatars/${user_id}/${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.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;
+
+	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 = `avatars/${user_id}/${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("/:user_id", async (req: Request, res: Response) => {
 	let { user_id } = req.params;
@@ -89,8 +72,7 @@ router.get("/:user_id/:hash", async (req: Request, res: Response) => {
 });
 
 router.delete("/:user_id/:id", async (req: Request, res: Response) => {
-	if (req.headers.signature !== Config.get().security.requestSignature)
-		throw new HTTPError("Invalid request signature");
+	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}`;