1/*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import fileAccess from '@ohos.file.fileAccess';
17import { MILLISECOND } from '../../../base/constants/Constant';
18import { FileUtil } from '../../../base/utils/FileUtil';
19import ObjectUtil from '../../../base/utils/ObjectUtil';
20import Logger from '../../../base/log/Logger';
21
22const TAG: string = 'FileBase';
23
24export class FileBase {
25  uri: string;
26  relativePath: string;
27  fileName: string;
28  mode: number;
29  isFolder: boolean;
30  fileSize: number;
31  modifyTime: number;
32  mimeType: string;
33  childCount: number;
34  firstUri: string;
35  subList: Array<FileBase>;
36  currentDir?: string;
37
38  constructor(fileInfo: fileAccess.FileInfo, needChildCount: boolean = true) {
39    if (ObjectUtil.isNullOrUndefined(fileInfo)) {
40      return;
41    }
42    this.uri = fileInfo.uri;
43    this.relativePath = fileInfo.relativePath;
44    this.fileName = fileInfo.fileName;
45    this.fileSize = fileInfo.size;
46    this.mode = fileInfo.mode;
47    this.isFolder = FileUtil.isFolder(this.mode);
48    this.modifyTime = fileInfo.mtime * MILLISECOND.ONE_SECOND;
49    if (this.modifyTime <= 0) {
50      Logger.w(TAG, "The modification time of " + this.fileName + " is " + this.modifyTime);
51    }
52    this.mimeType = fileInfo.mimeType;
53    if (this.isFolder && needChildCount) {
54      this.childCount = FileUtil.getChildCountOfFolder(fileInfo.listFile());
55    }
56    this.currentDir = FileUtil.getCurrentDir(this.relativePath, this.isFolder);
57  }
58}