const {uploadPhysicalFile} = require("../middlewares");

const handleUploadMiddleware = (req, res, next) => {
    uploadPhysicalFile(req, res, (err) => {
        if (err) {
            console.error('Multer upload error:', err);
            return res.status(500).send({ message: `Upload error: ${err.message}` });
        }
        next();
    });
};

module.exports = app => {
    const upload = require("../controllers/upload.controller.js");
    var router = require("express").Router();
    router.get("/:name", upload.getFile);
    router.post("/upload/:filename", upload.uploadFiles);
    router.post("/upload-physical", handleUploadMiddleware, upload.uploadPhysicalFile);
    router.post("/delete", upload.deleteFile);
    router.post("/delete-physical", upload.deletePhysicalFile);
    router.get("/uploadedDocs/:model/:objectId/type/:fileType/name/:name", upload.getFilesByType);
    app.use('/api/files', router);
 };