feat: implement file retrieval API endpoint

This commit is contained in:
phattaranun.a committed 2026-05-25 01:21:11 +07:00
1 parent ae4c665b28
commit ee05890661
2 files changed
+35 -4

No files matched your search

-4
View File
@@ -10,10 +10,6 @@ const nextConfig = {
},
async rewrites() {
return [
{
source: "/files/:path*",
destination: `${BACKEND_INTERNAL_URL}/files/:path*`
},
{
source: "/api/:path*",
destination: `${BACKEND_INTERNAL_URL}/api/:path*`
+35
View File
@@ -0,0 +1,35 @@
import { NextRequest, NextResponse } from "next/server";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
const BACKEND =
process.env.BACKEND_INTERNAL_URL || "http://localhost:20001";
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ path: string[] }> }
) {
const { path } = await params;
const url = `${BACKEND}/files/${path.join("/")}`;
let res: Response;
try {
res = await fetch(url, { cache: "no-store" });
} catch {
return new NextResponse("Bad Gateway", { status: 502 });
}
if (!res.ok) {
return new NextResponse(null, { status: res.status });
}
const body = await res.arrayBuffer();
return new NextResponse(body, {
headers: {
"Content-Type":
res.headers.get("Content-Type") ?? "application/octet-stream",
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}