diff --git a/static/js/rooms/fileManager.js b/static/js/rooms/fileManager.js index 3aedd8a..63d71a3 100644 --- a/static/js/rooms/fileManager.js +++ b/static/js/rooms/fileManager.js @@ -210,11 +210,41 @@ export class FileManager { } } - downloadFile(fileId) { - console.log('[FileManager] Downloading file:', fileId); - const url = `/api/rooms/${this.roomManager.roomId}/files/${fileId}/download`; + async downloadFile(filename, path = '') { + console.log('[FileManager] Downloading file:', { filename, path }); + const url = `/api/rooms/${this.roomManager.roomId}/files/${encodeURIComponent(filename)}`; + if (path) { + url += `?path=${encodeURIComponent(path)}`; + } console.log('[FileManager] Download URL:', url); - window.location.href = url; + + try { + const response = await fetch(url, { + method: 'GET', + headers: { + 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content') + } + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const blob = await response.blob(); + const downloadUrl = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = downloadUrl; + a.download = filename; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(downloadUrl); + document.body.removeChild(a); + console.log('[FileManager] Download initiated'); + } catch (error) { + console.error('[FileManager] Error downloading file:', error); + document.getElementById('fileError').textContent = 'Failed to download file. Please try again.'; + throw error; + } } async downloadSelected() { @@ -238,13 +268,13 @@ export class FileManager { } try { - const response = await fetch(`/api/rooms/${this.roomManager.roomId}/files/download`, { + const response = await fetch(`/api/rooms/${this.roomManager.roomId}/download-zip`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }, - body: JSON.stringify({ file_ids: fileIds }) + body: JSON.stringify({ items: selectedItems }) }); console.log('[FileManager] Download response status:', response.status); diff --git a/static/js/rooms/modalManager.js b/static/js/rooms/modalManager.js index 428a640..b5713aa 100644 --- a/static/js/rooms/modalManager.js +++ b/static/js/rooms/modalManager.js @@ -87,6 +87,7 @@ export class ModalManager { console.log('[ModalManager] Found file:', file); return { + id: file.id, name: file.name, path: file.path || '', type: file.type diff --git a/static/js/rooms/viewManager.js b/static/js/rooms/viewManager.js index ae41aae..a00e7d3 100644 --- a/static/js/rooms/viewManager.js +++ b/static/js/rooms/viewManager.js @@ -247,7 +247,7 @@ export class ViewManager { } else { if (this.roomManager.canDownload) { actions.push(` -