bgbye routing fix
This commit is contained in:
parent
3c76ab69e5
commit
c1fbdc843c
4 changed files with 56 additions and 50 deletions
47
app/api/bgbye/process/route.ts
Normal file
47
app/api/bgbye/process/route.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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 });
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
// /app/api/bgremove/route.ts
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const BGBYE_URL =
|
||||
process.env.BGBYE_URL ||
|
||||
process.env.BG_BYE_URL ||
|
||||
process.env.BGREMOVER_BASE_URL || // <-- support your existing env var
|
||||
"http://bgbye:7001";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const inForm = await req.formData();
|
||||
const method = String(inForm.get("method") || "");
|
||||
const file = inForm.get("file") as any;
|
||||
|
||||
// Loosen the guard: some Node/undici builds return a File-like Blob from a different realm
|
||||
if (!file || !method) {
|
||||
return NextResponse.json({ error: "file and method are required" }, { status: 400 });
|
||||
}
|
||||
|
||||
const outForm = new FormData();
|
||||
const filename = (file as any).name || "upload";
|
||||
outForm.set("method", method);
|
||||
outForm.set("file", file, filename);
|
||||
|
||||
const res = await fetch(`${BGBYE_URL}/remove_background/`, { method: "POST", body: outForm });
|
||||
|
||||
const buf = await res.arrayBuffer();
|
||||
return new NextResponse(buf, {
|
||||
status: res.status,
|
||||
headers: {
|
||||
"content-type": res.headers.get("content-type") || "application/octet-stream",
|
||||
"cache-control": "no-store",
|
||||
},
|
||||
});
|
||||
} catch (err: any) {
|
||||
return NextResponse.json({ error: String(err?.message || err) }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue