Просмотр исходного кода

🐛 fix(files): 修复文件预览和大小格式化的空值处理问题

- 修改isPreviewable函数参数类型为string | null,并添加空值检查
- 修改formatFileSize函数参数类型为number | null,并添加空值检查,防止空值导致的运行错误
yourname 4 месяцев назад
Родитель
Сommit
3c32d6e2f0
1 измененных файлов с 4 добавлено и 3 удалено
  1. 4 3
      src/client/admin-shadcn/pages/Files.tsx

+ 4 - 3
src/client/admin-shadcn/pages/Files.tsx

@@ -107,7 +107,8 @@ export const FilesPage: React.FC = () => {
   };
 
   // 检查是否为可预览的文件类型
-  const isPreviewable = (fileType: string) => {
+  const isPreviewable = (fileType: string | null) => {
+    if (!fileType) return false;
     return fileType.startsWith('image/') || fileType.startsWith('video/');
   };
 
@@ -171,8 +172,8 @@ export const FilesPage: React.FC = () => {
   };
 
   // 格式化文件大小
-  const formatFileSize = (bytes: number) => {
-    if (bytes === 0) return '0 Bytes';
+  const formatFileSize = (bytes: number | null) => {
+    if (!bytes || bytes === 0) return '0 Bytes';
     const k = 1024;
     const sizes = ['Bytes', 'KB', 'MB', 'GB'];
     const i = Math.floor(Math.log(bytes) / Math.log(k));