diff --git a/packages/web/src/app/api/[chainId]/files/[filename]/download/route.ts b/packages/web/src/app/api/[chainId]/files/[filename]/download/route.ts new file mode 100644 index 0000000..56c550c --- /dev/null +++ b/packages/web/src/app/api/[chainId]/files/[filename]/download/route.ts @@ -0,0 +1,23 @@ +import { getFile } from "../getFile"; + +export async function GET( + req: Request, + { params }: { params: { chainId: string; filename: string } }, +) { + const chainId = parseInt(params.chainId) || 0; + + const file = await getFile(chainId, params.filename); + + // TODO: handle reverts (e.g. FileNotFound) + // TODO: add cache headers + + if (!file) return new Response(null, { status: 404 }); + + return new Response(file.contents, { + headers: { + "Content-Disposition": `attachment; filename=${encodeURIComponent(file.filename)}`, + ...(file.type ? { "Content-Type": file.type } : null), + ...(file.encoding ? { "Content-Encoding": file.encoding } : null), + }, + }); +}