|
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.memoryStorage(); |
|
|
|
const fileFilter = function (req, file, cb) { |
|
const allowedExt = /\.(pdf|csv|ppt|pptx|doc|docx|xls|xlsx|txt)$/i; |
|
|
|
|
|
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", |
|
]; |
|
|
|
|
|
const extname = allowedExt.test(path.extname(file.originalname)); |
|
|
|
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; |
|
|