From 3a6c5e5f74f05cd51cc6c5b54b0da8f68ecaba8a Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 19 Apr 2026 19:02:15 +0200 Subject: [PATCH] CDN: clean up unused imports --- src/cdn/routes/app-assets.ts | 2 +- src/cdn/routes/app-icons.ts | 2 +- src/cdn/routes/attachments.ts | 2 +- src/cdn/routes/avatars.ts | 2 +- src/cdn/routes/banners.ts | 2 +- src/cdn/routes/channel-icons.ts | 2 +- src/cdn/routes/discover-splashes.ts | 2 +- src/cdn/routes/discovery-splashes.ts | 2 +- src/cdn/routes/emojis.ts | 2 +- src/cdn/routes/guild-profiles.ts | 2 +- src/cdn/routes/icons.ts | 2 +- src/cdn/routes/role-icons.ts | 4 +- src/cdn/routes/splashes.ts | 2 +- src/cdn/routes/stickers.ts | 2 +- src/cdn/routes/team-icons.ts | 2 +- src/cdn/util/basicCrdFileRouter.ts | 117 --------------------------- 16 files changed, 16 insertions(+), 133 deletions(-) delete mode 100644 src/cdn/util/basicCrdFileRouter.ts diff --git a/src/cdn/routes/app-assets.ts b/src/cdn/routes/app-assets.ts index 0764d5201..13d5703b9 100644 --- a/src/cdn/routes/app-assets.ts +++ b/src/cdn/routes/app-assets.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/app-icons.ts b/src/cdn/routes/app-icons.ts index 300d4bdaa..7670d927d 100644 --- a/src/cdn/routes/app-icons.ts +++ b/src/cdn/routes/app-icons.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/attachments.ts b/src/cdn/routes/attachments.ts index ab5028a83..054089c51 100644 --- a/src/cdn/routes/attachments.ts +++ b/src/cdn/routes/attachments.ts @@ -16,7 +16,7 @@ along with this program. If not, see . */ -import { Attachment, Config, hasValidSignature, NewUrlUserSignatureData, Snowflake, UrlSignResult } from "@spacebar/util"; +import { Config, hasValidSignature, NewUrlUserSignatureData, Snowflake, UrlSignResult } from "@spacebar/util"; import { Request, Response, Router } from "express"; import imageSize from "image-size"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/avatars.ts b/src/cdn/routes/avatars.ts index 9fc74bf69..f50303b93 100644 --- a/src/cdn/routes/avatars.ts +++ b/src/cdn/routes/avatars.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/banners.ts b/src/cdn/routes/banners.ts index 89bd8bc06..2fedf41b2 100644 --- a/src/cdn/routes/banners.ts +++ b/src/cdn/routes/banners.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/channel-icons.ts b/src/cdn/routes/channel-icons.ts index 587a09644..234370571 100644 --- a/src/cdn/routes/channel-icons.ts +++ b/src/cdn/routes/channel-icons.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/discover-splashes.ts b/src/cdn/routes/discover-splashes.ts index ee6e0af53..eca15a3ee 100644 --- a/src/cdn/routes/discover-splashes.ts +++ b/src/cdn/routes/discover-splashes.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/discovery-splashes.ts b/src/cdn/routes/discovery-splashes.ts index 32dfb9b22..ba6fee362 100644 --- a/src/cdn/routes/discovery-splashes.ts +++ b/src/cdn/routes/discovery-splashes.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/emojis.ts b/src/cdn/routes/emojis.ts index 074e727d7..2890d3b33 100644 --- a/src/cdn/routes/emojis.ts +++ b/src/cdn/routes/emojis.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/guild-profiles.ts b/src/cdn/routes/guild-profiles.ts index d96a6ddcc..97e3a47a4 100644 --- a/src/cdn/routes/guild-profiles.ts +++ b/src/cdn/routes/guild-profiles.ts @@ -16,7 +16,7 @@ along with this program. If not, see . */ -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import crypto from "crypto"; import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/icons.ts b/src/cdn/routes/icons.ts index 727497f88..a42a4fec5 100644 --- a/src/cdn/routes/icons.ts +++ b/src/cdn/routes/icons.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/role-icons.ts b/src/cdn/routes/role-icons.ts index f169309c8..7539fed69 100644 --- a/src/cdn/routes/role-icons.ts +++ b/src/cdn/routes/role-icons.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; @@ -42,7 +42,7 @@ router.post("/:role_id", multer.single("file"), async (req: Request, res: Respon const { buffer, size } = req.file; const { role_id } = req.params as { [key: string]: string }; - const hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex"); + const hash = crypto.createHash("md5").update(buffer).digest("hex"); const type = await fileTypeFromBuffer(buffer); if (!type || !ALLOWED_MIME_TYPES.includes(type.mime)) throw new HTTPError("Invalid file type"); diff --git a/src/cdn/routes/splashes.ts b/src/cdn/routes/splashes.ts index 44626dc76..85a0b90bf 100644 --- a/src/cdn/routes/splashes.ts +++ b/src/cdn/routes/splashes.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/stickers.ts b/src/cdn/routes/stickers.ts index 2ffa92fc4..67e7625eb 100644 --- a/src/cdn/routes/stickers.ts +++ b/src/cdn/routes/stickers.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/routes/team-icons.ts b/src/cdn/routes/team-icons.ts index de5f02254..ad943a195 100644 --- a/src/cdn/routes/team-icons.ts +++ b/src/cdn/routes/team-icons.ts @@ -17,7 +17,7 @@ */ import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; +import { Config } from "@spacebar/util"; import { storage } from "@spacebar/cdn"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; diff --git a/src/cdn/util/basicCrdFileRouter.ts b/src/cdn/util/basicCrdFileRouter.ts deleted file mode 100644 index 1d2f76b7a..000000000 --- a/src/cdn/util/basicCrdFileRouter.ts +++ /dev/null @@ -1,117 +0,0 @@ -/* - Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2023 Spacebar and Spacebar Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; -import { storage } from "@spacebar/cdn"; -import { fileTypeFromBuffer } from "file-type"; -import { HTTPError } from "lambert-server"; -import crypto from "crypto"; -import { multer } from "../util/multer"; - -// 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]; - -export class BasicCrdFileRouterOptions { - public pathPrefix: string; - public fallbackToAvatarPath: boolean = true; - - constructor(data: Partial = {}) { - Object.assign(this, data); - } -} - -export function createBasicCrdFileRouter(opts: BasicCrdFileRouterOptions) { - const router = Router({ mergeParams: true }); - - 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, size } = req.file; - const { user_id } = req.params as { [key: string]: string }; - - let hash = crypto.createHash("md5").update(buffer).digest("hex"); - - const type = await fileTypeFromBuffer(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 = `${opts.pathPrefix}/${user_id}/${hash}`; - const endpoint = Config.get().cdn.endpointPublic; - - 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 as { [key: string]: string }; - user_id = user_id.split(".")[0]; // remove .file extension - const path = `${opts.pathPrefix}/${user_id}`; - - const file = await storage.get(path); - if (!file) throw new HTTPError("not found", 404); - const type = await fileTypeFromBuffer(file); - - res.set("Content-Type", type?.mime); - res.set("Cache-Control", "public, max-age=31536000"); - - return res.send(file); - }); - - const getAvatar = async (req: Request, res: Response) => { - const { user_id } = req.params as { [key: string]: string }; - let { hash } = req.params as { [key: string]: string }; - hash = hash.split(".")[0]; // remove .file extension - const path = `${opts.pathPrefix}/${user_id}/${hash}`; - - const file = await storage.get(path); - if (!file) throw new HTTPError("not found", 404); - const type = await fileTypeFromBuffer(file); - - res.set("Content-Type", type?.mime); - res.set("Cache-Control", "public, max-age=31536000"); - - return res.send(file); - }; - - router.get("/:user_id/:hash", getAvatar); - - router.delete("/:user_id/:hash", async (req: Request, res: Response) => { - if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); - const { user_id, hash } = req.params as { [key: string]: string }; - const path = `${opts.pathPrefix}/${user_id}/${hash}`; - - await storage.delete(path); - - return res.send({ success: true }); - }); - - return router; -}