2
0
Эх сурвалжийг харах

♻️ refactor(files): 优化文件预览和下载功能实现

- 移除冗余的getFileDownloadUrl和getFilePreviewUrl异步函数
- 简化handleDownload方法,直接使用record.fullUrl属性
- 简化handlePreview方法,直接使用record.fullUrl属性
- 添加isPreviewable类型检查,优化预览逻辑判断
yourname 4 сар өмнө
parent
commit
1fab8f0457

+ 12 - 46
src/client/admin-shadcn/pages/Files.tsx

@@ -55,32 +55,6 @@ export const FilesPage: React.FC = () => {
     return await response.json() as FileListResponse;
   };
 
-  // 获取文件下载URL
-  const getFileDownloadUrl = async (fileId: number) => {
-    try {
-      const response = await fileClient[':id']['download'].$get({ param: { id: fileId } });
-      if (!response.ok) throw new Error('获取文件下载URL失败');
-      const data = await response.json();
-      return data;
-    } catch (error) {
-      toast.error('获取文件下载URL失败');
-      return null;
-    }
-  };
-
-  // 获取文件预览URL
-  const getFilePreviewUrl = async (fileId: number) => {
-    try {
-      const response = await fileClient[':id']['url'].$get({ param: { id: fileId } });
-      if (!response.ok) throw new Error('获取文件URL失败');
-      const data = await response.json();
-      return data.url;
-    } catch (error) {
-      toast.error('获取文件URL失败');
-      return null;
-    }
-  };
-
   const { data, isLoading, error } = useQuery({
     queryKey: ['files', pagination.current, pagination.pageSize, searchText],
     queryFn: () => fetchFiles({ page: pagination.current, pageSize: pagination.pageSize }),
@@ -114,29 +88,21 @@ export const FilesPage: React.FC = () => {
   });
 
   // 处理文件下载
-  const handleDownload = async (record: FileItem) => {
-    const result = await getFileDownloadUrl(record.id);
-    if (result?.url) {
-      const a = document.createElement('a');
-      a.href = result.url;
-      a.download = result.filename || record.name;
-      document.body.appendChild(a);
-      a.click();
-      document.body.removeChild(a);
-    }
+  const handleDownload = (record: FileItem) => {
+    const a = document.createElement('a');
+    a.href = record.fullUrl;
+    a.download = record.name;
+    document.body.appendChild(a);
+    a.click();
+    document.body.removeChild(a);
   };
 
   // 处理文件预览
-  const handlePreview = async (record: FileItem) => {
-    const url = await getFilePreviewUrl(record.id);
-    if (url) {
-      if (record.type.startsWith('image/')) {
-        window.open(url, '_blank');
-      } else if (record.type.startsWith('video/')) {
-        window.open(url, '_blank');
-      } else {
-        toast.warning('该文件类型不支持预览');
-      }
+  const handlePreview = (record: FileItem) => {
+    if (isPreviewable(record.type)) {
+      window.open(record.fullUrl, '_blank');
+    } else {
+      toast.warning('该文件类型不支持预览');
     }
   };