• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 { HiLog } from '../HiLog';
17import FileUtil from '../external/FileUtil';
18import { dlpPermission } from '@kit.DataProtectionKit';
19import fs from '@ohos.file.fs';
20
21const TAG = 'FileUtils';
22
23export interface FileMsg {
24  fileName: string;
25  filePath: string;
26  fileType: string;
27}
28
29export default class FileUtils {
30  static getSuffixFileMsgByUri(uri: string): FileMsg {
31    let strArray: string[] = uri.split('/');
32    let len: number = strArray.length;
33    let fileName: string = strArray[len - 1];
34    let filePath: string = strArray.slice(0, len - 1).join('/');
35    let pointIndex: number = fileName.lastIndexOf('.');
36    if (pointIndex < 0) {
37      pointIndex = fileName.length;
38    }
39    let fileType: string = fileName.slice(pointIndex, fileName.length);
40    let result: FileMsg = {
41      fileName: fileName.slice(0, pointIndex),
42      filePath: filePath,
43      fileType: fileType,
44    };
45    return result;
46  }
47
48  static getAllSuffixByUri(uri: string): FileMsg {
49    let strArray: string[] = uri.split('/');
50    let len: number = strArray.length;
51    let fileName: string = strArray[len - 1];
52    let filePath: string = strArray.slice(0, len - 1).join('/');
53    let lastIndex: number = fileName.lastIndexOf('.');
54    let secondIndex: number = fileName.lastIndexOf('.', lastIndex - 1);
55    let fileType: string = fileName.substring(secondIndex + 1, lastIndex);
56    let result: FileMsg = {
57      fileName: fileName.substring(0, secondIndex),
58      filePath: filePath,
59      fileType: fileType,
60    };
61    return result;
62  }
63
64  static getFileMsgByFileName(fileName: string): string {
65    let lastIndex: number = fileName.lastIndexOf('.');
66    let secondIndex: number = fileName.lastIndexOf('.', lastIndex - 1);
67    fileName = fileName.substring(0, secondIndex);
68    return fileName;
69  }
70
71  static isDLPFile(uri: string) {
72    return new Promise<boolean>(async (resolve, reject) => {
73      let file: fs.File | undefined;
74      try {
75        file = fs.openSync(uri);
76        try {
77          let res = await dlpPermission.isDLPFile(file.fd);
78          resolve(res);
79        } catch (err) {
80          HiLog.error(TAG, `isDLPFile error: ${JSON.stringify(err)}`);
81          reject(err);
82        }
83      } catch (err) {
84        HiLog.error(TAG, `openSync error: ${JSON.stringify(err)}`);
85        reject(err);
86      } finally {
87        if (file) {
88          FileUtil.closeSync(file);
89        }
90      }
91    })
92  }
93
94  static removeFileTypeFirstDot(str: string) {
95    return str.trim().replace(/^\./, '');
96  }
97
98  static getFileNameByUri(uri: string): string {
99    const strArray: string[] = uri.split('/');
100    return strArray[strArray.length - 1];
101  }
102}