"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.textToAudioUploadMiddleware = 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 text-to-audio files
const storage = multer_1.default.diskStorage({
    destination: (req, file, cb) => {
        const tempDir = path_1.default.resolve('temp_text_to_audio');
        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 CSV files only
const fileFilter = (req, file, cb) => {
    const allowedTypes = ['text/csv', 'application/vnd.ms-excel'];
    const allowedExtensions = ['.csv'];
    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. Only CSV files are allowed.'));
    }
};
// Multer configuration
const upload = (0, multer_1.default)({
    storage,
    fileFilter,
    limits: {
        fileSize: 10 * 1024 * 1024, // 10MB limit for CSV file
        files: 1, // Only 1 file allowed
    },
});
// Custom middleware to handle CSV file upload
const textToAudioUploadMiddleware = (req, res, next) => {
    const uploadHandler = upload.fields([{ name: 'textFile', maxCount: 1 }]);
    uploadHandler(req, res, (err) => {
        if (err) {
            return res.status(400).json({
                success: false,
                error: err.message,
            });
        }
        next();
    });
};
exports.textToAudioUploadMiddleware = textToAudioUploadMiddleware;
//# sourceMappingURL=textToAudioUpload.js.map