48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { createClient } from "@/utils/supabase/server";
|
|
import { NextResponse } from "next/server";
|
|
import { Mistral } from "@mistralai/mistralai";
|
|
|
|
const apiKey = process.env.MISTRAL_API_KEY;
|
|
const client = new Mistral({ apiKey: apiKey });
|
|
|
|
export async function POST(request: Request) {
|
|
const supabase = await createClient();
|
|
const formData = await request.formData();
|
|
const file = formData.get("file") as File;
|
|
const fileName = formData.get("fileName") as string;
|
|
const id = formData.get("id") as string;
|
|
|
|
const uploaded_pdf = await client.files.upload({
|
|
file: {
|
|
fileName,
|
|
content: file,
|
|
},
|
|
purpose: "ocr",
|
|
});
|
|
|
|
const signedUrl = await client.files.getSignedUrl({
|
|
fileId: uploaded_pdf.id,
|
|
});
|
|
|
|
const ocrResponse = await client.ocr.process({
|
|
model: "mistral-ocr-latest",
|
|
document: {
|
|
type: "document_url",
|
|
documentUrl: signedUrl.url,
|
|
},
|
|
});
|
|
|
|
const { data, error } = await supabase
|
|
.from("documents")
|
|
.update({
|
|
ocr_data: ocrResponse,
|
|
})
|
|
.eq("id", id);
|
|
if (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ error: error.message }, { status: 500 });
|
|
}
|
|
console.log("Document updated successfully:", data);
|
|
return NextResponse.json({ message: "File processed successfully" });
|
|
}
|