Explorar el Código

实现webdav的新建文件夹功能

onecold hace 8 meses
padre
commit
c3d1edbe8e

+ 64 - 1
entry/src/main/ets/common/util/RemoteDriveManager.ets

@@ -2851,9 +2851,72 @@ export class RemoteDriveManager {
     }
   }
 
-}
+  /**
+   * 在WebDAV服务器上创建文件夹
+   * @param account WebDAV账户
+   * @param folderName 要创建的文件夹名称
+   * @param parentPath 父级路径,默认为当前路径
+   * @returns Promise<string> 创建成功后的完整路径
+   */
+  public async createWebDavFolder(account: WebDavAccount, folderName: string, parentPath?: string): Promise<string> {
+    if (!account || account.webType !== RemoteDriveType.WebDav) {
+      throw new Error('只支持在WebDAV账户中创建文件夹');
+    }
 
+    if (!folderName || folderName.trim().length === 0) {
+      throw new Error('文件夹名称不能为空');
+    }
 
+    // 检查文件夹名称是否包含非法字符
+    const invalidChars = /[\\/:*?"<>|]/;
+    if (invalidChars.test(folderName.trim())) {
+      throw new Error('文件夹名称不能包含 \\ / : * ? " < > | 字符');
+    }
+
+    // 确定服务器地址
+    const host = account.isUseLocalHost && account.localHost ? account.localHost : account.host;
+    if (!host || host.length === 0) {
+      throw new Error('WebDAV账户缺少服务器地址');
+    }
+
+    if (!account.account || account.account.length === 0) {
+      throw new Error('WebDAV账户缺少用户名');
+    }
+
+    if (!account.password || account.password.length === 0) {
+      throw new Error('WebDAV账户缺少密码');
+    }
+
+    // 确定父级路径
+    const basePath = parentPath || this.currentPath;
+
+    // 构建完整的文件夹路径
+    const fullPath = basePath === '' || basePath === '/'
+      ? `/${folderName.trim()}`
+      : `${basePath.replace(/\/$/, '')}/${folderName.trim()}`;
+
+    Logger.info(TAG, `heanup 准备在WebDAV服务器创建文件夹: ${fullPath}`);
+
+    try {
+      // 调用RcpSocket的createDirectory方法(使用MKCOL协议)
+      await this.rcpSocket.createDirectory(
+        host,
+        account.port,
+        account.account,
+        account.password,
+        fullPath,
+        account.enableHttps
+      );
+
+      Logger.info(TAG, `heanup WebDAV文件夹创建成功: ${fullPath}`);
+      return fullPath;
+    } catch (error) {
+      const err = error as Error;
+      Logger.error(TAG, `heanup WebDAV文件夹创建失败: ${err.message}`);
+      throw err;
+    }
+  }
+}
 
 /**
  * 构建HTTP请求头,特别处理WebDAV认证

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

@@ -872,7 +872,6 @@ export struct WebDavMainPage {
       })
         .onClick(async () => {
             this.showCreateFolderDialog();
-
         })
 
     }.attributeModifier(new MenuModifier())
@@ -1638,8 +1637,13 @@ export struct WebDavMainPage {
         // 创建成功后刷新当前目录
         await this.webdavManager.loadFilesInfoFromAccount(this.selectedAccount, this.webdavManager.currentPath);
       }else if(this.selectedAccount.webType==RemoteDriveType.WebDav){
-        // 这里帮我实现调用RemoteDriveManager的createWebDavFolder方法
+        // 调用RemoteDriveManager的createWebDavFolder方法
+        const createdPath = await this.webdavManager.createWebDavFolder(this.selectedAccount, folderName);
 
+        // 创建成功后刷新当前目录
+        await this.webdavManager.loadFilesInfoFromAccount(this.selectedAccount, this.webdavManager.currentPath);
+      }else{
+        throw new Error('当前账户类型不支持创建文件夹');
       }