// 文件详情页面功能 class FileDetail { constructor() { this.filePath = this.getFilePathFromURL(); this.init(); } // 从 URL 获取文件路径 getFilePathFromURL() { const pathParts = window.location.pathname.split('/'); const encodedPath = pathParts.slice(2).join('/'); // 去掉 /file/ 前缀 // 解码路径 return decodeURIComponent(encodedPath); } init() { this.loadFileDetail(); this.bindEvents(); } // 绑定事件 bindEvents() { document.getElementById('expand-all').addEventListener('click', () => { this.expandAllChunks(); }); document.getElementById('collapse-all').addEventListener('click', () => { this.collapseAllChunks(); }); document.getElementById('back-btn').addEventListener('click', (e) => { e.preventDefault(); this.goBack(); }); document.getElementById('download-btn').addEventListener('click', (e) => { e.preventDefault(); this.downloadFile(); }); document.getElementById('delete-btn').addEventListener('click', (e) => { e.preventDefault(); this.showDeleteConfirmDialog(); }); } // 返回文件所在目录 goBack() { // 计算父目录路径 const pathParts = this.filePath.split('/'); pathParts.pop(); // 移除文件名 const parentPath = pathParts.join('/'); // 构建返回 URL if (parentPath) { window.location.href = `/files?path=${encodeURIComponent(parentPath)}`; } else { window.location.href = '/files'; } } // 下载文件 downloadFile() { const downloadBtn = document.getElementById('download-btn'); const originalText = downloadBtn.textContent; // 显示下载状态 downloadBtn.textContent = '下载中...'; downloadBtn.classList.add('downloading'); // 构建下载URL const downloadUrl = `/api/file/${this.filePath.split('/').map(part => encodeURIComponent(part)).join('/')}/download`; // 创建隐藏的下载链接 const link = document.createElement('a'); link.href = downloadUrl; link.style.display = 'none'; document.body.appendChild(link); // 触发下载 link.click(); // 清理 document.body.removeChild(link); // 恢复按钮状态 setTimeout(() => { downloadBtn.textContent = originalText; downloadBtn.classList.remove('downloading'); }, 1000); } // 显示删除确认对话框 showDeleteConfirmDialog() { const fileName = decodeURIComponent(this.filePath.split('/').pop()); const modalHtml = `
`; document.body.insertAdjacentHTML('beforeend', modalHtml); } // 关闭删除确认对话框 closeDeleteModal(event) { if (!event || event.target.classList.contains('modal-overlay') || event.target.classList.contains('modal-close')) { const modal = document.getElementById('delete-confirm-modal'); if (modal) { modal.remove(); } } } // 确认删除文件 confirmDeleteFile() { const confirmBtn = document.getElementById('confirm-delete-btn'); const originalText = confirmBtn.textContent; // 显示删除中状态 confirmBtn.textContent = '删除中...'; confirmBtn.disabled = true; confirmBtn.classList.add('loading'); // 构建删除URL const encodedPath = this.filePath.split('/').map(part => encodeURIComponent(part)).join('/'); const deleteUrl = `/api/file/${encodedPath}/delete`; Utils.ajax(deleteUrl, 'DELETE') .then(data => { // 删除成功 this.closeDeleteModal(); this.showDeleteSuccessMessage(data.message); // 2秒后返回文件列表 setTimeout(() => { this.goBack(); }, 2000); }) .catch(error => { // 删除失败 this.showDeleteErrorMessage(error.message); // 恢复按钮状态 confirmBtn.textContent = originalText; confirmBtn.disabled = false; confirmBtn.classList.remove('loading'); }); } // 显示删除成功消息 showDeleteSuccessMessage(message) { const messageHtml = ` `; const fileInfoElement = document.getElementById('file-info'); fileInfoElement.insertAdjacentHTML('beforebegin', messageHtml); // 3秒后自动移除消息 setTimeout(() => { const msg = document.getElementById('delete-success-message'); if (msg) msg.remove(); }, 3000); } // 显示删除错误消息 showDeleteErrorMessage(message) { const messageHtml = ` `; const fileInfoElement = document.getElementById('file-info'); fileInfoElement.insertAdjacentHTML('beforebegin', messageHtml); } // 加载文件详情 loadFileDetail() { const fileInfoElement = document.getElementById('file-info'); Utils.showLoading(fileInfoElement); // 更新页面标题 document.getElementById('file-title').textContent = `文件详情 - ${decodeURIComponent(this.filePath.split('/').pop())}`; const url = `/api/file/${this.filePath.split('/').map(part => encodeURIComponent(part)).join('/')}`; Utils.ajax(url) .then(data => { this.renderFileInfo(data); if (data.chunks && data.chunks.length > 0) { this.renderChunks(data.chunks); } }) .catch(error => { Utils.showError(fileInfoElement, error.message); }); } // 渲染文件信息 renderFileInfo(fileInfo) { const fileInfoElement = document.getElementById('file-info'); const statusBadge = fileInfo.indexed ? '已索引' : '未索引'; let html = `