"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.customAIApplicationUploadMiddleware = void 0;
exports.cleanupCustomAIUploadFile = cleanupCustomAIUploadFile;
const fs_1 = __importDefault(require("fs"));
const multer_1 = __importDefault(require("multer"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const bundleSecurityLimits_1 = require("../../util/customAIApplication/bundleSecurityLimits");
const storage = multer_1.default.diskStorage({
    destination: (_req, _file, cb) => {
        cb(null, os_1.default.tmpdir());
    },
    filename: (_req, file, cb) => {
        const safeName = path_1.default.basename(file.originalname || 'bundle.zip').replace(/[^\w.-]/g, '_');
        cb(null, `custom-ai-${Date.now()}-${safeName}`);
    },
});
const zipFilter = (_req, file, cb) => {
    const name = (file.originalname || '').toLowerCase();
    if (name.endsWith('.zip') || file.mimetype === 'application/zip' || file.mimetype === 'application/x-zip-compressed') {
        cb(null, true);
        return;
    }
    cb(new Error('Only .zip files are allowed'));
};
exports.customAIApplicationUploadMiddleware = (0, multer_1.default)({
    storage,
    limits: { fileSize: bundleSecurityLimits_1.CUSTOM_AI_BUNDLE_MAX_COMPRESSED_BYTES },
    fileFilter: zipFilter,
}).single('zipFile');
function cleanupCustomAIUploadFile(filePath) {
    if (!filePath) {
        return;
    }
    try {
        if (fs_1.default.existsSync(filePath)) {
            fs_1.default.unlinkSync(filePath);
        }
    }
    catch {
        // best-effort cleanup
    }
}
//# sourceMappingURL=customAIApplicationUpload.js.map