/** * File Upload Handler for Legal Dashboard * Manages document uploads, OCR processing, and real-time progress */ class FileUploadHandler { constructor() { this.uploadEndpoint = '/api/ocr/upload'; this.processEndpoint = '/api/ocr/process'; this.maxFileSize = 10 * 1024 * 1024; // 10MB this.allowedTypes = ['application/pdf', 'image/jpeg', 'image/png', 'image/tiff']; this.currentUpload = null; this.uploadQueue = []; this.initializeEventListeners(); } initializeEventListeners() { // File input change const fileInput = document.getElementById('documentUpload'); if (fileInput) { fileInput.addEventListener('change', (e) => this.handleFileSelection(e)); } // Upload button const uploadBtn = document.getElementById('uploadButton'); if (uploadBtn) { uploadBtn.addEventListener('click', () => this.startUpload()); } // Drag and drop const dropZone = document.getElementById('uploadDropZone'); if (dropZone) { dropZone.addEventListener('dragover', (e) => this.handleDragOver(e)); dropZone.addEventListener('drop', (e) => this.handleDrop(e)); dropZone.addEventListener('dragleave', (e) => this.handleDragLeave(e)); } } handleFileSelection(event) { const files = event.target.files; if (files.length > 0) { this.validateAndQueueFiles(files); } } handleDragOver(event) { event.preventDefault(); event.currentTarget.classList.add('drag-over'); } handleDragLeave(event) { event.preventDefault(); event.currentTarget.classList.remove('drag-over'); } handleDrop(event) { event.preventDefault(); event.currentTarget.classList.remove('drag-over'); const files = event.dataTransfer.files; if (files.length > 0) { this.validateAndQueueFiles(files); } } validateAndQueueFiles(files) { const validFiles = []; const errors = []; for (let file of files) { // Check file size if (file.size > this.maxFileSize) { errors.push(`${file.name}: File too large (max 10MB)`); continue; } // Check file type if (!this.allowedTypes.includes(file.type)) { errors.push(`${file.name}: Unsupported file type`); continue; } validFiles.push(file); } // Show errors if any if (errors.length > 0) { this.showErrors(errors); } // Queue valid files if (validFiles.length > 0) { this.uploadQueue.push(...validFiles); this.updateUploadQueue(); } } updateUploadQueue() { const queueContainer = document.getElementById('uploadQueue'); if (!queueContainer) return; if (this.uploadQueue.length === 0) { queueContainer.innerHTML = '
هیچ فایلی برای آپلود انتخاب نشده
'; return; } const queueHTML = this.uploadQueue.map((file, index) => `کیفیت: ${(result.quality || 0).toFixed(2)}%
متن استخراج شده: