Introduces core backend and frontend infrastructure for a PDF translation interface. Adds API endpoints for translation, PDF testing, and AI provider testing; implements PDF text extraction, cost tracking, and pricing logic in the lib directory; adds reusable UI components; and provides comprehensive documentation (SDD, environment setup, Claude instructions). Updates Tailwind and global styles, and includes a sample test PDF and configuration files.
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server"
|
|
import { extractTextFromPDF } from "@/lib/pdf-processor"
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const formData = await request.formData()
|
|
const file = formData.get("file") as File
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ error: "No file provided" }, { status: 400 })
|
|
}
|
|
|
|
if (file.type !== "application/pdf") {
|
|
return NextResponse.json({ error: "File must be PDF" }, { status: 400 })
|
|
}
|
|
|
|
console.log(`Testing PDF: ${file.name}, size: ${file.size} bytes`)
|
|
|
|
const arrayBuffer = await file.arrayBuffer()
|
|
const buffer = Buffer.from(arrayBuffer)
|
|
|
|
const result = await extractTextFromPDF(buffer)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
result: {
|
|
text: result.text,
|
|
textLength: result.text.length,
|
|
pageCount: result.pageCount,
|
|
isScanned: result.isScanned,
|
|
metadata: result.metadata
|
|
}
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error("PDF test error:", error)
|
|
return NextResponse.json({
|
|
error: `PDF test failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
details: error instanceof Error ? error.stack : undefined
|
|
}, { status: 500 })
|
|
}
|
|
} |