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