import React, { useState } from 'react'; import { useFileDownload } from '../hooks/useFileDownload'; import { useTusUpload } from '../hooks/useTusUpload'; export function AdvancedFileDownload() { const { getFileInfo } = useTusUpload(); const { downloadProgress, isDownloading, downloadError, downloadFile, downloadFileWithProgress, previewFile, copyFileLink, canPreview, getFileIcon, } = useFileDownload(); const [fileId, setFileId] = useState(''); const [fileInfo, setFileInfo] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // 获取文件信息 const handleGetFileInfo = async () => { if (!fileId.trim()) { setError('请输入文件ID'); return; } setLoading(true); setError(null); try { const info = await getFileInfo(fileId); if (info) { setFileInfo(info); } else { setError('文件不存在或未准备好'); } } catch (err) { setError('获取文件信息失败'); } finally { setLoading(false); } }; // 简单下载 const handleSimpleDownload = () => { downloadFile(fileId, fileInfo?.title); }; // 带进度的下载 const handleProgressDownload = async () => { try { await downloadFileWithProgress(fileId, fileInfo?.title); } catch (error) { console.error('Download with progress failed:', error); } }; // 预览文件 const handlePreview = () => { previewFile(fileId); }; // 复制链接 const handleCopyLink = async () => { try { await copyFileLink(fileId); alert('链接已复制到剪贴板!'); } catch (error) { alert('复制失败'); } }; return (

高级文件下载

{/* 文件ID输入 */}
setFileId(e.target.value)} placeholder="输入文件ID" className="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* 错误信息 */} {(error || downloadError) && (
{error || downloadError}
)} {/* 下载进度 */} {isDownloading && downloadProgress && (
下载进度 {downloadProgress.percentage}%
{formatFileSize(downloadProgress.loaded)} / {formatFileSize(downloadProgress.total)}
)} {/* 文件信息 */} {fileInfo && (
{getFileIcon(fileInfo.type || '')}

{fileInfo.title || '未知文件'}

{fileInfo.type || '未知类型'}

状态: {fileInfo.status || '未知'}
{fileInfo.meta?.size && (
大小: {formatFileSize(fileInfo.meta.size)}
)}
创建时间: {new Date(fileInfo.createdAt).toLocaleString()}
存储类型: {fileInfo.storageType || '未知'}
)} {/* 操作按钮 */} {fileInfo && (
{canPreview(fileInfo.type || '') && ( )}
{/* 文件预览提示 */} {canPreview(fileInfo.type || '') && (

💡 此文件支持在线预览,点击"预览文件"可以在浏览器中直接查看

)}
)} {/* 使用说明 */}

功能说明:

  • 快速下载:直接通过浏览器下载文件
  • 进度下载:显示下载进度,适合大文件
  • 预览文件:图片、PDF、视频等可在线预览
  • 复制链接:复制文件访问链接到剪贴板
); } // 格式化文件大小 function formatFileSize(bytes: number): string { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }