UNPKG

@mvp-factory/holy-upload

Version:

File upload processing system extracted from Holy Habit project with security validation and image optimization

226 lines 7.81 kB
"use strict"; /** * Upload Configuration Manager * * Manages upload settings and environment variables * Extracted from Holy Habit upload system */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UploadConfigManager = void 0; const Upload_1 = require("../types/Upload"); const path_1 = __importDefault(require("path")); class UploadConfigManager { /** * Load configuration from environment or defaults * * @param name - Configuration name * @returns Upload configuration */ static load(name = 'default') { if (this.configs.has(name)) { return this.configs.get(name); } const config = this.loadFromEnvironment(name); this.configs.set(name, config); return config; } /** * Set custom configuration * * @param name - Configuration name * @param config - Upload configuration */ static set(name, config) { this.configs.set(name, config); } /** * Get configuration * * @param name - Configuration name * @returns Upload configuration or null */ static get(name) { return this.configs.get(name) || null; } /** * Clear configuration cache */ static clear() { this.configs.clear(); } /** * Load configuration from environment variables * * @param name - Configuration name * @returns Upload configuration */ static loadFromEnvironment(name) { const prefix = name === 'default' ? 'UPLOAD_' : `UPLOAD_${name.toUpperCase()}_`; return { uploadDir: this.getEnvString(`${prefix}DIR`) || Upload_1.DEFAULT_CONFIG.uploadDir, maxSize: this.getEnvNumber(`${prefix}MAX_SIZE`) || Upload_1.DEFAULT_CONFIG.maxSize, maxWidth: this.getEnvNumber(`${prefix}MAX_WIDTH`) || Upload_1.DEFAULT_CONFIG.maxWidth, maxHeight: this.getEnvNumber(`${prefix}MAX_HEIGHT`) || Upload_1.DEFAULT_CONFIG.maxHeight, allowedMimeTypes: this.getEnvArray(`${prefix}ALLOWED_MIME_TYPES`) || Upload_1.DEFAULT_CONFIG.allowedMimeTypes, allowedExtensions: this.getEnvArray(`${prefix}ALLOWED_EXTENSIONS`) || Upload_1.DEFAULT_CONFIG.allowedExtensions, enableOptimization: this.getEnvBoolean(`${prefix}ENABLE_OPTIMIZATION`) ?? Upload_1.DEFAULT_CONFIG.enableOptimization, jpegQuality: this.getEnvNumber(`${prefix}JPEG_QUALITY`) || Upload_1.DEFAULT_CONFIG.jpegQuality, pngCompression: this.getEnvNumber(`${prefix}PNG_COMPRESSION`) || Upload_1.DEFAULT_CONFIG.pngCompression, webpQuality: this.getEnvNumber(`${prefix}WEBP_QUALITY`) || Upload_1.DEFAULT_CONFIG.webpQuality }; } /** * Get string environment variable * * @param key - Environment variable key * @returns String value or undefined */ static getEnvString(key) { return process.env[key]; } /** * Get number environment variable * * @param key - Environment variable key * @returns Number value or undefined */ static getEnvNumber(key) { const value = process.env[key]; if (!value) return undefined; const parsed = parseInt(value, 10); return isNaN(parsed) ? undefined : parsed; } /** * Get boolean environment variable * * @param key - Environment variable key * @returns Boolean value or undefined */ static getEnvBoolean(key) { const value = process.env[key]; if (!value) return undefined; return value.toLowerCase() === 'true' || value === '1'; } /** * Get array environment variable (comma-separated) * * @param key - Environment variable key * @returns Array of strings or undefined */ static getEnvArray(key) { const value = process.env[key]; if (!value) return undefined; return value.split(',').map(item => item.trim()).filter(Boolean); } /** * Validate configuration * * @param config - Upload configuration * @returns Validation result */ static validate(config) { const errors = []; // Validate upload directory if (!config.uploadDir) { errors.push('Upload directory is required'); } // Validate max size if (config.maxSize && config.maxSize <= 0) { errors.push('Max size must be greater than 0'); } // Validate image dimensions if (config.maxWidth && config.maxWidth <= 0) { errors.push('Max width must be greater than 0'); } if (config.maxHeight && config.maxHeight <= 0) { errors.push('Max height must be greater than 0'); } // Validate MIME types if (config.allowedMimeTypes && config.allowedMimeTypes.length === 0) { errors.push('At least one MIME type must be allowed'); } // Validate extensions if (config.allowedExtensions && config.allowedExtensions.length === 0) { errors.push('At least one extension must be allowed'); } // Validate quality settings if (config.jpegQuality && (config.jpegQuality < 1 || config.jpegQuality > 100)) { errors.push('JPEG quality must be between 1 and 100'); } if (config.pngCompression && (config.pngCompression < 0 || config.pngCompression > 9)) { errors.push('PNG compression must be between 0 and 9'); } if (config.webpQuality && (config.webpQuality < 1 || config.webpQuality > 100)) { errors.push('WebP quality must be between 1 and 100'); } return { isValid: errors.length === 0, errors }; } /** * Get absolute upload directory path * * @param config - Upload configuration * @returns Absolute path */ static getAbsoluteUploadDir(config) { if (path_1.default.isAbsolute(config.uploadDir)) { return config.uploadDir; } return path_1.default.resolve(process.cwd(), config.uploadDir); } /** * Merge configurations * * @param base - Base configuration * @param override - Override configuration * @returns Merged configuration */ static merge(base, override) { return { ...base, ...override, // Merge arrays properly allowedMimeTypes: override.allowedMimeTypes || base.allowedMimeTypes, allowedExtensions: override.allowedExtensions || base.allowedExtensions }; } /** * Create development configuration * * @returns Development upload configuration */ static createDevelopmentConfig() { return { ...Upload_1.DEFAULT_CONFIG, uploadDir: './uploads/dev', maxSize: 5 * 1024 * 1024, // 5MB for development enableOptimization: false // Faster uploads in development }; } /** * Create production configuration * * @returns Production upload configuration */ static createProductionConfig() { return { ...Upload_1.DEFAULT_CONFIG, uploadDir: '/tmp/uploads', // Cloud-friendly path maxSize: 10 * 1024 * 1024, // 10MB enableOptimization: true, jpegQuality: 80, // Lower quality for smaller files webpQuality: 80 }; } } exports.UploadConfigManager = UploadConfigManager; UploadConfigManager.configs = new Map(); //# sourceMappingURL=UploadConfig.js.map