Browse Source

优化上传界面

chendeben 9 months ago
parent
commit
290831d3e6

+ 37 - 4
entry/src/main/ets/common/util/RemoteDriveManager.ets

@@ -797,6 +797,19 @@ export class RemoteDriveManager {
     return normalized || '/';
   }
 
+  private getSongKey(song: VideoItem): string {
+    if (!song) {
+      return '';
+    }
+    if (song.filePath && song.filePath.length > 0) {
+      return song.filePath;
+    }
+    if (song.name && song.name.length > 0) {
+      return song.name;
+    }
+    return '';
+  }
+
   private sanitizeRelativeUploadPath(path?: string): string {
     if (!path || path.length === 0) {
       return '';
@@ -1577,11 +1590,31 @@ export class RemoteDriveManager {
     
     // 限制添加数量
     const songsToAdd = songs.length > availableSlots ? songs.slice(0, availableSlots) : songs;
-    
-    Logger.info(TAG, `实际添加: ${songsToAdd.length} 个文件`);
-    
+    const existingKeys = new Set<string>();
+    for (let i = 0; i < this.uploadQueue.length; i++) {
+      const key = this.getSongKey(this.uploadQueue[i].song);
+      if (key) {
+        existingKeys.add(key);
+      }
+    }
+    const filteredSongs: VideoItem[] = [];
     for (let i = 0; i < songsToAdd.length; i++) {
       const song = songsToAdd[i];
+      const key = this.getSongKey(song);
+      if (key && existingKeys.has(key)) {
+        Logger.warn(TAG, `检测到重复任务,已跳过: ${song.name}`);
+        continue;
+      }
+      if (key) {
+        existingKeys.add(key);
+      }
+      filteredSongs.push(song);
+    }
+    
+    Logger.info(TAG, `实际添加: ${filteredSongs.length} 个文件`);
+    
+    for (let i = 0; i < filteredSongs.length; i++) {
+      const song = filteredSongs[i];
       const task: TransferTask = { 
         song, 
         account,
@@ -1592,7 +1625,7 @@ export class RemoteDriveManager {
     }
     
     if (songs.length > songsToAdd.length) {
-      Logger.warn(TAG, `队列空间不足,仅添加了${songsToAdd.length}/${songs.length}个任务`);
+      Logger.warn(TAG, `队列空间不足,仅处理前${songsToAdd.length}/${songs.length}个任务`);
     }
     
     Logger.info(TAG, `新队列大小: ${this.uploadQueue.length}/${this.MAX_UPLOAD_QUEUE_SIZE}`);

+ 30 - 4
entry/src/main/ets/pages/UploadMusicPage.ets

@@ -645,9 +645,11 @@ export struct UploadMusicPage {
 
     // 将选中的项目添加到已选列表
     const selectedItems: LocalFileItem[] = [];
+    const selectedSet = new Set<string>();
     this.selectedBrowserItems.forEach((path) => {
       const found = this.browserFiles.find((f) => f.path === path);
-      if (found) {
+      if (found && !selectedSet.has(found.path)) {
+        selectedSet.add(found.path);
         selectedItems.push(found);
       }
     });
@@ -1013,8 +1015,9 @@ export struct UploadMusicPage {
 
       // 添加到上传队列,传递用户选择的上传路径
       Logger.info(TAG, `使用上传路径: ${this.uploadPath}`);
-      this.webdavManager.addToUploadQueue(songs, this.selectedAccount, this.uploadPath);
-      this.totalUploadCount = songs.length;
+    const uniqueSongs = this.deduplicateSongs(songs);
+    this.webdavManager.addToUploadQueue(uniqueSongs, this.selectedAccount, this.uploadPath);
+    this.totalUploadCount = uniqueSongs.length;
       this.uploadedCount = 0;
 
       // 开始处理上传队列
@@ -1024,6 +1027,10 @@ export struct UploadMusicPage {
       this.selectedFiles = [];
       this.selectedFileNames = [];
       this.selectedFileTypes = [];
+      this.pendingCount = this.webdavManager.uploadQueue.length;
+      this.selectedFiles = [];
+      this.selectedFileNames = [];
+      this.selectedFileTypes = [];
     } catch (error) {
       const err = error as Error;
       Logger.error(TAG, `启动上传失败: ${err.message}`);
@@ -1034,6 +1041,25 @@ export struct UploadMusicPage {
     }
   }
 
+  private deduplicateSongs(songs: VideoItem[]): VideoItem[] {
+    const unique: VideoItem[] = [];
+    const seen = new Set<string>();
+    for (let i = 0; i < songs.length; i++) {
+      const song = songs[i];
+      const key = song.filePath || song.name;
+      if (!key) {
+        continue;
+      }
+      if (seen.has(key)) {
+        Logger.warn(TAG, `跳过重复文件: ${song.name}`);
+        continue;
+      }
+      seen.add(key);
+      unique.push(song);
+    }
+    return unique;
+  }
+
   /**
    * 暂停上传
    */
@@ -1907,7 +1933,7 @@ export struct UploadMusicPage {
           Row({ space: 6 }) {
             Text('📁')
               .fontSize(16)
-            Text('浏览选择')
+        Text('浏览选择')
               .fontSize(14)
               .fontWeight(FontWeight.Medium)
           }