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

feat(cast): 优化远程文件缓存路径处理逻辑

- 引入 resolveCacheFilePath 函数处理缓存路径解析
- 新增 FileManager 工具类用于文件操作
- 实现 waitForCachePathReady 方法确保缓存文件就绪
- 添加文件存在性和大小验证机制
- 增加轮询等待逻辑确保缓存文件准备完成
- 重构 SMB 类型文件的缓存处理流程
chendeben 7 месяцев назад
Родитель
Сommit
a3ac15c7fd
1 измененных файлов с 32 добавлено и 5 удалено
  1. 32 5
      entry/src/main/ets/controller/CastController.ets

+ 32 - 5
entry/src/main/ets/controller/CastController.ets

@@ -23,8 +23,9 @@ import { fileUri } from '@kit.CoreFileKit';
 import { VideoItem } from '../viewmodel/VideoItem';
 import { VideoItem } from '../viewmodel/VideoItem';
 import { RemoteDriveManager } from '../common/util/RemoteDriveManager';
 import { RemoteDriveManager } from '../common/util/RemoteDriveManager';
 import { RemoteCacheManager } from '../common/network/RemoteCacheManager';
 import { RemoteCacheManager } from '../common/network/RemoteCacheManager';
-import { RemoteCacheType } from '../common/network/RemoteSongCache';
+import { RemoteCacheType, resolveCacheFilePath } from '../common/network/RemoteSongCache';
 import { extractFtpRelativePath, extractSmbRelativePath, isFtpType, isSmbType } from '../common/util/RemotePlayerUtil';
 import { extractFtpRelativePath, extractSmbRelativePath, isFtpType, isSmbType } from '../common/util/RemotePlayerUtil';
+import FileManager from '../common/util/FileManager';
 
 
 const TAG = 'heanup CastController';
 const TAG = 'heanup CastController';
 
 
@@ -174,10 +175,12 @@ export class CastController {
               if (isSmbType(songItem.type)) {
               if (isSmbType(songItem.type)) {
                 const relative = extractSmbRelativePath(songItem, account.smbShare);
                 const relative = extractSmbRelativePath(songItem, account.smbShare);
                 if (relative) {
                 if (relative) {
-                  localCachePath = await RemoteCacheManager.ensureCached(RemoteCacheType.SMB, {
-                    account,
-                    remotePath: relative
-                  });
+                  const cacheInfo = await resolveCacheFilePath(
+                    RemoteCacheType.SMB,
+                    account.id?.toString(),
+                    relative
+                  );
+                  localCachePath = await this.waitForCachePathReady(cacheInfo.cachePath);
                 }
                 }
               } else if (isFtpType(songItem.type)) {
               } else if (isFtpType(songItem.type)) {
                 const relative = extractFtpRelativePath(songItem);
                 const relative = extractFtpRelativePath(songItem);
@@ -285,6 +288,30 @@ export class CastController {
     return false;
     return false;
   }
   }
 
 
+  private async waitForCachePathReady(cachePath: string): Promise<string | undefined> {
+    if (!cachePath) {
+      return undefined;
+    }
+    const exists = await FileManager.isExist(cachePath);
+    if (exists) {
+      const size = await FileManager.getFileSize(cachePath);
+      if (size > 0) {
+        return cachePath;
+      }
+    }
+    const maxAttempts = 20;
+    for (let i = 0; i < maxAttempts; i++) {
+      await new Promise<void>((resolve) => setTimeout(resolve, 100));
+      if (await FileManager.isExist(cachePath)) {
+        const size = await FileManager.getFileSize(cachePath);
+        if (size > 0) {
+          return cachePath;
+        }
+      }
+    }
+    return undefined;
+  }
+
   /**
   /**
    * 设置投播状态变化监听器
    * 设置投播状态变化监听器
    */
    */