47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
export const runtime = "nodejs";
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
/**
|
|
* Proxies multipart POSTs (file + method) to the bgbye upstream.
|
|
* Set BG_BYE_UPSTREAM to the FULL endpoint that accepts the form:
|
|
* e.g. http://127.0.0.1:8010/process
|
|
* http://localhost:8010/api/removebg
|
|
*/
|
|
export async function POST(req: Request) {
|
|
const upstream = process.env.BG_BYE_UPSTREAM;
|
|
if (!upstream) {
|
|
return NextResponse.json(
|
|
{ error: "BG_BYE_UPSTREAM is not configured on the server" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
|
|
try {
|
|
const form = await req.formData();
|
|
|
|
// Forward the form as-is to the upstream
|
|
const ures = await fetch(upstream, {
|
|
method: "POST",
|
|
body: form,
|
|
// Let undici set boundary; don't set Content-Type manually
|
|
});
|
|
|
|
const contentType = ures.headers.get("content-type") || "application/octet-stream";
|
|
const status = ures.status;
|
|
|
|
// Stream/buffer back to the client preserving content-type
|
|
const ab = await ures.arrayBuffer();
|
|
return new Response(Buffer.from(ab), {
|
|
status,
|
|
headers: {
|
|
"content-type": contentType,
|
|
// Allow the client to see error text if upstream returns text
|
|
"cache-control": "no-store",
|
|
},
|
|
});
|
|
} catch (err: any) {
|
|
const msg = err?.message || String(err);
|
|
return NextResponse.json({ error: `Proxy error: ${msg}` }, { status: 502 });
|
|
}
|
|
}
|