VocRT / backend /routes /rag.routes.js
Anurag
version-2 initial version
5306da4
const express = require("express");
const {
uploadPDF,
uploadLink,
clearContext,
uploadText,
} = require("../controller/file");
const multer = require("multer");
const path = require("path");
const ragRouter = express.Router();
// const storage = multer.diskStorage({
// destination: function (req, file, cb) {
// cb(null, "uploads/");
// },
// filename: function (req, file, cb) {
// cb(null, Date.now() + "-" + file.originalname);
// },
// });
const storage = multer.memoryStorage();
const fileFilter = function (req, file, cb) {
const allowedExt = /\.(pdf|csv|ppt|pptx|doc|docx|xls|xlsx|txt)$/i;
// Allowed MIME types
const allowedMime = [
"application/pdf",
"text/csv",
"application/vnd.ms-powerpoint",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"text/plain",
];
// Check extension
const extname = allowedExt.test(path.extname(file.originalname));
// Check mime
const mimetype = allowedMime.includes(file.mimetype);
if (extname && mimetype) {
cb(null, true);
} else {
cb(
new Error(
"Invalid file type. Only document files are allowed: PDF, CSV, PPT(X), DOC(X), XLS(X), TXT."
),
false
);
}
};
const upload = multer({
storage: storage,
fileFilter: fileFilter,
});
ragRouter.post("/pdf", upload.single("pdfFile"), uploadPDF);
ragRouter.post("/link", uploadLink);
ragRouter.post("/text", uploadText);
ragRouter.post("/clear-context", clearContext);
module.exports = ragRouter;