diff --git a/src/cdn/routes/app-assets.ts b/src/cdn/routes/app-assets.ts index ed4bb63e0..252c9d1eb 100644 --- a/src/cdn/routes/app-assets.ts +++ b/src/cdn/routes/app-assets.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/app-icons.ts b/src/cdn/routes/app-icons.ts index 0880c247b..a836dc06a 100644 --- a/src/cdn/routes/app-icons.ts +++ b/src/cdn/routes/app-icons.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/attachments.ts b/src/cdn/routes/attachments.ts index 04f750002..2b40f25d0 100644 --- a/src/cdn/routes/attachments.ts +++ b/src/cdn/routes/attachments.ts @@ -22,7 +22,7 @@ import imageSize from "image-size"; import { HTTPError } from "lambert-server/HTTPError"; import { CloudAttachment } from "@spacebar/database"; import { Config, hasValidSignature, NewUrlUserSignatureData, Snowflake, UrlSignResult } from "@spacebar/util"; -import { storage, multer, cache } from "../util"; +import { storage, multer, setCacheControl } from "../util"; import { InternalCdnAttachment } from "@spacebar/util/dtos/MessageOptions"; const router = Router({ mergeParams: true }); @@ -75,7 +75,7 @@ router.post("/:channel_id/:message_id", multer.single("file"), async (req: Reque return res.json(file); }); -router.get("/:channel_id/:message_id/:filename", cache, async (req: Request, res: Response) => { +router.get("/:channel_id/:message_id/:filename", setCacheControl, async (req: Request, res: Response) => { const { channel_id, message_id, filename } = req.params as { [key: string]: string }; // const { format } = req.query; diff --git a/src/cdn/routes/avatar-decoration-presets.ts b/src/cdn/routes/avatar-decoration-presets.ts index c31b23064..9fa5fc091 100644 --- a/src/cdn/routes/avatar-decoration-presets.ts +++ b/src/cdn/routes/avatar-decoration-presets.ts @@ -1,6 +1,6 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2023 Spacebar and Spacebar Contributors + Copyright (C) 2026 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 @@ -17,18 +17,22 @@ */ import { Router, Response, Request } from "express"; -import { HTTPError } from "lambert-server/HTTPError"; import { fileTypeFromBuffer } from "file-type"; -import { storage, cache } from "../util"; +import { HTTPError } from "lambert-server/HTTPError"; +import { AvatarDecoration } from "@spacebar/database"; +import { storage, setCacheControl } from "../util"; const router = Router({ mergeParams: true }); -router.get("/:avatar_decoration_data_asset", cache, async (req: Request, res: Response) => { +router.get("/:avatar_decoration_data_asset", setCacheControl, async (req: Request, res: Response) => { const { avatar_decoration_data_asset } = req.params as { [key: string]: string }; const path = `avatar-decoration-presets/${avatar_decoration_data_asset}`; const file = await storage.get(path); - if (!file) throw new HTTPError("not found", 404); + if (!file) { + if (!(await tryReturnFromCollectiblesShop(req, res, avatar_decoration_data_asset))) return; + else throw new HTTPError("not found", 404); + } const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -36,4 +40,25 @@ router.get("/:avatar_decoration_data_asset", cache, async (req: Request, res: Re return res.send(file); }); +async function tryReturnFromCollectiblesShop(req: Request, res: Response, avatar_decoration_data_asset: string) { + const coll = await AvatarDecoration.findOne({ where: { asset: avatar_decoration_data_asset } }); + if (!coll) return false; + + const basePath = `collectibles-shop/${coll.id}`; + + let file: Buffer | null; + if (await storage.exists(basePath + "/animated")) { + file = await storage.get(basePath + "/animated"); + } else if (await storage.exists(basePath + "/static")) { + file = await storage.get(basePath + "/static"); + } else return false; + + const type = await fileTypeFromBuffer(file!); + + res.set("Content-Type", type?.mime); + + res.send(file); + return true; +} + export default router; diff --git a/src/cdn/routes/avatars.ts b/src/cdn/routes/avatars.ts index 80580cd0e..050368386 100644 --- a/src/cdn/routes/avatars.ts +++ b/src/cdn/routes/avatars.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { Config } from "@spacebar/util"; import { HTTPError } from "lambert-server/HTTPError"; -import { storage, multer, cache } from "../util"; +import { storage, multer, setCacheControl } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -59,7 +59,7 @@ router.post("/:user_id", multer.single("file"), async (req: Request, res: Respon }); }); -router.get("/:user_id", cache, async (req: Request, res: Response) => { +router.get("/:user_id", setCacheControl, 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 = `avatars/${user_id}`; @@ -88,7 +88,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:user_id/:hash", cache, getAvatar); +router.get("/:user_id/:hash", setCacheControl, getAvatar); router.delete("/:user_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/badge-icons.ts b/src/cdn/routes/badge-icons.ts index 9a33d720a..336dd2124 100644 --- a/src/cdn/routes/badge-icons.ts +++ b/src/cdn/routes/badge-icons.ts @@ -19,11 +19,11 @@ import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server/HTTPError"; import { fileTypeFromBuffer } from "file-type"; -import { storage, cache } from "../util"; +import { storage, setCacheControl } from "../util"; const router = Router({ mergeParams: true }); -router.get("/:badge_id", cache, async (req: Request, res: Response) => { +router.get("/:badge_id", setCacheControl, async (req: Request, res: Response) => { const { badge_id } = req.params as { [key: string]: string }; const path = `badge-icons/${badge_id}`; diff --git a/src/cdn/routes/banners.ts b/src/cdn/routes/banners.ts index 254b7bbb6..569f65377 100644 --- a/src/cdn/routes/banners.ts +++ b/src/cdn/routes/banners.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/channel-icons.ts b/src/cdn/routes/channel-icons.ts index eb69ca146..2581097a0 100644 --- a/src/cdn/routes/channel-icons.ts +++ b/src/cdn/routes/channel-icons.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/discover-splashes.ts b/src/cdn/routes/discover-splashes.ts index 5f5c54244..49f2645f2 100644 --- a/src/cdn/routes/discover-splashes.ts +++ b/src/cdn/routes/discover-splashes.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/discovery-splashes.ts b/src/cdn/routes/discovery-splashes.ts index 07ec9a0f8..c81e92af9 100644 --- a/src/cdn/routes/discovery-splashes.ts +++ b/src/cdn/routes/discovery-splashes.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index 5ed6505a1..e62a4022c 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -21,7 +21,7 @@ import { join } from "node:path"; import { Request, Response, Router } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; -import { cache } from "../util"; +import { setCacheControl } from "../util"; const defaultAvatarHashMap = new Map([ ["0", "4a8562cf00887030c416d3ec2d46385a"], @@ -59,7 +59,7 @@ async function getFile(path: string) { } } -router.get("/avatars/:id", cache, async (req: Request, res: Response) => { +router.get("/avatars/:id", setCacheControl, async (req: Request, res: Response) => { let { id } = req.params as { [key: string]: string }; id = id.split(".")[0]; // remove .file extension const hash = defaultAvatarHashMap.get(id); @@ -75,7 +75,7 @@ router.get("/avatars/:id", cache, async (req: Request, res: Response) => { return res.send(file); }); -router.get("/group-avatars/:id", cache, async (req: Request, res: Response) => { +router.get("/group-avatars/:id", setCacheControl, async (req: Request, res: Response) => { let { id } = req.params as { [key: string]: string }; id = id.split(".")[0]; // remove .file extension const hash = defaultGroupDMAvatarHashMap.get(id); diff --git a/src/cdn/routes/emojis.ts b/src/cdn/routes/emojis.ts index d2fe786ae..bd7bdb4c5 100644 --- a/src/cdn/routes/emojis.ts +++ b/src/cdn/routes/emojis.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:emoji_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:emoji_id", cache, async (req: Request, res: Response) => { +router.get("/:emoji_id", setCacheControl, async (req: Request, res: Response) => { let { emoji_id } = req.params as { [key: string]: string }; emoji_id = emoji_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${emoji_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); diff --git a/src/cdn/routes/guild-profiles.ts b/src/cdn/routes/guild-profiles.ts index 0a8690f30..51e36f860 100644 --- a/src/cdn/routes/guild-profiles.ts +++ b/src/cdn/routes/guild-profiles.ts @@ -21,7 +21,7 @@ import { Request, Response, Router } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache } from "../util"; +import { storage, multer, setCacheControl } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -59,7 +59,7 @@ router.post("/", multer.single("file"), async (req: Request, res: Response) => { }); }); -router.get("/", cache, async (req: Request, res: Response) => { +router.get("/", setCacheControl, async (req: Request, res: Response) => { const { guild_id } = req.params as { [key: string]: string }; let { user_id } = req.params as { [key: string]: string }; user_id = user_id.split(".")[0]; // remove .file extension @@ -74,7 +74,7 @@ router.get("/", cache, async (req: Request, res: Response) => { return res.send(file); }); -router.get("/:hash", cache, async (req: Request, res: Response) => { +router.get("/:hash", setCacheControl, async (req: Request, res: Response) => { const { guild_id, user_id } = req.params as { [key: string]: string }; let { hash } = req.params as { [key: string]: string }; hash = hash.split(".")[0]; // remove .file extension diff --git a/src/cdn/routes/icons.ts b/src/cdn/routes/icons.ts index 098b125a9..01d4da800 100644 --- a/src/cdn/routes/icons.ts +++ b/src/cdn/routes/icons.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/media/v1/collectibles-shop.ts b/src/cdn/routes/media/v1/collectibles-shop.ts new file mode 100644 index 000000000..60c27554c --- /dev/null +++ b/src/cdn/routes/media/v1/collectibles-shop.ts @@ -0,0 +1,62 @@ +/* + Spacebar: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2026 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 { HTTPError } from "lambert-server/HTTPError"; +import { fileTypeFromBuffer } from "file-type"; +import { storage, setCacheControl } from "../../../util"; + +const router = Router({ mergeParams: true }); + +router.get("/:sku_id/static", setCacheControl, async (req: Request, res: Response) => { + const { sku_id } = req.params as { [key: string]: string }; + const basePath = `collectibles-shop/${sku_id}`; + + let file: Buffer | null; + if (await storage.exists(basePath + "/static")) { + file = await storage.get(basePath + "/static"); + } else if (await storage.exists(basePath + "/animated")) { + file = await storage.get(basePath + "/animated"); + } else throw new HTTPError("not found", 404); + + const type = await fileTypeFromBuffer(file!); + + res.set("Content-Type", type?.mime); + + return res.send(file); +}); + +router.get("/:sku_id/animated", setCacheControl, async (req: Request, res: Response) => { + const { sku_id } = req.params as { [key: string]: string }; + const basePath = `collectibles-shop/${sku_id}`; + + let file: Buffer | null; + if (await storage.exists(basePath + "/animated")) { + file = await storage.get(basePath + "/animated"); + } else if (await storage.exists(basePath + "/static")) { + file = await storage.get(basePath + "/static"); + } else throw new HTTPError("not found", 404); + + const type = await fileTypeFromBuffer(file!); + + res.set("Content-Type", type?.mime); + + return res.send(file); +}); + +export default router; diff --git a/src/cdn/routes/role-icons.ts b/src/cdn/routes/role-icons.ts index 1524c32d4..c0902f7f3 100644 --- a/src/cdn/routes/role-icons.ts +++ b/src/cdn/routes/role-icons.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache } from "../util"; +import { storage, multer, setCacheControl } from "../util"; //Role icons ---> avatars.ts modified @@ -58,7 +58,7 @@ router.post("/:role_id", multer.single("file"), async (req: Request, res: Respon }); }); -router.get("/:role_id", cache, async (req: Request, res: Response) => { +router.get("/:role_id", setCacheControl, async (req: Request, res: Response) => { const { role_id } = req.params as { [key: string]: string }; //role_id = role_id.split(".")[0]; // remove .file extension const path = `role-icons/${role_id}`; @@ -72,7 +72,7 @@ router.get("/:role_id", cache, async (req: Request, res: Response) => { return res.send(file); }); -router.get("/:role_id/:hash", cache, async (req: Request, res: Response) => { +router.get("/:role_id/:hash", setCacheControl, async (req: Request, res: Response) => { const { role_id, hash } = req.params as { [key: string]: string }; //hash = hash.split(".")[0]; // remove .file extension const requested_extension = hash.split(".")[1]; diff --git a/src/cdn/routes/splashes.ts b/src/cdn/routes/splashes.ts index 2222af0f4..bf008a2ee 100644 --- a/src/cdn/routes/splashes.ts +++ b/src/cdn/routes/splashes.ts @@ -21,7 +21,7 @@ import crypto from "node:crypto"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/routes/stickers.ts b/src/cdn/routes/stickers.ts index ba9984a29..5ef6364a1 100644 --- a/src/cdn/routes/stickers.ts +++ b/src/cdn/routes/stickers.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:sticker_id", multer.single("file"), async (req: Request, res: Res }); }); -router.get("/:sticker_id", cache, async (req: Request, res: Response) => { +router.get("/:sticker_id", setCacheControl, async (req: Request, res: Response) => { let { sticker_id } = req.params as { [key: string]: string }; sticker_id = sticker_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${sticker_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); diff --git a/src/cdn/routes/team-icons.ts b/src/cdn/routes/team-icons.ts index 4f92d0e43..cec18136d 100644 --- a/src/cdn/routes/team-icons.ts +++ b/src/cdn/routes/team-icons.ts @@ -21,7 +21,7 @@ import { Router, Response, Request } from "express"; import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server/HTTPError"; import { Config } from "@spacebar/util"; -import { storage, multer, cache, cacheNotFound } from "../util"; +import { storage, multer, setCacheControl, setCacheControlNotFound } from "../util"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -60,13 +60,13 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo }); }); -router.get("/:guild_id", cache, async (req: Request, res: Response) => { +router.get("/:guild_id", setCacheControl, async (req: Request, res: Response) => { let { guild_id } = req.params as { [key: string]: string }; guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +81,7 @@ export const getAvatar = async (req: Request, res: Response) => { const path = `${pathPrefix}/${guild_id}/${hash}`; const file = await storage.get(path); - if (!file) return cacheNotFound(req, res); + if (!file) return setCacheControlNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -89,7 +89,7 @@ export const getAvatar = async (req: Request, res: Response) => { return res.send(file); }; -router.get("/:guild_id/:hash", cache, getAvatar); +router.get("/:guild_id/:hash", setCacheControl, getAvatar); router.delete("/:guild_id/:id", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); diff --git a/src/cdn/util/index.ts b/src/cdn/util/index.ts index 7d4e2758b..73b8f73b9 100644 --- a/src/cdn/util/index.ts +++ b/src/cdn/util/index.ts @@ -16,6 +16,6 @@ along with this program. If not, see . */ -export * from "./cache"; +export * from "./setCacheControl"; export * from "./multer"; export * from "./Storage"; diff --git a/src/cdn/util/cache.ts b/src/cdn/util/setCacheControl.ts similarity index 88% rename from src/cdn/util/cache.ts rename to src/cdn/util/setCacheControl.ts index 3f011ad4b..d41295ebf 100644 --- a/src/cdn/util/cache.ts +++ b/src/cdn/util/setCacheControl.ts @@ -18,13 +18,13 @@ import { NextFunction, Response, Request } from "express"; -export function cache(req: Request, res: Response, next: NextFunction) { +export function setCacheControl(req: Request, res: Response, next: NextFunction) { const cacheDuration = 21600; // 6 hours res.setHeader("Cache-Control", `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}, immutable`); next(); } -export function cacheNotFound(req: Request, res: Response) { +export function setCacheControlNotFound(req: Request, res: Response) { const cacheDuration = 60; // 1 minute res.setHeader("Cache-Control", `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}, immutable`); res.status(404).send(req.path + " not found");