mirror of
https://github.com/spacebarchat/server.git
synced 2026-03-29 14:09:52 +00:00
CDN: use central cache middleware. Closes #1455
This commit is contained in:
@@ -24,6 +24,7 @@ import { multer } from "../util/multer";
|
||||
import { storage } from "../util/Storage";
|
||||
import { CloudAttachment } from "../../util/entities/CloudAttachment";
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import { cache } from "../util/cache";
|
||||
|
||||
const router = Router({ mergeParams: true });
|
||||
|
||||
@@ -69,7 +70,7 @@ router.post("/:channel_id", multer.single("file"), async (req: Request, res: Res
|
||||
return res.json(file);
|
||||
});
|
||||
|
||||
router.get("/:channel_id/:id/:filename", async (req: Request, res: Response) => {
|
||||
router.get("/:channel_id/:id/:filename", cache, async (req: Request, res: Response) => {
|
||||
const { channel_id, id, filename } = req.params;
|
||||
// const { format } = req.query;
|
||||
|
||||
@@ -100,7 +101,6 @@ router.get("/:channel_id/:id/:filename", async (req: Request, res: Response) =>
|
||||
}
|
||||
|
||||
res.set("Content-Type", content_type);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
@@ -23,6 +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";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -60,7 +61,7 @@ router.post("/:user_id", multer.single("file"), async (req: Request, res: Respon
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/:user_id", async (req: Request, res: Response) => {
|
||||
router.get("/:user_id", cache, async (req: Request, res: Response) => {
|
||||
let { user_id } = req.params;
|
||||
user_id = user_id.split(".")[0]; // remove .file extension
|
||||
const path = `avatars/${user_id}`;
|
||||
@@ -70,7 +71,6 @@ router.get("/:user_id", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
@@ -86,12 +86,11 @@ export const getAvatar = async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
};
|
||||
|
||||
router.get("/:user_id/:hash", getAvatar);
|
||||
router.get("/:user_id/:hash", cache, 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");
|
||||
|
||||
@@ -20,10 +20,11 @@ import { Router, Response, Request } from "express";
|
||||
import { storage } from "../util/Storage";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import { cache } from "../util/cache";
|
||||
|
||||
const router = Router({ mergeParams: true });
|
||||
|
||||
router.get("/:badge_id", async (req: Request, res: Response) => {
|
||||
router.get("/:badge_id", cache, async (req: Request, res: Response) => {
|
||||
const { badge_id } = req.params;
|
||||
const path = `badge-icons/${badge_id}`;
|
||||
|
||||
@@ -32,7 +33,6 @@ router.get("/:badge_id", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000, must-revalidate");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
@@ -21,6 +21,7 @@ import fs from "fs/promises";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { join } from "path";
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import { cache } from "../util/cache";
|
||||
|
||||
const defaultAvatarHashMap = new Map([
|
||||
["0", "4a8562cf00887030c416d3ec2d46385a"],
|
||||
@@ -58,7 +59,7 @@ async function getFile(path: string) {
|
||||
}
|
||||
}
|
||||
|
||||
router.get("/avatars/:id", async (req: Request, res: Response) => {
|
||||
router.get("/avatars/:id", cache, async (req: Request, res: Response) => {
|
||||
let { id } = req.params;
|
||||
id = id.split(".")[0]; // remove .file extension
|
||||
const hash = defaultAvatarHashMap.get(id);
|
||||
@@ -70,12 +71,11 @@ router.get("/avatars/:id", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
router.get("/group-avatars/:id", async (req: Request, res: Response) => {
|
||||
router.get("/group-avatars/:id", cache, async (req: Request, res: Response) => {
|
||||
let { id } = req.params;
|
||||
id = id.split(".")[0]; // remove .file extension
|
||||
const hash = defaultGroupDMAvatarHashMap.get(id);
|
||||
@@ -87,7 +87,6 @@ router.get("/group-avatars/:id", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
@@ -23,6 +23,7 @@ import { HTTPError } from "lambert-server";
|
||||
import { multer } from "../util/multer";
|
||||
import { storage } from "../util/Storage";
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import { cache } from "../util/cache";
|
||||
|
||||
// TODO: check premium and animated pfp are allowed in the config
|
||||
// TODO: generate different sizes of icon
|
||||
@@ -60,7 +61,7 @@ router.post("/", multer.single("file"), async (req: Request, res: Response) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
router.get("/", cache, async (req: Request, res: Response) => {
|
||||
const { guild_id } = req.params;
|
||||
let { user_id } = req.params;
|
||||
user_id = user_id.split(".")[0]; // remove .file extension
|
||||
@@ -71,12 +72,11 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
router.get("/:hash", async (req: Request, res: Response) => {
|
||||
router.get("/:hash", cache, async (req: Request, res: Response) => {
|
||||
const { guild_id, user_id } = req.params;
|
||||
let { hash } = req.params;
|
||||
hash = hash.split(".")[0]; // remove .file extension
|
||||
@@ -87,7 +87,6 @@ router.get("/:hash", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
@@ -23,6 +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";
|
||||
|
||||
//Role icons ---> avatars.ts modified
|
||||
|
||||
@@ -59,7 +60,7 @@ router.post("/:role_id", multer.single("file"), async (req: Request, res: Respon
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/:role_id", async (req: Request, res: Response) => {
|
||||
router.get("/:role_id", cache, async (req: Request, res: Response) => {
|
||||
const { role_id } = req.params;
|
||||
//role_id = role_id.split(".")[0]; // remove .file extension
|
||||
const path = `role-icons/${role_id}`;
|
||||
@@ -69,12 +70,11 @@ router.get("/:role_id", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000, must-revalidate");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
router.get("/:role_id/:hash", async (req: Request, res: Response) => {
|
||||
router.get("/:role_id/:hash", cache, async (req: Request, res: Response) => {
|
||||
const { role_id, hash } = req.params;
|
||||
//hash = hash.split(".")[0]; // remove .file extension
|
||||
const requested_extension = hash.split(".")[1];
|
||||
@@ -92,7 +92,6 @@ router.get("/:role_id/:hash", async (req: Request, res: Response) => {
|
||||
const type = await fileTypeFromBuffer(file);
|
||||
|
||||
res.set("Content-Type", type?.mime);
|
||||
res.set("Cache-Control", "public, max-age=31536000, must-revalidate");
|
||||
|
||||
return res.send(file);
|
||||
});
|
||||
|
||||
24
src/cdn/util/cache.ts
Normal file
24
src/cdn/util/cache.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
Copyright (C) 2025 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 { NextFunction, Response, Request } from "express";
|
||||
|
||||
export function cache(req: Request, res: Response, next: NextFunction) {
|
||||
const cacheDuration = 21600; // 6 hours
|
||||
res.setHeader("Cache-Control", `public, max-age=${cacheDuration}, s-maxage=${cacheDuration}, immutable`);
|
||||
}
|
||||
Reference in New Issue
Block a user