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); +};