1bea4f105Sopenharmony_ci/* 2bea4f105Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3bea4f105Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bea4f105Sopenharmony_ci * you may not use this file except in compliance with the License. 5bea4f105Sopenharmony_ci * You may obtain a copy of the License at 6bea4f105Sopenharmony_ci * 7bea4f105Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bea4f105Sopenharmony_ci * 9bea4f105Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bea4f105Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bea4f105Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bea4f105Sopenharmony_ci * See the License for the specific language governing permissions and 13bea4f105Sopenharmony_ci * limitations under the License. 14bea4f105Sopenharmony_ci */ 15bea4f105Sopenharmony_ci 16bea4f105Sopenharmony_ciimport fileExtensionInfo from '@ohos.file.fileExtensionInfo'; 17bea4f105Sopenharmony_ciimport fileAccess from '@ohos.file.fileAccess'; 18bea4f105Sopenharmony_ciimport ObjectUtil from './ObjectUtil'; 19bea4f105Sopenharmony_ciimport Logger from '../log/Logger'; 20bea4f105Sopenharmony_ciimport StringUtil from './StringUtil'; 21bea4f105Sopenharmony_ciimport { FILENAME_MAX_LENGTH, RENAME_CONNECT_CHARACTER } from '../constants/Constant'; 22bea4f105Sopenharmony_ciimport fs from '@ohos.file.fs'; 23bea4f105Sopenharmony_ciimport FileUri from '@ohos.file.fileuri'; 24bea4f105Sopenharmony_ciimport { photoAccessHelper } from '@kit.MediaLibraryKit'; 25bea4f105Sopenharmony_ci 26bea4f105Sopenharmony_ciconst TAG = 'FileUtil'; 27bea4f105Sopenharmony_ci 28bea4f105Sopenharmony_ciexport class FileUtil { 29bea4f105Sopenharmony_ci /** 30bea4f105Sopenharmony_ci * uri 格式开头 31bea4f105Sopenharmony_ci */ 32bea4f105Sopenharmony_ci static readonly URI_START = 'file://'; 33bea4f105Sopenharmony_ci 34bea4f105Sopenharmony_ci /** 35bea4f105Sopenharmony_ci * 根据fileAccess.FileInfo中的mode匹配是否是文件夹 36bea4f105Sopenharmony_ci * @param mode number 37bea4f105Sopenharmony_ci * @returns boolean 38bea4f105Sopenharmony_ci */ 39bea4f105Sopenharmony_ci public static isFolder(mode: number): boolean { 40bea4f105Sopenharmony_ci return (mode & fileExtensionInfo.DocumentFlag.REPRESENTS_DIR) === fileExtensionInfo.DocumentFlag.REPRESENTS_DIR; 41bea4f105Sopenharmony_ci } 42bea4f105Sopenharmony_ci 43bea4f105Sopenharmony_ci /** 44bea4f105Sopenharmony_ci * 计算文件夹子文件个数 45bea4f105Sopenharmony_ci * @param fileIterator fileAccess.FileIterator 46bea4f105Sopenharmony_ci * @returns number 47bea4f105Sopenharmony_ci */ 48bea4f105Sopenharmony_ci public static getChildCountOfFolder(fileIterator: fileAccess.FileIterator): number { 49bea4f105Sopenharmony_ci let count = 0; 50bea4f105Sopenharmony_ci if (ObjectUtil.isNullOrUndefined(fileIterator)) { 51bea4f105Sopenharmony_ci return count; 52bea4f105Sopenharmony_ci } 53bea4f105Sopenharmony_ci let isDone: boolean = false; 54bea4f105Sopenharmony_ci while (!isDone) { 55bea4f105Sopenharmony_ci let currItem = fileIterator.next(); 56bea4f105Sopenharmony_ci isDone = currItem.done; 57bea4f105Sopenharmony_ci if (isDone) { 58bea4f105Sopenharmony_ci break; 59bea4f105Sopenharmony_ci } 60bea4f105Sopenharmony_ci count++; 61bea4f105Sopenharmony_ci } 62bea4f105Sopenharmony_ci return count; 63bea4f105Sopenharmony_ci } 64bea4f105Sopenharmony_ci 65bea4f105Sopenharmony_ci /** 66bea4f105Sopenharmony_ci * 获取文件信息 67bea4f105Sopenharmony_ci * @param uri 文件uri 68bea4f105Sopenharmony_ci * @param fileAccessHelper fileAccess.FileAccessHelper 69bea4f105Sopenharmony_ci * @returns fileAccess.FileInfo 70bea4f105Sopenharmony_ci */ 71bea4f105Sopenharmony_ci public static async getFileInfoByUri(uri: string, 72bea4f105Sopenharmony_ci fileAccessHelper: fileAccess.FileAccessHelper): Promise<fileAccess.FileInfo> { 73bea4f105Sopenharmony_ci try { 74bea4f105Sopenharmony_ci return await fileAccessHelper.getFileInfoFromUri(uri); 75bea4f105Sopenharmony_ci } catch (err) { 76bea4f105Sopenharmony_ci Logger.e(TAG, 'getFileInfoByUri err: ' + JSON.stringify(err)); 77bea4f105Sopenharmony_ci } 78bea4f105Sopenharmony_ci return null; 79bea4f105Sopenharmony_ci } 80bea4f105Sopenharmony_ci 81bea4f105Sopenharmony_ci /** 82bea4f105Sopenharmony_ci * 获取文件信息 83bea4f105Sopenharmony_ci * @param relativePath 文件relativePath 84bea4f105Sopenharmony_ci * @param fileAccessHelper fileAccess.FileAccessHelper 85bea4f105Sopenharmony_ci * @returns fileAccess.FileInfo 86bea4f105Sopenharmony_ci */ 87bea4f105Sopenharmony_ci public static async getFileInfoByRelativePath(relativePath: string, 88bea4f105Sopenharmony_ci fileAccessHelper: fileAccess.FileAccessHelper): Promise<fileAccess.FileInfo> { 89bea4f105Sopenharmony_ci try { 90bea4f105Sopenharmony_ci return await fileAccessHelper.getFileInfoFromRelativePath(relativePath); 91bea4f105Sopenharmony_ci } catch (err) { 92bea4f105Sopenharmony_ci Logger.e(TAG, 'getFileInfoByRelativePath err: ' + JSON.stringify(err)); 93bea4f105Sopenharmony_ci } 94bea4f105Sopenharmony_ci return null; 95bea4f105Sopenharmony_ci } 96bea4f105Sopenharmony_ci 97bea4f105Sopenharmony_ci /** 98bea4f105Sopenharmony_ci * 根据uri获取文件夹子文件列表Iterator 99bea4f105Sopenharmony_ci * @param uri 100bea4f105Sopenharmony_ci * @param fileAccessHelper 101bea4f105Sopenharmony_ci * @returns FileIterator 102bea4f105Sopenharmony_ci */ 103bea4f105Sopenharmony_ci public static async getFileIteratorByUri(uri: string, 104bea4f105Sopenharmony_ci fileAccessHelper: fileAccess.FileAccessHelper): Promise<fileAccess.FileIterator> { 105bea4f105Sopenharmony_ci try { 106bea4f105Sopenharmony_ci let fileInfo = await fileAccessHelper.getFileInfoFromUri(uri); 107bea4f105Sopenharmony_ci return fileInfo.listFile(); 108bea4f105Sopenharmony_ci } catch (err) { 109bea4f105Sopenharmony_ci Logger.e(TAG, 'getFileIteratorByUri err: ' + JSON.stringify(err)); 110bea4f105Sopenharmony_ci } 111bea4f105Sopenharmony_ci return null; 112bea4f105Sopenharmony_ci } 113bea4f105Sopenharmony_ci 114bea4f105Sopenharmony_ci public static getFileAccessHelper(context, wants): fileAccess.FileAccessHelper { 115bea4f105Sopenharmony_ci try { 116bea4f105Sopenharmony_ci return fileAccess.createFileAccessHelper(context, wants); 117bea4f105Sopenharmony_ci } catch (err) { 118bea4f105Sopenharmony_ci Logger.i(TAG, 'getFileAccessHelper err: ' + JSON.stringify(err)); 119bea4f105Sopenharmony_ci } 120bea4f105Sopenharmony_ci return null; 121bea4f105Sopenharmony_ci } 122bea4f105Sopenharmony_ci 123bea4f105Sopenharmony_ci public static async getFileAccessHelperAsync(context): Promise<fileAccess.FileAccessHelper> { 124bea4f105Sopenharmony_ci try { 125bea4f105Sopenharmony_ci let wants = await fileAccess.getFileAccessAbilityInfo(); 126bea4f105Sopenharmony_ci return fileAccess.createFileAccessHelper(context, wants); 127bea4f105Sopenharmony_ci } catch (err) { 128bea4f105Sopenharmony_ci Logger.i(TAG, 'getFileAccessHelperAsync err: ' + JSON.stringify(err)); 129bea4f105Sopenharmony_ci } 130bea4f105Sopenharmony_ci return null; 131bea4f105Sopenharmony_ci } 132bea4f105Sopenharmony_ci 133bea4f105Sopenharmony_ci public static getParentRelativePath(relativePath: string): string { 134bea4f105Sopenharmony_ci let curPath = relativePath; 135bea4f105Sopenharmony_ci if (StringUtil.isEmpty(relativePath)) { 136bea4f105Sopenharmony_ci return ''; 137bea4f105Sopenharmony_ci } 138bea4f105Sopenharmony_ci 139bea4f105Sopenharmony_ci let index: number = curPath.lastIndexOf('/'); 140bea4f105Sopenharmony_ci // 去掉最后一个'/' 141bea4f105Sopenharmony_ci if (index === curPath.length - 1) { 142bea4f105Sopenharmony_ci curPath = curPath.substr(0, index); 143bea4f105Sopenharmony_ci } 144bea4f105Sopenharmony_ci index = curPath.lastIndexOf('/'); 145bea4f105Sopenharmony_ci if (index <= 0) { 146bea4f105Sopenharmony_ci return ''; 147bea4f105Sopenharmony_ci } 148bea4f105Sopenharmony_ci return curPath.substr(0, index + 1); 149bea4f105Sopenharmony_ci } 150bea4f105Sopenharmony_ci 151bea4f105Sopenharmony_ci public static getUsageHabitsKey(prefix: string, suffix: string): string { 152bea4f105Sopenharmony_ci return prefix + suffix.charAt(0).toLocaleUpperCase() + suffix.substring(1); 153bea4f105Sopenharmony_ci } 154bea4f105Sopenharmony_ci 155bea4f105Sopenharmony_ci /** 156bea4f105Sopenharmony_ci * 是否是uri路径 157bea4f105Sopenharmony_ci * @param path 路径 158bea4f105Sopenharmony_ci * @returns 结果 159bea4f105Sopenharmony_ci */ 160bea4f105Sopenharmony_ci public static isUriPath(path: string): boolean { 161bea4f105Sopenharmony_ci if (ObjectUtil.isNullOrUndefined(path)) { 162bea4f105Sopenharmony_ci return false; 163bea4f105Sopenharmony_ci } 164bea4f105Sopenharmony_ci return path.startsWith(this.URI_START); 165bea4f105Sopenharmony_ci } 166bea4f105Sopenharmony_ci 167bea4f105Sopenharmony_ci /** 168bea4f105Sopenharmony_ci * 从目录下获取某个文件名的文件 169bea4f105Sopenharmony_ci * @param foldrUri 目录uri 170bea4f105Sopenharmony_ci * @param fileName 文件名 171bea4f105Sopenharmony_ci * return 结果 172bea4f105Sopenharmony_ci */ 173bea4f105Sopenharmony_ci public static async getFileFromFolder(foldrUri: string, fileName, 174bea4f105Sopenharmony_ci fileAccessHelper: fileAccess.FileAccessHelper): Promise<fileAccess.FileInfo> { 175bea4f105Sopenharmony_ci // 先将目录的信息查询出来 176bea4f105Sopenharmony_ci let fileInfo: fileAccess.FileInfo = await this.getFileInfoByUri(foldrUri, fileAccessHelper); 177bea4f105Sopenharmony_ci if (ObjectUtil.isNullOrUndefined(fileInfo)) { 178bea4f105Sopenharmony_ci return null; 179bea4f105Sopenharmony_ci } 180bea4f105Sopenharmony_ci // 构建目标目录下的同名文件的相对路径 181bea4f105Sopenharmony_ci const destFileRelativePath = fileInfo.relativePath + fileInfo.fileName + '/' + fileName; 182bea4f105Sopenharmony_ci // 根据相对路径查询相应的文件 183bea4f105Sopenharmony_ci return await this.getFileInfoByRelativePath(destFileRelativePath, fileAccessHelper); 184bea4f105Sopenharmony_ci } 185bea4f105Sopenharmony_ci 186bea4f105Sopenharmony_ci /** 187bea4f105Sopenharmony_ci * 根据FileInfo获取当前文件的文件夹 188bea4f105Sopenharmony_ci * 189bea4f105Sopenharmony_ci * @param fileInfo 文件对象 190bea4f105Sopenharmony_ci * @returns 返回当前文件的文件夹 191bea4f105Sopenharmony_ci */ 192bea4f105Sopenharmony_ci public static getCurrentFolderByFileInfo(fileInfo: fileAccess.FileInfo): string { 193bea4f105Sopenharmony_ci if (fileInfo !== null) { 194bea4f105Sopenharmony_ci let path = fileInfo.relativePath; 195bea4f105Sopenharmony_ci return FileUtil.getCurrentDir(path, FileUtil.isFolder(fileInfo.mode)); 196bea4f105Sopenharmony_ci } 197bea4f105Sopenharmony_ci return ""; 198bea4f105Sopenharmony_ci } 199bea4f105Sopenharmony_ci 200bea4f105Sopenharmony_ci public static async createFolder(fileAccessHelper: fileAccess.FileAccessHelper, parentUri: string, 201bea4f105Sopenharmony_ci name: string): Promise<{ 202bea4f105Sopenharmony_ci code, 203bea4f105Sopenharmony_ci uri 204bea4f105Sopenharmony_ci }> { 205bea4f105Sopenharmony_ci let uri: string = ''; 206bea4f105Sopenharmony_ci let code: any; 207bea4f105Sopenharmony_ci try { 208bea4f105Sopenharmony_ci uri = await fileAccessHelper.mkDir(parentUri, name); 209bea4f105Sopenharmony_ci } catch (error) { 210bea4f105Sopenharmony_ci code = error.code; 211bea4f105Sopenharmony_ci Logger.e(TAG, 'createFolder error occurred:' + error.code + ', ' + error.message); 212bea4f105Sopenharmony_ci } 213bea4f105Sopenharmony_ci return { code: code, uri: uri }; 214bea4f105Sopenharmony_ci } 215bea4f105Sopenharmony_ci 216bea4f105Sopenharmony_ci public static async hardDelete(uri: string): Promise<boolean> { 217bea4f105Sopenharmony_ci try { 218bea4f105Sopenharmony_ci await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(globalThis.abilityContext , [uri]); 219bea4f105Sopenharmony_ci return true; 220bea4f105Sopenharmony_ci } catch (e) { 221bea4f105Sopenharmony_ci Logger.e(TAG, 'hardDelete error: ' + JSON.stringify(e)); 222bea4f105Sopenharmony_ci } 223bea4f105Sopenharmony_ci return false; 224bea4f105Sopenharmony_ci } 225bea4f105Sopenharmony_ci 226bea4f105Sopenharmony_ci /** 227bea4f105Sopenharmony_ci * 重命名 228bea4f105Sopenharmony_ci * @param fileAccessHelper FileAccessHelper 229bea4f105Sopenharmony_ci * @param oldUri oldUri 230bea4f105Sopenharmony_ci * @param newName newName 231bea4f105Sopenharmony_ci * @returns {err, uri} 232bea4f105Sopenharmony_ci */ 233bea4f105Sopenharmony_ci public static async rename(fileAccessHelper: fileAccess.FileAccessHelper, oldUri: string, newName: string): Promise<{ 234bea4f105Sopenharmony_ci err, 235bea4f105Sopenharmony_ci uri 236bea4f105Sopenharmony_ci }> { 237bea4f105Sopenharmony_ci let uri: string = ''; 238bea4f105Sopenharmony_ci let err: any; 239bea4f105Sopenharmony_ci try { 240bea4f105Sopenharmony_ci uri = await fileAccessHelper.rename(oldUri, newName); 241bea4f105Sopenharmony_ci } catch (error) { 242bea4f105Sopenharmony_ci err = { code: error.code, message: error.message }; 243bea4f105Sopenharmony_ci Logger.e(TAG, 'rename error occurred:' + error.code + ', ' + error.message); 244bea4f105Sopenharmony_ci } 245bea4f105Sopenharmony_ci return { err: err, uri: uri }; 246bea4f105Sopenharmony_ci } 247bea4f105Sopenharmony_ci 248bea4f105Sopenharmony_ci public static async createFile(fileAccessHelper: fileAccess.FileAccessHelper, parentUri: string, 249bea4f105Sopenharmony_ci fileName: string): Promise<{ 250bea4f105Sopenharmony_ci err, 251bea4f105Sopenharmony_ci uri 252bea4f105Sopenharmony_ci }> { 253bea4f105Sopenharmony_ci let retUri: string = ''; 254bea4f105Sopenharmony_ci let err: any; 255bea4f105Sopenharmony_ci try { 256bea4f105Sopenharmony_ci Logger.i(TAG, 'createFile ' + fileAccessHelper + '; ' + parentUri + " ; " + fileName); 257bea4f105Sopenharmony_ci retUri = await fileAccessHelper.createFile(parentUri, fileName); 258bea4f105Sopenharmony_ci } catch (e) { 259bea4f105Sopenharmony_ci Logger.e(TAG, 'createFile error: ' + e.code + ', ' + e.message); 260bea4f105Sopenharmony_ci err = { code: e.code, message: e.message }; 261bea4f105Sopenharmony_ci } 262bea4f105Sopenharmony_ci return { err: err, uri: retUri }; 263bea4f105Sopenharmony_ci } 264bea4f105Sopenharmony_ci 265bea4f105Sopenharmony_ci public static hasSubFolder(loadPath: string, curFolderPath: string): boolean { 266bea4f105Sopenharmony_ci if (!StringUtil.isEmpty(loadPath)) { 267bea4f105Sopenharmony_ci if (!StringUtil.isEmpty(curFolderPath)) { 268bea4f105Sopenharmony_ci loadPath = FileUtil.getPathWithFileSplit(loadPath); 269bea4f105Sopenharmony_ci curFolderPath = FileUtil.getPathWithFileSplit(curFolderPath); 270bea4f105Sopenharmony_ci if (loadPath.startsWith(curFolderPath)) { 271bea4f105Sopenharmony_ci return true; 272bea4f105Sopenharmony_ci } 273bea4f105Sopenharmony_ci } 274bea4f105Sopenharmony_ci } 275bea4f105Sopenharmony_ci return false; 276bea4f105Sopenharmony_ci } 277bea4f105Sopenharmony_ci 278bea4f105Sopenharmony_ci public static getPathWithFileSplit(path: string): string { 279bea4f105Sopenharmony_ci let fileSplit: string = '/'; 280bea4f105Sopenharmony_ci if (path && !path.endsWith(fileSplit)) { 281bea4f105Sopenharmony_ci path = path + fileSplit; 282bea4f105Sopenharmony_ci } 283bea4f105Sopenharmony_ci return path; 284bea4f105Sopenharmony_ci } 285bea4f105Sopenharmony_ci 286bea4f105Sopenharmony_ci public static loadSubFinish(loadPath: string, curFolderPath: string, maxLevel: number): boolean { 287bea4f105Sopenharmony_ci let fileSplit: string = '/'; 288bea4f105Sopenharmony_ci if (!StringUtil.isEmpty(loadPath)) { 289bea4f105Sopenharmony_ci if (!loadPath.endsWith(fileSplit)) { 290bea4f105Sopenharmony_ci loadPath = loadPath + fileSplit; 291bea4f105Sopenharmony_ci } 292bea4f105Sopenharmony_ci 293bea4f105Sopenharmony_ci let folders = curFolderPath.split(fileSplit); 294bea4f105Sopenharmony_ci 295bea4f105Sopenharmony_ci if ((curFolderPath + fileSplit) === loadPath || folders.length >= maxLevel) { 296bea4f105Sopenharmony_ci return true; 297bea4f105Sopenharmony_ci } 298bea4f105Sopenharmony_ci } 299bea4f105Sopenharmony_ci return false; 300bea4f105Sopenharmony_ci } 301bea4f105Sopenharmony_ci 302bea4f105Sopenharmony_ci public static renameFile(fileName: string, renameCount: number, suffix: string): string { 303bea4f105Sopenharmony_ci if (ObjectUtil.isNullOrUndefined(fileName)) { 304bea4f105Sopenharmony_ci return fileName; 305bea4f105Sopenharmony_ci } 306bea4f105Sopenharmony_ci let newName = fileName; 307bea4f105Sopenharmony_ci if (renameCount > 0) { 308bea4f105Sopenharmony_ci newName = fileName + RENAME_CONNECT_CHARACTER + renameCount; 309bea4f105Sopenharmony_ci let strLen = newName.length + suffix.length; 310bea4f105Sopenharmony_ci // 字符长度大于最大长度 311bea4f105Sopenharmony_ci if (strLen > FILENAME_MAX_LENGTH) { 312bea4f105Sopenharmony_ci // 计算需要裁剪的长度 313bea4f105Sopenharmony_ci let subLen = strLen - FILENAME_MAX_LENGTH + 1; 314bea4f105Sopenharmony_ci newName = fileName.substring(0, fileName.length - subLen) + RENAME_CONNECT_CHARACTER + renameCount; 315bea4f105Sopenharmony_ci } 316bea4f105Sopenharmony_ci } 317bea4f105Sopenharmony_ci return newName + suffix; 318bea4f105Sopenharmony_ci } 319bea4f105Sopenharmony_ci 320bea4f105Sopenharmony_ci public static getFileNameReName(fileName: string): string[] { 321bea4f105Sopenharmony_ci if (StringUtil.isEmpty(fileName)) { 322bea4f105Sopenharmony_ci return null; 323bea4f105Sopenharmony_ci } 324bea4f105Sopenharmony_ci let index = fileName.lastIndexOf(RENAME_CONNECT_CHARACTER); 325bea4f105Sopenharmony_ci if (index === -1) { 326bea4f105Sopenharmony_ci return null; 327bea4f105Sopenharmony_ci } 328bea4f105Sopenharmony_ci let str = fileName.substring(index + 1, fileName.length); 329bea4f105Sopenharmony_ci let name = fileName.substring(0, index); 330bea4f105Sopenharmony_ci return [name, str]; 331bea4f105Sopenharmony_ci } 332bea4f105Sopenharmony_ci 333bea4f105Sopenharmony_ci public static getCurrentDir(path: string, isFolder: boolean): string { 334bea4f105Sopenharmony_ci if (isFolder) { 335bea4f105Sopenharmony_ci return path; 336bea4f105Sopenharmony_ci } 337bea4f105Sopenharmony_ci if (path) { 338bea4f105Sopenharmony_ci let index: number = path.lastIndexOf('/'); 339bea4f105Sopenharmony_ci let len: number = path.length; 340bea4f105Sopenharmony_ci if (len > 1 && index > 1) { 341bea4f105Sopenharmony_ci return path.substring(0, index); 342bea4f105Sopenharmony_ci } 343bea4f105Sopenharmony_ci } 344bea4f105Sopenharmony_ci return path; 345bea4f105Sopenharmony_ci } 346bea4f105Sopenharmony_ci 347bea4f105Sopenharmony_ci public static getUriPath(path: string): string { 348bea4f105Sopenharmony_ci if (path && FileUtil.isUriPath(path)) { 349bea4f105Sopenharmony_ci return path; 350bea4f105Sopenharmony_ci } 351bea4f105Sopenharmony_ci return null; 352bea4f105Sopenharmony_ci } 353bea4f105Sopenharmony_ci 354bea4f105Sopenharmony_ci /** 355bea4f105Sopenharmony_ci * 根据文件的沙箱路径获取文件uri 356bea4f105Sopenharmony_ci * @param path 文件的沙箱路径 357bea4f105Sopenharmony_ci * @returns 文件的uri 358bea4f105Sopenharmony_ci */ 359bea4f105Sopenharmony_ci public static getUriFromPath(path: string): string { 360bea4f105Sopenharmony_ci let uri = ''; 361bea4f105Sopenharmony_ci try { 362bea4f105Sopenharmony_ci // 该接口如果以’/'结尾,返回的uri会以‘/'结尾 363bea4f105Sopenharmony_ci uri = FileUri.getUriFromPath(path); 364bea4f105Sopenharmony_ci } catch (error) { 365bea4f105Sopenharmony_ci Logger.e(TAG, 'getUriFromPath fail, error:' + JSON.stringify(error)); 366bea4f105Sopenharmony_ci } 367bea4f105Sopenharmony_ci return uri; 368bea4f105Sopenharmony_ci } 369bea4f105Sopenharmony_ci 370bea4f105Sopenharmony_ci /** 371bea4f105Sopenharmony_ci * 将文件uri转换成FileUri对象 372bea4f105Sopenharmony_ci */ 373bea4f105Sopenharmony_ci public static getFileUriObjectFromUri(uri: string): FileUri.FileUri | undefined { 374bea4f105Sopenharmony_ci let fileUriObject: FileUri.FileUri | undefined; 375bea4f105Sopenharmony_ci try { 376bea4f105Sopenharmony_ci fileUriObject = new FileUri.FileUri(uri); 377bea4f105Sopenharmony_ci } catch (error) { 378bea4f105Sopenharmony_ci Logger.e(TAG, 'getFileUriObjectFromUri fail, error:' + JSON.stringify(error)); 379bea4f105Sopenharmony_ci } 380bea4f105Sopenharmony_ci return fileUriObject; 381bea4f105Sopenharmony_ci } 382bea4f105Sopenharmony_ci 383bea4f105Sopenharmony_ci /** 384bea4f105Sopenharmony_ci * 通过将文件uri转换成FileUri对象获取文件的沙箱路径 385bea4f105Sopenharmony_ci * @param uri 文件uri 386bea4f105Sopenharmony_ci * @returns 文件的沙箱路径 387bea4f105Sopenharmony_ci */ 388bea4f105Sopenharmony_ci public static getPathFromUri(uri: string): string { 389bea4f105Sopenharmony_ci let path = ''; 390bea4f105Sopenharmony_ci const fileUriObj = FileUtil.getFileUriObjectFromUri(uri); 391bea4f105Sopenharmony_ci if (!!fileUriObj) { 392bea4f105Sopenharmony_ci path = fileUriObj.path; 393bea4f105Sopenharmony_ci } 394bea4f105Sopenharmony_ci return path; 395bea4f105Sopenharmony_ci } 396bea4f105Sopenharmony_ci 397bea4f105Sopenharmony_ci /** 398bea4f105Sopenharmony_ci * 创建文件夹 399bea4f105Sopenharmony_ci * @param parentFolderUri 父目录uri 400bea4f105Sopenharmony_ci * @param newFolderName 新文件夹名 401bea4f105Sopenharmony_ci * @returns 新文件夹uri 402bea4f105Sopenharmony_ci */ 403bea4f105Sopenharmony_ci public static createFolderByFs(parentFolderUri: string, newFolderName: string): string { 404bea4f105Sopenharmony_ci try { 405bea4f105Sopenharmony_ci const parentFolderPath = FileUtil.getPathFromUri(parentFolderUri); 406bea4f105Sopenharmony_ci const newFolderPath = parentFolderPath + '/' + newFolderName; 407bea4f105Sopenharmony_ci fs.mkdirSync(newFolderPath); 408bea4f105Sopenharmony_ci return FileUtil.getUriFromPath(newFolderPath); 409bea4f105Sopenharmony_ci } catch (error) { 410bea4f105Sopenharmony_ci Logger.e(TAG, 'createFolderByFs fail, error:' + JSON.stringify(error)); 411bea4f105Sopenharmony_ci throw error as Error; 412bea4f105Sopenharmony_ci } 413bea4f105Sopenharmony_ci } 414bea4f105Sopenharmony_ci}