From b099e40c6005ae5e578caa38675b2e1370f183e0 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Tue, 28 Nov 2023 14:34:09 -1000 Subject: [PATCH] Enable serving files using local uploads --- server/api/media/[id]/index.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 server/api/media/[id]/index.ts diff --git a/server/api/media/[id]/index.ts b/server/api/media/[id]/index.ts new file mode 100644 index 00000000..4857a0cd --- /dev/null +++ b/server/api/media/[id]/index.ts @@ -0,0 +1,32 @@ +import { errorResponse } from "@response"; +import { applyConfig } from "@api"; +import type { MatchedRoute } from "bun"; + +export const meta = applyConfig({ + allowedMethods: ["GET"], + route: "/media/:id", + ratelimits: { + max: 100, + duration: 60, + }, + auth: { + required: false, + }, +}); + +export default async ( + req: Request, + matchedRoute: MatchedRoute +): Promise => { + // TODO: Add checks for disabled or not email verified accounts + + const id = matchedRoute.params.id; + + // Serve file from filesystem + const file = Bun.file(`./uploads/${id}`); + + if (!(await file.exists())) return errorResponse("File not found", 404); + + // @ts-expect-error Bun allows this + return new Response(file); +};