"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ragPoisoningUploadMiddleware = exports.uploadZipFile = exports.uploadMultipleFiles = exports.uploadSingleFile = void 0;
const fs_1 = __importDefault(require("fs"));
const multer_1 = __importDefault(require("multer"));
const path_1 = __importDefault(require("path"));
const uuid_1 = require("uuid");
// Configure multer storage for RAG poisoning files
const storage = multer_1.default.diskStorage({
    destination: (req, file, cb) => {
        const tempDir = path_1.default.resolve('temp_rag_poison');
        if (!fs_1.default.existsSync(tempDir)) {
            fs_1.default.mkdirSync(tempDir, { recursive: true });
        }
        cb(null, tempDir);
    },
    filename: (req, file, cb) => {
        const uniqueName = `${(0, uuid_1.v4)()}_${file.originalname}`;
        cb(null, uniqueName);
    },
});
// File filter for supported document types
const fileFilter = (req, file, cb) => {
    const allowedTypes = [
        'text/plain',
        'application/pdf',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'application/zip',
    ];
    const allowedExtensions = ['.txt', '.pdf', '.docx', '.zip'];
    const fileExtension = path_1.default.extname(file.originalname).toLowerCase();
    if (allowedTypes.includes(file.mimetype) || allowedExtensions.includes(fileExtension)) {
        cb(null, true);
    }
    else {
        cb(new Error(`Unsupported file type. Allowed types: ${allowedExtensions.join(', ')}`));
    }
};
// Multer configuration
const upload = (0, multer_1.default)({
    storage,
    fileFilter,
    limits: {
        fileSize: 50 * 1024 * 1024, // 50MB limit per file
        files: 10, // Maximum 10 files for multiple uploads
    },
});
// Middleware for single file upload
exports.uploadSingleFile = upload.single('file');
// Middleware for multiple files upload
exports.uploadMultipleFiles = upload.array('files', 10);
// Middleware for ZIP file upload
exports.uploadZipFile = upload.single('zipFile');
// Custom middleware to handle different upload types
const ragPoisoningUploadMiddleware = (req, res, next) => {
    // Use a more flexible approach - try to detect upload type based on available fields
    // We'll use a single multer instance that can handle all cases
    const upload = (0, multer_1.default)({
        storage,
        fileFilter,
        limits: {
            fileSize: 50 * 1024 * 1024, // 50MB limit per file
            files: 10, // Maximum 10 files for multiple uploads
        },
    });
    // Use fields to handle different upload types
    upload.fields([
        { name: 'file', maxCount: 1 }, // For single file uploads
        { name: 'files', maxCount: 10 }, // For multiple file uploads
        { name: 'zipFile', maxCount: 1 }, // For ZIP file uploads
    ])(req, res, (err) => {
        if (err) {
            return res.status(400).json({
                success: false,
                error: err.message,
            });
        }
        next();
    });
};
exports.ragPoisoningUploadMiddleware = ragPoisoningUploadMiddleware;
//# sourceMappingURL=ragPoisoningUpload.js.map