feat: add OCR recognition feature with page and API route

This commit is contained in:
root
2026-05-27 10:51:57 +08:00
parent ed6907d87e
commit 4794f40fb6
8 changed files with 522 additions and 6 deletions
+46
View File
@@ -0,0 +1,46 @@
import { NextRequest, NextResponse } from "next/server"
const OCR_SERVICE_URL = "http://192.168.10.236:8000/ocr/predict-pdf-file"
export async function POST(request: NextRequest) {
try {
const formData = await request.formData()
const file = formData.get("file")
if (!file || !(file instanceof Blob)) {
return NextResponse.json({ error: "请上传文件" }, { status: 400 })
}
const ocrFormData = new FormData()
const fileName = file instanceof File ? file.name : "file"
ocrFormData.append("file", file, fileName)
const response = await fetch(OCR_SERVICE_URL, {
method: "POST",
body: ocrFormData,
signal: AbortSignal.timeout(60000),
})
if (!response.ok) {
return NextResponse.json(
{ error: `OCR 服务请求失败 (${response.status})` },
{ status: 502 }
)
}
const data = await response.json()
return NextResponse.json(data)
} catch (error) {
console.error("OCR API error:", error)
return NextResponse.json(
{ error: "OCR 识别失败,请稍后重试" },
{ status: 500 }
)
}
}
export const config = {
api: {
bodyParser: false,
},
}