|
| 1 | +/* |
| 2 | + Spacebar: A FOSS re-implementation and extension of the Discord.com backend. |
| 3 | + Copyright (C) 2023 Spacebar and Spacebar Contributors |
| 4 | + |
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU Affero General Public License as published |
| 7 | + by the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU Affero General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU Affero General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +*/ |
| 18 | + |
| 19 | +import { Router, Response, Request } from "express"; |
| 20 | +import { storage } from "../util/Storage"; |
| 21 | +import FileType from "file-type"; |
| 22 | +import { HTTPError } from "lambert-server"; |
| 23 | + |
| 24 | +const router = Router(); |
| 25 | + |
| 26 | +router.get("/:badge_id", async (req: Request, res: Response) => { |
| 27 | + const { badge_id } = req.params; |
| 28 | + const path = `badge-icons/${badge_id}`; |
| 29 | + |
| 30 | + const file = await storage.get(path); |
| 31 | + if (!file) throw new HTTPError("not found", 404); |
| 32 | + const type = await FileType.fromBuffer(file); |
| 33 | + |
| 34 | + res.set("Content-Type", type?.mime); |
| 35 | + res.set("Cache-Control", "public, max-age=31536000, must-revalidate"); |
| 36 | + |
| 37 | + return res.send(file); |
| 38 | +}); |
| 39 | + |
| 40 | +export default router; |
0 commit comments