Переглянути джерело

feat(webdav):优化WebDAV URL构建逻辑

- 支持直接返回已完整的HTTP/HTTPS URL,避免重复拼接
- 在构建视频URL时优先使用远端相对路径
- 增强错误处理,提供更准确的日志记录与路径回退机制
chendeben 9 місяців тому
батько
коміт
72d13d94ec

+ 7 - 1
entry/src/main/ets/common/util/WebDavUrlUtil.ets

@@ -34,6 +34,12 @@ export class WebDavUrlUtil {
       return '';
     }
 
+    // 如果传入的路径已经是完整的HTTP/HTTPS URL,直接返回,避免重复拼接前缀
+    if (WebDavUrlUtil.isHttpUrl(relativePath)) {
+      Logger.info(TAG, `buildFullUrl: 传入路径已是完整URL,直接返回: ${relativePath}`);
+      return relativePath;
+    }
+
     // 确保relativePath以/开头
     const normalizedPath = relativePath.startsWith('/') ? relativePath : '/' + relativePath;
 
@@ -166,4 +172,4 @@ export class WebDavUrlUtil {
     // 如果是相对路径,需要拼接完整URL
     return WebDavUrlUtil.buildFullUrl(account, storagePath);
   }
-}
+}

+ 7 - 4
entry/src/main/ets/view/LocalMusic.ets

@@ -153,17 +153,20 @@ function getTypeOrder(type: number) {
 async function setVideoUrlForSong(song: VideoItem): Promise<string> {
   if (song.type === CommonConstants.TYPE_WEBDAV && song.webdav_account_id) {
     try {
-      const fullUrl = await WebDavUrlUtil.buildFullUrlByAccountId(song.webdav_account_id, song.filePath);
+      // 优先使用远端相对路径,其次回退到 filePath
+      const relativePath = song.remote_rel_path || song.filePath;
+      const fullUrl = await WebDavUrlUtil.buildFullUrlByAccountId(song.webdav_account_id, relativePath);
       if (fullUrl) {
         Logger.info(TAG, `WebDAV完整URL构建成功: ${fullUrl}`);
         return fullUrl.replace(/ /g, '%20');
       } else {
-        Logger.error(TAG, `WebDAV URL构建失败,使用原始路径: ${song.filePath}`);
-        return song.filePath.replace(/ /g, '%20');
+        Logger.error(TAG, `WebDAV URL构建失败,使用原始路径: ${relativePath}`);
+        return relativePath.replace(/ /g, '%20');
       }
     } catch (error) {
       Logger.error(TAG, `WebDAV URL构建出错: ${(error as Error).message}`);
-      return song.filePath.replace(/ /g, '%20');
+      const fallbackPath = song.remote_rel_path || song.filePath;
+      return fallbackPath.replace(/ /g, '%20');
     }
   } else {
     // 非WebDAV文件或缺少webdav_account_id,直接使用原始路径