feat: rewire routes to BullMQ and speed up documents page
This commit is contained in:
@@ -1,49 +1,56 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import multer from "multer";
|
||||
import { forwardToPaymentOCRService } from "../services/paymentOCRService";
|
||||
import { ocrQueue } from "../queue/queues";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// keep files in memory; FastAPI accepts them as multipart bytes
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
|
||||
const ALLOWED_MIMES = new Set([
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/tiff",
|
||||
"image/bmp",
|
||||
"image/jpg",
|
||||
]);
|
||||
|
||||
// POST /payment-ocr/extract (field name: "files")
|
||||
router.post(
|
||||
"/extract",
|
||||
upload.array("files"), // allow multiple images
|
||||
upload.array("files"),
|
||||
async (req: Request, res: Response): Promise<any> => {
|
||||
try {
|
||||
const files = req.files as Express.Multer.File[] | undefined;
|
||||
const files = req.files as Express.Multer.File[] | undefined;
|
||||
|
||||
if (!files || files.length === 0) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "No image files uploaded. Use field name 'files'." });
|
||||
}
|
||||
|
||||
// (optional) basic client-side MIME guard
|
||||
const allowed = new Set([
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/tiff",
|
||||
"image/bmp",
|
||||
"image/jpg",
|
||||
]);
|
||||
const bad = files.filter((f) => !allowed.has(f.mimetype.toLowerCase()));
|
||||
if (bad.length) {
|
||||
return res.status(415).json({
|
||||
error: `Unsupported file types: ${bad
|
||||
.map((b) => b.originalname)
|
||||
.join(", ")}`,
|
||||
});
|
||||
}
|
||||
|
||||
const rows = await forwardToPaymentOCRService(files);
|
||||
return res.json({ rows });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: "Payment OCR extraction failed" });
|
||||
if (!files || files.length === 0) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "No image files uploaded. Use field name 'files'." });
|
||||
}
|
||||
|
||||
const bad = files.filter((f) => !ALLOWED_MIMES.has(f.mimetype.toLowerCase()));
|
||||
if (bad.length) {
|
||||
return res.status(415).json({
|
||||
error: `Unsupported file types: ${bad.map((b) => b.originalname).join(", ")}`,
|
||||
});
|
||||
}
|
||||
|
||||
const filesForQueue = files.map((f) => ({
|
||||
originalname: f.originalname,
|
||||
bufferBase64: f.buffer.toString("base64"),
|
||||
mimetype: f.mimetype,
|
||||
}));
|
||||
|
||||
const socketId: string | undefined =
|
||||
(req.body?.socketId as string) ?? undefined;
|
||||
|
||||
const job = await ocrQueue.add("ocr", {
|
||||
userId: (req.user as any)?.id ?? 0,
|
||||
socketId,
|
||||
files: filesForQueue,
|
||||
});
|
||||
|
||||
return res.json({ jobId: job.id, status: "queued" });
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user