summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-02-28 11:38:04 +0100
committerRory& <root@rory.gay>2026-02-28 11:38:04 +0100
commitf65f11720e361042404b36f7bbee3d4ff59ffd13 (patch)
tree3c2409b7af55895f324547b095ec5cdfb9b22957
parentAdd sticker migration (diff)
downloadserver-ts-f65f11720e361042404b36f7bbee3d4ff59ffd13.tar.xz
Rip out CDN migrations
-rw-r--r--src/cdn/routes/app-assets.ts21
-rw-r--r--src/cdn/routes/app-icons.ts21
-rw-r--r--src/cdn/routes/banners.ts21
-rw-r--r--src/cdn/routes/channel-icons.ts21
-rw-r--r--src/cdn/routes/discover-splashes.ts21
-rw-r--r--src/cdn/routes/discovery-splashes.ts21
-rw-r--r--src/cdn/routes/emojis.ts36
-rw-r--r--src/cdn/routes/icons.ts21
-rw-r--r--src/cdn/routes/splashes.ts21
-rw-r--r--src/cdn/routes/stickers.ts74
-rw-r--r--src/cdn/routes/team-icons.ts21
-rw-r--r--src/cdn/util/cache.ts6
12 files changed, 68 insertions, 237 deletions
diff --git a/src/cdn/routes/app-assets.ts b/src/cdn/routes/app-assets.ts

index 4ac51425..572cb3aa 100644 --- a/src/cdn/routes/app-assets.ts +++ b/src/cdn/routes/app-assets.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/app-icons.ts b/src/cdn/routes/app-icons.ts
index c7b1e867..b8ab98b9 100644 --- a/src/cdn/routes/app-icons.ts +++ b/src/cdn/routes/app-icons.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/banners.ts b/src/cdn/routes/banners.ts
index ebbbcc4e..211643f9 100644 --- a/src/cdn/routes/banners.ts +++ b/src/cdn/routes/banners.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/channel-icons.ts b/src/cdn/routes/channel-icons.ts
index 5885b64f..946759dd 100644 --- a/src/cdn/routes/channel-icons.ts +++ b/src/cdn/routes/channel-icons.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/discover-splashes.ts b/src/cdn/routes/discover-splashes.ts
index ea4fbd88..d9822feb 100644 --- a/src/cdn/routes/discover-splashes.ts +++ b/src/cdn/routes/discover-splashes.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/discovery-splashes.ts b/src/cdn/routes/discovery-splashes.ts
index f8715894..c9f8ddf7 100644 --- a/src/cdn/routes/discovery-splashes.ts +++ b/src/cdn/routes/discovery-splashes.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/emojis.ts b/src/cdn/routes/emojis.ts
index bd7d3151..d92cdf90 100644 --- a/src/cdn/routes/emojis.ts +++ b/src/cdn/routes/emojis.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; import { FileStorage } from "@spacebar/cdn"; import fs from "fs/promises"; @@ -69,7 +69,8 @@ router.get("/:emoji_id", cache, async (req: Request, res: Response) => { emoji_id = emoji_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${emoji_id}`; - const file = await getOrMoveFile(path, `avatars/${emoji_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -87,35 +88,4 @@ router.delete("/:emoji_id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (file) { - if (!(await storage.isFile(newPath))) { - console.log(`[CDN] Migrating emoji from subdirectory+fallback to direct path for ${newPath}`); - // noinspection SuspiciousTypeOfGuard -- not sure whats up with this - if (storage instanceof FileStorage) { - const files = await fs.readdir(storage.getFsPath(newPath)); - if (files.length === 1) { - const oldFilePath = storage.getFsPath(`${newPath}/${files[0]}`); - const newFilePath = storage.getFsPath(newPath); - await fs.rename(oldFilePath, newFilePath + ".tmp"); - await fs.rmdir(storage.getFsPath(newPath)); - await fs.rename(newFilePath + ".tmp", newFilePath); - file = await storage.get(newPath); - } else console.log(`[CDN] Warning: not migrating emojis ${newPath}, as there are multiple files in the old directory`); - } else { - console.log("[CDN] Warning: no migration for s3 storage emojis, as it is not a filesystem"); - } - } - } else { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/icons.ts b/src/cdn/routes/icons.ts
index 0106c05c..c98313f4 100644 --- a/src/cdn/routes/icons.ts +++ b/src/cdn/routes/icons.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/splashes.ts b/src/cdn/routes/splashes.ts
index 07a98cec..8a501ec4 100644 --- a/src/cdn/routes/splashes.ts +++ b/src/cdn/routes/splashes.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/stickers.ts b/src/cdn/routes/stickers.ts
index 7ddff335..c2347efb 100644 --- a/src/cdn/routes/stickers.ts +++ b/src/cdn/routes/stickers.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; import { FileStorage } from "@spacebar/cdn*"; import fs from "fs/promises"; @@ -39,11 +39,11 @@ const ALLOWED_MIME_TYPES = [...ANIMATED_MIME_TYPES, ...STATIC_MIME_TYPES]; const router = Router({ mergeParams: true }); const pathPrefix = "stickers"; -router.post("/:guild_id", multer.single("file"), async (req: Request, res: Response) => { +router.post("/:sticker_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 { guild_id } = req.params as { [key: string]: string }; + const { sticker_id } = req.params as { [key: string]: string }; let hash = crypto.createHash("md5").update(Snowflake.generate()).digest("hex"); @@ -51,7 +51,7 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo 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 = `${pathPrefix}/${guild_id}/${hash}`; + const path = `${pathPrefix}/${sticker_id}`; const endpoint = Config.get().cdn.endpointPublic; await storage.set(path, buffer); @@ -60,16 +60,17 @@ router.post("/:guild_id", multer.single("file"), async (req: Request, res: Respo id: hash, content_type: type.mime, size, - url: `${endpoint}${req.baseUrl}/${guild_id}/${hash}`, + url: `${endpoint}${req.baseUrl}/${sticker_id}`, }); }); -router.get("/:guild_id", cache, 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}`; +router.get("/:sticker_id", cache, 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 getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -77,61 +78,14 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { return res.send(file); }); -export const getAvatar = async (req: Request, res: Response) => { - const { guild_id } = req.params as { [key: string]: string }; - let { hash } = req.params as { [key: string]: string }; - hash = hash.split(".")[0]; // remove .file extension - const path = `${pathPrefix}/${guild_id}/${hash}`; - - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); - const type = await fileTypeFromBuffer(file); - - res.set("Content-Type", type?.mime); - - return res.send(file); -}; - -router.get("/:guild_id/:hash", cache, getAvatar); - -router.delete("/:guild_id/:id", async (req: Request, res: Response) => { +router.delete("/:sticker_id/", async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); - const { guild_id, id } = req.params as { [key: string]: string }; - const path = `${pathPrefix}/${guild_id}/${id}`; + const { sticker_id } = req.params as { [key: string]: string }; + const path = `${pathPrefix}/${sticker_id}`; await storage.delete(path); return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (file) { - if (!(await storage.isFile(newPath))) { - console.log(`[CDN] Migrating sticker from subdirectory+fallback to direct path for ${newPath}`); - // noinspection SuspiciousTypeOfGuard -- not sure whats up with this - if (storage instanceof FileStorage) { - const files = await fs.readdir(storage.getFsPath(newPath)); - if (files.length === 1) { - const oldFilePath = storage.getFsPath(`${newPath}/${files[0]}`); - const newFilePath = storage.getFsPath(newPath); - await fs.rename(oldFilePath, newFilePath + ".tmp"); - await fs.rmdir(storage.getFsPath(newPath)); - await fs.rename(newFilePath + ".tmp", newFilePath); - file = await storage.get(newPath); - } else console.log(`[CDN] Warning: not migrating sticker ${newPath}, as there are multiple files in the old directory`); - } else { - console.log("[CDN] Warning: no migration for s3 storage stickers, as it is not a filesystem"); - } - } - } else { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/routes/team-icons.ts b/src/cdn/routes/team-icons.ts
index 3ddbd1ed..103974da 100644 --- a/src/cdn/routes/team-icons.ts +++ b/src/cdn/routes/team-icons.ts
@@ -23,7 +23,7 @@ import { fileTypeFromBuffer } from "file-type"; import { HTTPError } from "lambert-server"; import crypto from "crypto"; import { multer } from "../util/multer"; -import { cache } from "../util/cache"; +import { cache, cacheNotFound } from "../util/cache"; // TODO: check premium and animated pfp are allowed in the config // TODO: generate different sizes of icon @@ -67,7 +67,8 @@ router.get("/:guild_id", cache, async (req: Request, res: Response) => { guild_id = guild_id.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -81,7 +82,8 @@ export const getAvatar = async (req: Request, res: Response) => { hash = hash.split(".")[0]; // remove .file extension const path = `${pathPrefix}/${guild_id}/${hash}`; - const file = await getOrMoveFile(path, `avatars/${guild_id}/${hash}`); + const file = await storage.get(path); + if (!file) return cacheNotFound(req, res); const type = await fileTypeFromBuffer(file); res.set("Content-Type", type?.mime); @@ -101,17 +103,4 @@ router.delete("/:guild_id/:id", async (req: Request, res: Response) => { return res.send({ success: true }); }); -async function getOrMoveFile(newPath: string, oldPath: string) { - let file = await storage.get(newPath); - if (!file) { - if (await storage.exists(oldPath)) { - console.log(`[${pathPrefix}] found file at old path ${oldPath}, moving to new path ${newPath}`); - await storage.move(oldPath, newPath); - file = await storage.get(newPath); - } - } - if (!file) throw new HTTPError("not found", 404); - return file; -} - export default router; diff --git a/src/cdn/util/cache.ts b/src/cdn/util/cache.ts
index a36f2c4c..3f011ad4 100644 --- a/src/cdn/util/cache.ts +++ b/src/cdn/util/cache.ts
@@ -23,3 +23,9 @@ export function cache(req: Request, res: Response, next: NextFunction) { res.setHeader("Cache-Control", `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}, immutable`); next(); } + +export function cacheNotFound(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"); +}