cc 2 недель назад
Родитель
Сommit
297246587f
1 измененных файлов с 57 добавлено и 8 удалено
  1. 57 8
      ai-electron/frontend/src/case/views/data/upload.vue

+ 57 - 8
ai-electron/frontend/src/case/views/data/upload.vue

@@ -132,7 +132,7 @@
                   </template>
                 </div>
                 <button
-                  v-if="!isUploading && (uploadStatuses.get(file) || 'pending') === 'pending'"
+                  v-if="!isUploading && canRemoveFile(file)"
                   type="button"
                   class="upload-file-item__remove"
                   aria-label="移除文件"
@@ -224,7 +224,9 @@
   const folderInputRef = ref<HTMLInputElement | null>(null);
   const selectedFiles = ref<File[]>([]);
   const uploadProgressMap = ref<Map<File, number>>(new Map());
-  const uploadStatuses = ref<Map<File, 'pending' | 'uploading' | 'done' | 'error'>>(new Map());
+  // awaiting-read:桌面端文件夹上传的「占位条目」——只登记了名称/大小、内容还没读,
+  // startUpload 只认 pending,会自动跳过它们(见 handleDesktopFolderSelect 的两阶段流程)
+  const uploadStatuses = ref<Map<File, 'pending' | 'awaiting-read' | 'uploading' | 'done' | 'error'>>(new Map());
   const isUploading = ref(false);
   const dragOver = ref(false);
 
@@ -451,6 +453,13 @@
     }
   };
 
+  /**
+   * 桌面端文件夹上传的占位条目 → 本机完整路径。
+   * 占位条目是一个内容为空的 File(size 用 defineProperty 覆写为真实大小),
+   * 阶段二轮到它时按这里的路径读内容、原位替换成真实 File。
+   */
+  const desktopPathByFile = new WeakMap<File, string>();
+
   const handleDesktopFolderSelect = async () => {
     if (desktopFolderReading.value) return;
     if (!batchIdReady.value) {
@@ -472,21 +481,26 @@
     desktopFolderReading.value = true;
     let added = 0;
     let failed = 0;
+    /** 占位条目按发现顺序排队;真实内容严格串行读取,内存里始终只有正在上传的那一个 */
+    const placeholders: File[] = [];
     try {
+      // 阶段一:只 stat 不读内容,占位条目立即全部入队 —— 完整的待上传清单对用户可见
       for (const dir of dirs) {
         try {
           await walkDesktopDirectory(nodeModules.fs, nodeModules.path, dir, async (fullPath, fileName) => {
             if (isAbandoned.value) return;
             try {
               const stat = await nodeModules.fs.promises.stat(fullPath);
-              const buffer = await nodeModules.fs.promises.readFile(fullPath);
-              const file = new File([buffer as unknown as BlobPart], fileName, { lastModified: stat.mtimeMs });
-              addFiles([file], false);
+              const placeholder = new File([], fileName, { lastModified: stat.mtimeMs });
+              Object.defineProperty(placeholder, 'size', { value: stat.size });
+              desktopPathByFile.set(placeholder, fullPath);
+              uploadStatuses.value.set(placeholder, 'awaiting-read');
+              selectedFiles.value = [...selectedFiles.value, placeholder];
+              placeholders.push(placeholder);
               added += 1;
-              await waitForUploadSettled(file);
             } catch (error) {
               failed += 1;
-              console.error('读取本机文件失败', fullPath, error);
+              console.error('读取本机文件信息失败', fullPath, error);
             }
           });
         } catch (error) {
@@ -494,6 +508,35 @@
           console.error('读取本机文件夹失败', dir, error);
         }
       }
+
+      // 阶段二:串行「读取内容 → 原位替换为真实 File → 等它上传结束」。
+      // 真实 File 是队列里唯一的 pending,startUpload 立即消费,内存峰值不随文件夹大小增长。
+      for (const placeholder of placeholders) {
+        if (isAbandoned.value) break;
+        if (uploadStatuses.value.get(placeholder) !== 'awaiting-read') continue; // 已被用户移除/清空
+        const fullPath = desktopPathByFile.get(placeholder);
+        if (!fullPath) continue;
+        try {
+          const buffer = await nodeModules.fs.promises.readFile(fullPath);
+          if (uploadStatuses.value.get(placeholder) !== 'awaiting-read') continue; // 读取期间被移除
+          const file = new File([buffer as unknown as BlobPart], placeholder.name, {
+            lastModified: placeholder.lastModified,
+          });
+          const index = selectedFiles.value.indexOf(placeholder);
+          if (index === -1) continue;
+          selectedFiles.value.splice(index, 1, file);
+          uploadStatuses.value.delete(placeholder);
+          uploadStatuses.value.set(file, 'pending');
+          desktopPathByFile.delete(placeholder);
+          void startUpload();
+          await waitForUploadSettled(file);
+        } catch (error) {
+          // 内容读取失败:占位条目标 ✗ 留在列表里,不阻塞后续文件
+          uploadStatuses.value.set(placeholder, 'error');
+          failed += 1;
+          console.error('读取本机文件失败', fullPath, error);
+        }
+      }
     } finally {
       desktopFolderReading.value = false;
     }
@@ -529,13 +572,19 @@
     uploadStatuses.value = new Map();
   };
 
+  /** 可移除:还没开始上传的条目(含桌面端「待读取」的占位条目) */
+  const canRemoveFile = (target: File) => {
+    const status = uploadStatuses.value.get(target);
+    return !status || status === 'pending' || status === 'awaiting-read';
+  };
+
   /** 移除单个待上传文件(上传解析中不允许移除) */
   const removeSelectedFile = (target: File) => {
     if (isUploading.value) return;
     const index = selectedFiles.value.indexOf(target);
     if (index === -1) return;
     // 仅允许移除待上传文件
-    if ((uploadStatuses.value.get(target) || 'pending') !== 'pending') {
+    if (!canRemoveFile(target)) {
       return;
     }
     selectedFiles.value.splice(index, 1);