Spaces:
Sleeping
Sleeping
fix: fix route
Browse files- frontend/app/api/upload/route.ts +18 -10
frontend/app/api/upload/route.ts
CHANGED
|
@@ -1,28 +1,36 @@
|
|
| 1 |
import { type NextRequest, NextResponse } from "next/server";
|
| 2 |
import { backendUrl } from "@/lib/backend-url";
|
| 3 |
|
| 4 |
-
//
|
| 5 |
-
//
|
| 6 |
-
// the entire body before the 10 MB rewrite middleware limit kicks in.
|
| 7 |
|
| 8 |
export const dynamic = "force-dynamic";
|
|
|
|
| 9 |
|
| 10 |
export async function POST(request: NextRequest) {
|
| 11 |
const backend = backendUrl();
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
let response: Response;
|
| 17 |
try {
|
| 18 |
response = await fetch(`${backend}/upload`, {
|
| 19 |
method: "POST",
|
| 20 |
-
body:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
return NextResponse.json(
|
| 25 |
-
{ detail:
|
| 26 |
{ status: 502 }
|
| 27 |
);
|
| 28 |
}
|
|
|
|
| 1 |
import { type NextRequest, NextResponse } from "next/server";
|
| 2 |
import { backendUrl } from "@/lib/backend-url";
|
| 3 |
|
| 4 |
+
// Stream multipart uploads to FastAPI. Calling request.formData() here buffers
|
| 5 |
+
// the whole PDF in Next.js and can fail before the backend sees the file.
|
|
|
|
| 6 |
|
| 7 |
export const dynamic = "force-dynamic";
|
| 8 |
+
export const runtime = "nodejs";
|
| 9 |
|
| 10 |
export async function POST(request: NextRequest) {
|
| 11 |
const backend = backendUrl();
|
| 12 |
+
const headers = new Headers();
|
| 13 |
+
const requestContentType = request.headers.get("content-type");
|
| 14 |
+
const cookie = request.headers.get("cookie");
|
| 15 |
|
| 16 |
+
if (requestContentType) headers.set("content-type", requestContentType);
|
| 17 |
+
if (cookie) headers.set("cookie", cookie);
|
| 18 |
+
|
| 19 |
+
if (!request.body) {
|
| 20 |
+
return NextResponse.json({ detail: "Upload request did not include a file body." }, { status: 400 });
|
| 21 |
+
}
|
| 22 |
|
| 23 |
let response: Response;
|
| 24 |
try {
|
| 25 |
response = await fetch(`${backend}/upload`, {
|
| 26 |
method: "POST",
|
| 27 |
+
body: request.body,
|
| 28 |
+
duplex: "half",
|
| 29 |
+
headers,
|
| 30 |
+
} as RequestInit & { duplex: "half" });
|
| 31 |
+
} catch (error) {
|
| 32 |
return NextResponse.json(
|
| 33 |
+
{ detail: `Could not reach the backend upload service: ${(error as Error).message}` },
|
| 34 |
{ status: 502 }
|
| 35 |
);
|
| 36 |
}
|