gaurv007's picture
fix: web/app/api/parse-upload/route.ts
063b349 verified
raw
history blame
2.44 kB
import { NextRequest, NextResponse } from "next/server";
import { createClient } from "@/lib/supabase/server";
export const runtime = "nodejs";
// Add a 5MB size limit
const MAX_FILE_SIZE = 5 * 1024 * 1024;
export async function POST(req: NextRequest) {
try {
const supabase = await createClient();
const { data: { user } } = await supabase.auth.getUser();
if (!user) {
return NextResponse.json({ error: "Unauthorized. Please log in." }, { status: 401 });
}
const formData = await req.formData();
const file = formData.get("file") as File | null;
if (!file) {
return NextResponse.json({ error: "No file uploaded" }, { status: 400 });
}
if (file.size > MAX_FILE_SIZE) {
return NextResponse.json({ error: "File exceeds 5MB size limit" }, { status: 400 });
}
const name = file.name.toLowerCase();
const buffer = Buffer.from(await file.arrayBuffer());
let text = "";
if (name.endsWith(".txt") || name.endsWith(".md")) {
text = new TextDecoder().decode(buffer);
} else if (name.endsWith(".pdf")) {
// FIX v4.3: pdf-parse API compatible with both v1 and v2
// Try the standard pdf-parse import (works with v1.x which is more common)
try {
const pdfParse = (await import("pdf-parse")).default;
const result = await pdfParse(buffer);
text = result.text;
} catch {
// If pdf-parse fails, try sending to Gradio Space for OCR
return NextResponse.json({
error: "PDF parsing failed. Please copy-paste the text directly, or use the Gradio Space which has OCR support."
}, { status: 400 });
}
} else if (name.endsWith(".docx")) {
const mammoth = (await import("mammoth")).default;
const result = await mammoth.extractRawText({ buffer });
text = result.value;
} else {
return NextResponse.json({ error: "Unsupported file type. Use .pdf, .docx, .txt, or .md" }, { status: 400 });
}
if (!text || text.trim().length < 30) {
return NextResponse.json({ error: "Could not extract enough text from this file." }, { status: 400 });
}
return NextResponse.json({ text: text.trim(), filename: file.name, size: file.size });
} catch (error: any) {
console.error("File parse error:", error);
return NextResponse.json({ error: "Failed to parse file: " + (error.message || "Unknown error") }, { status: 500 });
}
}