summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-04-19 19:02:15 +0200
committerRory& <root@rory.gay>2026-04-19 22:45:37 +0200
commit3a6c5e5f74f05cd51cc6c5b54b0da8f68ecaba8a (patch)
tree86ff864c5325af433c8d137c8439dcf9b484a2f7
parentMerge pull request #1631 from CyberL1/fix/account-connections-visibility (diff)
downloadserver-ts-3a6c5e5f74f05cd51cc6c5b54b0da8f68ecaba8a.tar.xz
CDN: clean up unused imports
-rw-r--r--src/cdn/routes/app-assets.ts2
-rw-r--r--src/cdn/routes/app-icons.ts2
-rw-r--r--src/cdn/routes/attachments.ts2
-rw-r--r--src/cdn/routes/avatars.ts2
-rw-r--r--src/cdn/routes/banners.ts2
-rw-r--r--src/cdn/routes/channel-icons.ts2
-rw-r--r--src/cdn/routes/discover-splashes.ts2
-rw-r--r--src/cdn/routes/discovery-splashes.ts2
-rw-r--r--src/cdn/routes/emojis.ts2
-rw-r--r--src/cdn/routes/guild-profiles.ts2
-rw-r--r--src/cdn/routes/icons.ts2
-rw-r--r--src/cdn/routes/role-icons.ts4
-rw-r--r--src/cdn/routes/splashes.ts2
-rw-r--r--src/cdn/routes/stickers.ts2
-rw-r--r--src/cdn/routes/team-icons.ts2
-rw-r--r--src/cdn/util/basicCrdFileRouter.ts117
16 files changed, 16 insertions, 133 deletions
diff --git a/src/cdn/routes/app-assets.ts b/src/cdn/routes/app-assets.ts

index 0764d520..13d5703b 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 300d4bda..7670d927 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 ab5028a8..054089c5 100644 --- a/src/cdn/routes/attachments.ts +++ b/src/cdn/routes/attachments.ts
@@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -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 9fc74bf6..f50303b9 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 89bd8bc0..2fedf41b 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 587a0964..23437057 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 ee6e0af5..eca15a3e 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 32dfb9b2..ba6fee36 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 074e727d..2890d3b3 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 d96a6ddc..97e3a47a 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 <https://www.gnu.org/licenses/>. */ -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 727497f8..a42a4fec 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 f169309c..7539fed6 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 44626dc7..85a0b90b 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 2ffa92fc..67e7625e 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 de5f0225..ad943a19 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 1d2f76b7..00000000 --- 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 <https://www.gnu.org/licenses/>. -*/ - -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<BasicCrdFileRouterOptions> = {}) { - 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; -}