export class FilePreview { constructor(options = {}) { this.options = { containerId: options.containerId || 'filePreviewModal', onClose: options.onClose || (() => {}), ...options }; this.modal = null; this.init(); } init() { // Create modal if it doesn't exist if (!document.getElementById(this.options.containerId)) { const modalHtml = ` `; document.body.insertAdjacentHTML('beforeend', modalHtml); } this.modal = new bootstrap.Modal(document.getElementById(this.options.containerId)); // Add event listener for modal close document.getElementById(this.options.containerId).addEventListener('hidden.bs.modal', () => { this.options.onClose(); }); } getFileIcon(filename) { const extension = filename.split('.').pop().toLowerCase(); const iconMap = { pdf: 'fa-file-pdf', doc: 'fa-file-word', docx: 'fa-file-word', xls: 'fa-file-excel', xlsx: 'fa-file-excel', ppt: 'fa-file-powerpoint', pptx: 'fa-file-powerpoint', txt: 'fa-file-alt', jpg: 'fa-file-image', jpeg: 'fa-file-image', png: 'fa-file-image', gif: 'fa-file-image', zip: 'fa-file-archive', rar: 'fa-file-archive', mp3: 'fa-file-audio', mp4: 'fa-file-video' }; return iconMap[extension] || 'fa-file'; } async previewFile(file) { const contentDiv = document.getElementById('filePreviewContent'); const extension = file.name.split('.').pop().toLowerCase(); // Show loading spinner contentDiv.innerHTML = `
Loading...
`; try { // Handle different file types if (['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp', 'tiff'].includes(extension)) { // Image preview contentDiv.innerHTML = ` ${file.name} `; } else if (['pdf'].includes(extension)) { // PDF preview contentDiv.innerHTML = ` `; } else if (['mp4', 'webm', 'avi', 'mov', 'wmv', 'flv', 'mkv'].includes(extension)) { // Video preview contentDiv.innerHTML = ` `; } else if (['mp3', 'wav', 'ogg', 'm4a', 'flac'].includes(extension)) { // Audio preview contentDiv.innerHTML = ` `; } else if (['txt', 'md', 'csv', 'py', 'js', 'html', 'css', 'json', 'xml', 'sql', 'sh', 'bat'].includes(extension)) { // Text/Code preview const response = await fetch(file.url); const text = await response.text(); contentDiv.innerHTML = `
${text}
`; } else if (['docx', 'doc', 'xlsx', 'xls', 'pptx', 'ppt', 'odt', 'odp', 'ods'].includes(extension)) { // Office document preview using Office Web Viewer const encodedUrl = encodeURIComponent(file.url); contentDiv.innerHTML = ` `; } else { // Default preview for other file types contentDiv.innerHTML = `
${file.name}

Preview not available for this file type.

Download
`; } } catch (error) { console.error('Error previewing file:', error); contentDiv.innerHTML = `
Error Loading Preview

File could not be viewed. Please try downloading the file instead.

Download
`; } // Show the modal this.modal.show(); } close() { this.modal.hide(); } }