senlinyy commited on
Commit
5e00fb2
·
1 Parent(s): 6230227

fix: fix route

Browse files
Files changed (1) hide show
  1. 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
- // This route handler replaces the rewrite proxy for /api/upload so that
5
- // large PDF files (>10 MB) are forwarded to FastAPI without Next.js buffering
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
- const formData = await request.formData();
14
- const cookie = request.headers.get("cookie") ?? "";
 
 
 
 
15
 
16
  let response: Response;
17
  try {
18
  response = await fetch(`${backend}/upload`, {
19
  method: "POST",
20
- body: formData,
21
- headers: { cookie },
22
- });
23
- } catch {
 
24
  return NextResponse.json(
25
- { detail: "Could not reach the backend upload service." },
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
  }