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_ci/**
17bea4f105Sopenharmony_ci * FileAccessManager删除、复制、移动、重命名等操作
18bea4f105Sopenharmony_ci */
19bea4f105Sopenharmony_ciimport Logger from '../log/Logger';
20bea4f105Sopenharmony_ciimport { FilesData } from '../../databases/model/FileData';
21bea4f105Sopenharmony_ciimport { FOLDER_LEVEL, DESKTOP_FOLDER } from '../constants/Constant';
22bea4f105Sopenharmony_ciimport { sortDataByTime, randomId, sortBaseDataByOrderTime } from './Tools';
23bea4f105Sopenharmony_ciimport fileAccess from '@ohos.file.fileAccess';
24bea4f105Sopenharmony_ciimport fileExtensionInfo from '@ohos.file.fileExtensionInfo';
25bea4f105Sopenharmony_ciimport { FileBase } from '../../databases/model/base/FileBase';
26bea4f105Sopenharmony_ciimport ObjectUtil from './ObjectUtil';
27bea4f105Sopenharmony_ciimport { FileUtil } from './FileUtil';
28bea4f105Sopenharmony_ci
29bea4f105Sopenharmony_ciconst TAG = 'FileAccessExec';
30bea4f105Sopenharmony_ci
31bea4f105Sopenharmony_cinamespace FileAccessExec {
32bea4f105Sopenharmony_ci
33bea4f105Sopenharmony_ci  // 创建文件夹
34bea4f105Sopenharmony_ci  export function createFolder(sourceUri: string, folderName: string): any {
35bea4f105Sopenharmony_ci    return new Promise((resolve, reject) => {
36bea4f105Sopenharmony_ci      try {
37bea4f105Sopenharmony_ci        globalThis.fileAcsHelper.mkDir(sourceUri, folderName, (ret, data) => {
38bea4f105Sopenharmony_ci          if (ret && ret.code !== '0') {
39bea4f105Sopenharmony_ci            reject(ret);
40bea4f105Sopenharmony_ci            Logger.e(TAG, 'createFolder fail:' + JSON.stringify(ret));
41bea4f105Sopenharmony_ci          } else {
42bea4f105Sopenharmony_ci            resolve(data);
43bea4f105Sopenharmony_ci          }
44bea4f105Sopenharmony_ci        })
45bea4f105Sopenharmony_ci      } catch (error) {
46bea4f105Sopenharmony_ci        reject(error);
47bea4f105Sopenharmony_ci        Logger.e(TAG, 'createFolder error occurred:' + error);
48bea4f105Sopenharmony_ci      }
49bea4f105Sopenharmony_ci    })
50bea4f105Sopenharmony_ci  }
51bea4f105Sopenharmony_ci
52bea4f105Sopenharmony_ci  // 创建文件
53bea4f105Sopenharmony_ci  export function createFile(sourceUri: string, fileName: string): any {
54bea4f105Sopenharmony_ci    return new Promise((resolve, reject) => {
55bea4f105Sopenharmony_ci      try {
56bea4f105Sopenharmony_ci        globalThis.fileAcsHelper.createFile(sourceUri, fileName, (ret, data) => {
57bea4f105Sopenharmony_ci          if (ret && ret.code !== '0') {
58bea4f105Sopenharmony_ci            reject(ret);
59bea4f105Sopenharmony_ci            Logger.e(TAG, 'createFile fail:' + JSON.stringify(ret));
60bea4f105Sopenharmony_ci          } else {
61bea4f105Sopenharmony_ci            resolve(data);
62bea4f105Sopenharmony_ci          }
63bea4f105Sopenharmony_ci        })
64bea4f105Sopenharmony_ci      } catch (error) {
65bea4f105Sopenharmony_ci        reject(error);
66bea4f105Sopenharmony_ci        Logger.e(TAG, 'createFile error occurred:' + error);
67bea4f105Sopenharmony_ci      }
68bea4f105Sopenharmony_ci    })
69bea4f105Sopenharmony_ci  }
70bea4f105Sopenharmony_ci
71bea4f105Sopenharmony_ci  export function getFileData(): any {
72bea4f105Sopenharmony_ci    let folderList: FilesData[] = [];
73bea4f105Sopenharmony_ci    let fileList: FilesData[] = [];
74bea4f105Sopenharmony_ci    const allData = getRootFolder();
75bea4f105Sopenharmony_ci    allData.forEach((item) => {
76bea4f105Sopenharmony_ci      if (item.isFolder) {
77bea4f105Sopenharmony_ci        folderList.push(new FilesData({
78bea4f105Sopenharmony_ci          ...item
79bea4f105Sopenharmony_ci        }));
80bea4f105Sopenharmony_ci      } else {
81bea4f105Sopenharmony_ci        fileList.push(new FilesData({
82bea4f105Sopenharmony_ci          ...item
83bea4f105Sopenharmony_ci        }));
84bea4f105Sopenharmony_ci      }
85bea4f105Sopenharmony_ci    })
86bea4f105Sopenharmony_ci
87bea4f105Sopenharmony_ci    return { folderList, fileList };
88bea4f105Sopenharmony_ci  }
89bea4f105Sopenharmony_ci
90bea4f105Sopenharmony_ci  export function getFileByCurIterator(fileInfo: fileAccess.FileInfo | fileAccess.RootInfo): FilesData[] {
91bea4f105Sopenharmony_ci    let fileList: FilesData[] = [];
92bea4f105Sopenharmony_ci    try {
93bea4f105Sopenharmony_ci      let fileIterator = fileInfo.listFile();
94bea4f105Sopenharmony_ci      if (!fileIterator) {
95bea4f105Sopenharmony_ci        Logger.w(TAG, 'getFileByCurIterator fail, fileIterator is null');
96bea4f105Sopenharmony_ci        return fileList;
97bea4f105Sopenharmony_ci      }
98bea4f105Sopenharmony_ci      let result = fileIterator.next();
99bea4f105Sopenharmony_ci      let isDone = result.done;
100bea4f105Sopenharmony_ci      while (!isDone) {
101bea4f105Sopenharmony_ci        const {fileName, relativePath,uri, mode, size, mtime, mimeType} = result.value;
102bea4f105Sopenharmony_ci        let tempFile = new FilesData({
103bea4f105Sopenharmony_ci          id: randomId(),
104bea4f105Sopenharmony_ci          fileName,
105bea4f105Sopenharmony_ci          relativePath,
106bea4f105Sopenharmony_ci          uri,
107bea4f105Sopenharmony_ci          mode,
108bea4f105Sopenharmony_ci          size,
109bea4f105Sopenharmony_ci          mtime,
110bea4f105Sopenharmony_ci          mimeType,
111bea4f105Sopenharmony_ci          fileIterator: result.value
112bea4f105Sopenharmony_ci        });
113bea4f105Sopenharmony_ci        fileList.push(tempFile);
114bea4f105Sopenharmony_ci        result = fileIterator.next();
115bea4f105Sopenharmony_ci        isDone = result.done;
116bea4f105Sopenharmony_ci      }
117bea4f105Sopenharmony_ci      fileList = sortDataByTime(fileList);
118bea4f105Sopenharmony_ci    } catch (error) {
119bea4f105Sopenharmony_ci      fileList = [];
120bea4f105Sopenharmony_ci      Logger.e(TAG, 'getFileByCurIterator fail, error:' + JSON.stringify(error) + error);
121bea4f105Sopenharmony_ci    }
122bea4f105Sopenharmony_ci    return fileList;
123bea4f105Sopenharmony_ci  }
124bea4f105Sopenharmony_ci
125bea4f105Sopenharmony_ci  export function getPathPickSubFiles(fileInfo: fileAccess.FileInfo,
126bea4f105Sopenharmony_ci                                      defaultPathPick: string, level: number): FileBase[] {
127bea4f105Sopenharmony_ci    let fileArr: FileBase[] = [];
128bea4f105Sopenharmony_ci    let fileIterator = fileInfo.listFile();
129bea4f105Sopenharmony_ci    if (!fileIterator) {
130bea4f105Sopenharmony_ci      return fileArr;
131bea4f105Sopenharmony_ci    }
132bea4f105Sopenharmony_ci    let result = fileIterator.next();
133bea4f105Sopenharmony_ci    let isDone = result.done;
134bea4f105Sopenharmony_ci    while (!isDone) {
135bea4f105Sopenharmony_ci      try {
136bea4f105Sopenharmony_ci        let fileInfo: fileAccess.FileInfo = result.value;
137bea4f105Sopenharmony_ci        if (!ObjectUtil.isNullOrUndefined(fileInfo)) {
138bea4f105Sopenharmony_ci          let tempFile = new FileBase(result.value, false);
139bea4f105Sopenharmony_ci          if (tempFile.isFolder) {
140bea4f105Sopenharmony_ci            if (FileUtil.hasSubFolder(defaultPathPick, tempFile.currentDir) && level <= FOLDER_LEVEL.MAX_LEVEL) {
141bea4f105Sopenharmony_ci              tempFile.subList = getPathPickSubFiles(fileInfo, defaultPathPick, level + 1);
142bea4f105Sopenharmony_ci            }
143bea4f105Sopenharmony_ci          }
144bea4f105Sopenharmony_ci          // 根目录下不显示Documents文件夹
145bea4f105Sopenharmony_ci          fileArr.push(tempFile);
146bea4f105Sopenharmony_ci        }
147bea4f105Sopenharmony_ci        result = fileIterator.next();
148bea4f105Sopenharmony_ci        isDone = result.done;
149bea4f105Sopenharmony_ci      } catch (e) {
150bea4f105Sopenharmony_ci        Logger.e(TAG, 'getSubFileByIterator error: ' + e.toString());
151bea4f105Sopenharmony_ci        isDone = true;
152bea4f105Sopenharmony_ci      }
153bea4f105Sopenharmony_ci    }
154bea4f105Sopenharmony_ci    fileArr = sortBaseDataByOrderTime(fileArr, true);
155bea4f105Sopenharmony_ci    return fileArr;
156bea4f105Sopenharmony_ci  }
157bea4f105Sopenharmony_ci
158bea4f105Sopenharmony_ci
159bea4f105Sopenharmony_ci  export function getRootFolder(): FilesData[] {
160bea4f105Sopenharmony_ci    let fileList: FilesData[] = [];
161bea4f105Sopenharmony_ci    if (!globalThis.rootInfoArr) {
162bea4f105Sopenharmony_ci      Logger.e(TAG, 'getRootFolder fail, rootInfoArr is null');
163bea4f105Sopenharmony_ci      return fileList;
164bea4f105Sopenharmony_ci    }
165bea4f105Sopenharmony_ci    try {
166bea4f105Sopenharmony_ci      const rootFolder: fileAccess.RootInfo = globalThis.rootInfoArr.find(
167bea4f105Sopenharmony_ci        (item: fileAccess.RootInfo) => item.deviceType === fileExtensionInfo.DeviceType.DEVICE_LOCAL_DISK);
168bea4f105Sopenharmony_ci      if (rootFolder) {
169bea4f105Sopenharmony_ci        globalThis.documentInfo = rootFolder;
170bea4f105Sopenharmony_ci        fileList = getFileByCurIterator(rootFolder);
171bea4f105Sopenharmony_ci        fileList = fileList.filter(item => item.fileName !== DESKTOP_FOLDER);
172bea4f105Sopenharmony_ci      } else {
173bea4f105Sopenharmony_ci        Logger.e(TAG, 'rootFolder is null');
174bea4f105Sopenharmony_ci      }
175bea4f105Sopenharmony_ci    } catch (error) {
176bea4f105Sopenharmony_ci      fileList = [];
177bea4f105Sopenharmony_ci      Logger.e(TAG, 'getRootFolder fail, error:' + JSON.stringify(error) + error);
178bea4f105Sopenharmony_ci    }
179bea4f105Sopenharmony_ci    return fileList;
180bea4f105Sopenharmony_ci  }
181bea4f105Sopenharmony_ci}
182bea4f105Sopenharmony_ci
183bea4f105Sopenharmony_ciexport default FileAccessExec;