windsurf/converter/app/api/extract/route.ts
2024-11-20 18:27:41 -05:00

54 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { LlamaParseReader } from 'llamaindex';
import { writeFile } from 'fs/promises';
import { join } from 'path';
import { existsSync, mkdirSync } from 'fs';
const UPLOAD_DIR = join(process.cwd(), 'uploads');
// Ensure uploads directory exists
if (!existsSync(UPLOAD_DIR)) {
mkdirSync(UPLOAD_DIR);
}
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 uploaded' },
{ status: 400 }
);
}
// Save file temporarily
const bytes = await file.arrayBuffer();
const buffer = Buffer.from(bytes);
const filePath = join(UPLOAD_DIR, file.name);
await writeFile(filePath, buffer);
// Extract text using LlamaParser
const reader = new LlamaParseReader({ resultType: 'markdown' });
const documents = await reader.loadData(filePath);
// Combine all document chunks
const fullText = documents.map(doc => doc.text).join('\n\n');
// Clean up the temporary file
await writeFile(filePath, '');
return NextResponse.json({
fileName: file.name,
text: fullText
});
} catch (error) {
console.error('Error processing file:', error);
return NextResponse.json(
{ error: 'Error processing file' },
{ status: 500 }
);
}
}