1df226684Sopenharmony_ci/* 2df226684Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3df226684Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4df226684Sopenharmony_ci * you may not use this file except in compliance with the License. 5df226684Sopenharmony_ci * You may obtain a copy of the License at 6df226684Sopenharmony_ci * 7df226684Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8df226684Sopenharmony_ci * 9df226684Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10df226684Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11df226684Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12df226684Sopenharmony_ci * See the License for the specific language governing permissions and 13df226684Sopenharmony_ci * limitations under the License. 14df226684Sopenharmony_ci */ 15df226684Sopenharmony_ci 16df226684Sopenharmony_ciimport dlpPermission from '@ohos.dlpPermission'; 17df226684Sopenharmony_ciimport fs from '@ohos.file.fs'; 18df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog'; 19df226684Sopenharmony_ci 20df226684Sopenharmony_ciconst TAG = 'FileUtils'; 21df226684Sopenharmony_ciexport interface FileMsg { 22df226684Sopenharmony_ci fileName: string; 23df226684Sopenharmony_ci filePath: string; 24df226684Sopenharmony_ci fileType: string; 25df226684Sopenharmony_ci} 26df226684Sopenharmony_ci 27df226684Sopenharmony_ciexport default class FileUtils { 28df226684Sopenharmony_ci static getSuffixFileMsgByUri(uri: string): FileMsg { 29df226684Sopenharmony_ci let strArray: string[] = uri.split('/'); 30df226684Sopenharmony_ci let len: number = strArray.length; 31df226684Sopenharmony_ci let fileName: string = strArray[len - 1]; 32df226684Sopenharmony_ci let filePath: string = strArray.slice(0, len - 1).join('/'); 33df226684Sopenharmony_ci let pointIndex: number = fileName.lastIndexOf('.'); 34df226684Sopenharmony_ci if (pointIndex < 0) { 35df226684Sopenharmony_ci pointIndex = fileName.length; 36df226684Sopenharmony_ci } 37df226684Sopenharmony_ci let fileType: string = fileName.slice(pointIndex, fileName.length); 38df226684Sopenharmony_ci let result: FileMsg = { 39df226684Sopenharmony_ci fileName: fileName.slice(0, pointIndex), 40df226684Sopenharmony_ci filePath: filePath, 41df226684Sopenharmony_ci fileType: fileType, 42df226684Sopenharmony_ci }; 43df226684Sopenharmony_ci return result; 44df226684Sopenharmony_ci } 45df226684Sopenharmony_ci 46df226684Sopenharmony_ci static getAllSuffixByUri(uri: string): FileMsg { 47df226684Sopenharmony_ci let strArray: string[] = uri.split('/'); 48df226684Sopenharmony_ci let len: number = strArray.length; 49df226684Sopenharmony_ci let fileName: string = strArray[len - 1]; 50df226684Sopenharmony_ci let filePath: string = strArray.slice(0, len - 1).join('/'); 51df226684Sopenharmony_ci let lastIndex: number = fileName.lastIndexOf('.'); 52df226684Sopenharmony_ci let secondIndex: number = fileName.lastIndexOf('.', lastIndex - 1); 53df226684Sopenharmony_ci let fileType: string = fileName.substring(secondIndex + 1, lastIndex); 54df226684Sopenharmony_ci let result: FileMsg = { 55df226684Sopenharmony_ci fileName: fileName.substring(0, secondIndex), 56df226684Sopenharmony_ci filePath: filePath, 57df226684Sopenharmony_ci fileType: fileType, 58df226684Sopenharmony_ci }; 59df226684Sopenharmony_ci return result; 60df226684Sopenharmony_ci } 61df226684Sopenharmony_ci 62df226684Sopenharmony_ci static getFileMsgByFileName(fileName: string): string { 63df226684Sopenharmony_ci let lastIndex: number = fileName.lastIndexOf('.'); 64df226684Sopenharmony_ci let secondIndex: number = fileName.lastIndexOf('.', lastIndex - 1); 65df226684Sopenharmony_ci fileName = fileName.substring(0, secondIndex); 66df226684Sopenharmony_ci return fileName; 67df226684Sopenharmony_ci } 68df226684Sopenharmony_ci 69df226684Sopenharmony_ci static isDLPFile(uri: string) { 70df226684Sopenharmony_ci return new Promise<boolean>(async (resolve, reject) => { 71df226684Sopenharmony_ci let file: fs.File | undefined; 72df226684Sopenharmony_ci try { 73df226684Sopenharmony_ci file = fs.openSync(uri); 74df226684Sopenharmony_ci try { 75df226684Sopenharmony_ci let res = await dlpPermission.isDLPFile(file.fd); 76df226684Sopenharmony_ci resolve(res); 77df226684Sopenharmony_ci } catch (err) { 78df226684Sopenharmony_ci HiLog.error(TAG, `isDLPFile error: ${JSON.stringify(err)}`); 79df226684Sopenharmony_ci reject(err); 80df226684Sopenharmony_ci } 81df226684Sopenharmony_ci } catch (err) { 82df226684Sopenharmony_ci HiLog.error(TAG, `openSync error: ${JSON.stringify(err)}`); 83df226684Sopenharmony_ci reject(err); 84df226684Sopenharmony_ci } finally { 85df226684Sopenharmony_ci if (file) { 86df226684Sopenharmony_ci fs.closeSync(file); 87df226684Sopenharmony_ci } 88df226684Sopenharmony_ci } 89df226684Sopenharmony_ci }) 90df226684Sopenharmony_ci } 91df226684Sopenharmony_ci}