CDN: handle modern avatar decorations routes

This commit is contained in:
Rory&
2026-09-26 20:06:54 +02:00
parent d211b0d3a4
commit 5660cf2b5d
21 changed files with 162 additions and 75 deletions
+5 -5
View File
@@ -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");
+5 -5
View File
@@ -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");
+2 -2
View File
@@ -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;
+30 -5
View File
@@ -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<ArrayBufferLike> | 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;
+3 -3
View File
@@ -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");
+2 -2
View File
@@ -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}`;
+5 -5
View File
@@ -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");
+5 -5
View File
@@ -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");
+5 -5
View File
@@ -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");
+5 -5
View File
@@ -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");
+3 -3
View File
@@ -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);
+3 -3
View File
@@ -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);
+3 -3
View File
@@ -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
+5 -5
View File
@@ -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");
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<ArrayBufferLike> | 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<ArrayBufferLike> | 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;
+3 -3
View File
@@ -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];
+5 -5
View File
@@ -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");
+3 -3
View File
@@ -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);
+5 -5
View File
@@ -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");
+1 -1
View File
@@ -16,6 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export * from "./cache";
export * from "./setCacheControl";
export * from "./multer";
export * from "./Storage";
@@ -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");