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

修复webdav上传文件的没有实时进度的问题

onecold 8 месяцев назад
Родитель
Сommit
fda0b7a898

+ 34 - 16
entry/src/main/ets/common/util/RcpSocketUtil.ets

@@ -938,38 +938,56 @@ export class RcpSocket {
         let lastProgressTime = Date.now();
         const progressThrottle = 500; // 进度更新节流,每500ms更新一次
         let progressCallCount = 0; // 进度回调计数
-
         const customHttpEventsHandler: rcp.HttpEventsHandler = {
-          onDataReceive: async (incomingData: ArrayBuffer) => {
-            // 上传响应数据接收
-            uploadedSize += incomingData.byteLength;
+          onUploadProgress: async (total: number, uploaded: number) => {
+            // 上传进度监控回调
+            uploadedSize = uploaded;
             progressCallCount++;
             const currentTime = Date.now();
             const timeSinceLastUpdate = currentTime - lastProgressTime;
-            const isComplete = uploadedSize >= fileSize;
-            
+
+            // 更严格的上传完成判断:需要实际传输数据大于文件大小且回调次数足够多
+            const isActuallyTransferring = progressCallCount > 5; // 至少要有几次回调
+            const isComplete = isActuallyTransferring && uploadedSize >= fileSize && uploadedSize >= total;
+
             // 节流策略:
             // 1. 时间间隔超过阈值
-            // 2. 上传完成
-            // 3. 每100次回调强制更新一次(防止长时间无更新)
-            const shouldUpdate = timeSinceLastUpdate >= progressThrottle || 
-                                isComplete || 
-                                (progressCallCount % 100 === 0);
-            
+            // 2. 确实上传完成(需要多次回调验证)
+            // 3. 每50次回调强制更新一次(防止长时间无更新)
+            const shouldUpdate = timeSinceLastUpdate >= progressThrottle ||
+                                (isActuallyTransferring && isComplete) ||
+                                (progressCallCount % 50 === 0);
+
             if (onProgress && shouldUpdate) {
-              onProgress(uploadedSize, fileSize);
+              // 对于大文件,更加保守地显示进度
+              let displayProgress = uploadedSize;
+              let progressPercentage = Math.floor((uploadedSize / fileSize) * 100);
+
+              // 如果进度超过99%但还没有完成,保守显示99%
+              if (progressPercentage >= 99 && !isComplete) {
+                progressPercentage = 99;
+                displayProgress = Math.min(uploadedSize, fileSize - 1);
+              }
+
+              console.info(UtilName, 'testTag', `上传进度更新: ${progressPercentage}% (${displayProgress}/${fileSize} 字节), 回调次数: ${progressCallCount}`);
+              onProgress(displayProgress, fileSize);
               lastProgressTime = currentTime;
             }
-            
+
             // 后台任务更新也进行节流
             if (timeSinceLastUpdate >= 1000) {
               await this.backgroundManager.updateDataTransferContinuousTask();
             }
           },
+          onDataReceive: async (incomingData: ArrayBuffer) => {
+            // 接收服务器响应(这里主要用于监控上传完成后的响应)
+            console.info(UtilName, 'testTag', `接收到响应数据: ${incomingData.byteLength} 字节`);
+          },
           onDataEnd: () => {
-            console.info(UtilName, 'testTag', '文件数据传输完成');
-            // 确保最后一次进度更新
+            console.info(UtilName, 'testTag', '文件数据传输完成,回调次数:', progressCallCount);
+            // 确保最后一次进度更新设置为100%
             if (onProgress) {
+              console.info(UtilName, 'testTag', `最终上传完成: ${fileSize} 字节,设置为100%`);
               onProgress(fileSize, fileSize);
             }
           }

+ 9 - 13
entry/src/main/ets/common/util/RemoteDriveManager.ets

@@ -2572,20 +2572,16 @@ export class RemoteDriveManager {
         (uploaded: number, total: number) => {
           this.uploadReceivedSize = uploaded;
           this.uploadTotalSize = total;
-          
-          // 进度更新节流:只在间隔超过阈值或上传完成时通知
+
+          // 直接通知UI更新,因为RcpSocketUtil层已经做了节流
+          // 详细日志:上传进度
+          const progress = total > 0 ? Math.min(100, ((uploaded / total) * 100)) : 0;
           const currentTime = Date.now();
-          const shouldNotify = (currentTime - this.lastProgressNotifyTime >= this.PROGRESS_THROTTLE_MS) || 
-                              (uploaded >= total);
-          
-          if (shouldNotify) {
-            this.lastProgressNotifyTime = currentTime;
-            // 详细日志:上传进度
-            const progress = total > 0 ? ((uploaded / total) * 100).toFixed(2) : '0.00';
-            const speedMBps = uploaded > 0 ? (uploaded / (1024 * 1024) / ((currentTime - startTime) / 1000)).toFixed(2) : '0.00';
-            Logger.info(TAG, `上传进度: ${progress}%, 已上传: ${uploaded}/${total} 字节, 速度: ${speedMBps} MB/s`);
-            this.notifyObservers(RemoteDriveManagerStates.UploadProgress);
-          }
+          const elapsedSeconds = (currentTime - startTime) / 1000;
+          const speedMBps = uploaded > 0 && elapsedSeconds > 0 ? (uploaded / (1024 * 1024) / elapsedSeconds).toFixed(2) : '0.00';
+
+          Logger.info(TAG, `上传进度: ${progress.toFixed(2)}%, 已上传: ${uploaded}/${total} 字节, 速度: ${speedMBps} MB/s`);
+          this.notifyObservers(RemoteDriveManagerStates.UploadProgress);
         }
       );
 

+ 1 - 0
entry/src/main/ets/pages/UploadMusicPage.ets

@@ -1320,6 +1320,7 @@ export struct UploadMusicPage {
           top: 2,
           bottom: 2
         })
+        .visibility(Visibility.None)
         .backgroundColor(this.themeColor)
         .borderRadius(8)
     }

+ 6 - 1
entry/src/main/ets/pages/WebDavMainPage.ets

@@ -12,7 +12,7 @@ import { FileInfo } from '../viewmodel/FileInfo';
 import { emitter } from '@kit.BasicServicesKit';
 import { EventConstants } from '../common/constants/EventConstants';
 import { LazyDataSource } from '../common/util/LazyDataSource';
-import { PreferencesUtil, StrUtil } from '@pura/harmony-utils';
+import { ArrayUtil, PreferencesUtil, StrUtil } from '@pura/harmony-utils';
 import { DialogHelper } from '@pura/harmony-dialog';
 import { ButtonFancyModifier,
   MenuModifier,
@@ -150,6 +150,7 @@ export struct WebDavMainPage {
       },200)
 
     }
+
   }
 
   // 更新可见文件夹列表
@@ -190,7 +191,11 @@ export struct WebDavMainPage {
     } catch (error) {
       Logger.error(TAG, '更新文件夹列表失败:', error.toString());
       this.visibleFoldersState = [];
+
     }
+
+    this.isShowTitleBar = true
+
   }
 
   private isSongSelected(song: VideoItem): boolean {