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 { BYTE } from '../constants/Constant'; 17import LanguageUtil from './LanguageUtil'; 18import { FileMimeTypeUtil } from './FileMimeTypeUtil'; 19import { MimeType } from '../../databases/model/MimeType'; 20import Logger from '../log/Logger'; 21import { FileBase } from '../../databases/model/base/FileBase'; 22import { FilesData } from '../../databases/model/FileData'; 23 24const TAG = 'Tools'; 25 26/** 27 * 格式化显示大小 28 */ 29export const renderSize = (value: string, carry = BYTE.ONE_KB) => { 30 if (!value) { 31 return '0 B'; 32 } 33 let unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; 34 const srcSize = parseFloat(value); 35 let index = Math.floor(Math.log(srcSize) / Math.log(BYTE.ONE_KB)); 36 let size = srcSize / Math.pow(BYTE.ONE_KB, index); 37 if (size >= carry) { 38 size = size / BYTE.ONE_KB; 39 index++; 40 } 41 // 保留的小数位数 42 return size.toFixed(2) + ' ' + unitArr[index]; 43} 44 45/** 46 * @description 截取文件名后缀 文件格式 47 * @param fileName 文件名带后缀 48 */ 49export const formatSuffix = (fileName: string) => { 50 if (!fileName) { 51 return ''; 52 } 53 let newValue = fileName.split('.'); 54 if (newValue[newValue.length - 1].toUpperCase() === FileMimeTypeUtil.SUFFIX_DLP) { 55 newValue.pop(); 56 } 57 return newValue.pop()?.toUpperCase(); 58} 59 60/** 61 * @description 多选框选中状态 62 * @param flag 是否选中 63 */ 64export const getRightIcon = (flag: boolean) => { 65 return flag ? $r('app.media.checkbox_b') : $r('app.media.checkbox_g'); 66} 67 68/** 69 * @description 生成随机id 70 * @param 71 */ 72export const randomId = (): string => { 73 let str: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 74 let res: string = ''; 75 for (let i = 0; i < 6; i++) { 76 // 随机产生字符串的下标 77 let n = parseInt(Math.random() * str.length + ''); 78 res += str[n]; 79 } 80 return res; 81} 82 83/** 84 * @description 获取指定资源ID对应的字符串 85 * @param resource: 指定资源 86 * @return 87 */ 88export function getResourceString(resource: Resource, ...args: (string | number)[]): string { 89 let isString = /%s/; // 字符串类型 90 let isNum = /%d/; // 数字类型 91 let resStr = ''; 92 try { 93 resStr = globalThis.abilityContext.resourceManager.getStringSync(resource.id); 94 } catch (error) { 95 Logger.e(TAG, `getResourceString bundleName: ${globalThis.abilityContext.abilityInfo.bundleName}` + 96 `, abilityName: ${globalThis.abilityContext.abilityInfo.name}`); 97 Logger.e(TAG, `getResourceString error,message: ${error}, Resource:${JSON.stringify(resource)}`); 98 return resStr; 99 } 100 101 if (args.length) { 102 args.forEach(item => { 103 if (typeof item === 'string') { 104 resStr = resStr.replace(isString, item); 105 } else if (typeof item === 'number') { 106 resStr = resStr.replace(isNum, item.toString()); 107 } 108 }) 109 } 110 return resStr; 111} 112 113/** 114 * @description 获取文件夹/文件图标 115 * @param Object<FilesData> 文件对象 116 */ 117export const getFileIcon = (fileName: string, isFolder: boolean = false): MimeType => { 118 if (isFolder) { 119 return new MimeType( 120 '', 121 MimeType.FILE_CATEGORY_UNKNOW, 122 FileMimeTypeUtil.FILE_TYPE_UNKNOW, 123 $r('app.media.hidisk_icon_folder'), 124 $r('app.media.hidisk_icon_folder_grid'), 125 $r('app.media.hidisk_icon_folder_grid'), 126 '' 127 ); 128 } 129 return FileMimeTypeUtil.getFileMimeType(fileName); 130} 131 132/** 133 * @description 实现文件排序,时间倒序 134 * @param dataList: 待排序的文件列表 135 */ 136export const sortDataByTime = (dataList: FilesData[]): FilesData[] => { 137 // 按照时间排序 138 // 规避@State修饰的数组变量执行sort方法不生效问题 139 // const fileList: FilesData[] = dataList.filter(((item: bool) => {return item}); 140 return [...dataList].sort((a, b): number => { 141 if (b.mtime !== a.mtime) { 142 return b.mtime - a.mtime; 143 } else { 144 return compareStr(a.fileName, b.fileName); 145 } 146 }) 147} 148 149function compareStr(str1: string, str2: string) { 150 const language = LanguageUtil.getSystemLanguage(); 151 return str2.localeCompare(str1, language); 152} 153 154export const gridName = (fileName: string): string => { 155 // 文件名超长是中间部分'...'显示 156 const MAX_LENGTH = 11; 157 if (fileName.length > MAX_LENGTH) { 158 return fileName.slice(0, 6) + '...' + fileName.slice(-5); 159 } else { 160 return fileName; 161 } 162} 163 164/** 165 * @description 获取当前文件是否是DLP文件 166 * @param value: 文件名 167 * @result true/false 168 */ 169export const isDlpFile = (value: string): boolean => { 170 let newValue = value.split('.'); 171 if (newValue.pop()?.toUpperCase() === 'DLP') { 172 return true; 173 } 174 return false; 175} 176 177 178export const sortBaseDataByOrderTime = (dataList: Array<FileBase>, isDesc: boolean = false) => { 179 // 规避@State修饰的数组变量执行sort方法不生效问题 180 const fileList = dataList.filter(item => item); 181 return fileList.sort((a, b) => { 182 if (b.modifyTime !== a.modifyTime) { 183 return isDesc ? b.modifyTime - a.modifyTime : a.modifyTime - b.modifyTime; 184 } else { 185 const language = LanguageUtil.getSystemLanguage(); 186 return isDesc ? b.fileName.localeCompare(a.fileName, language) : a.fileName.localeCompare(b.fileName, language); 187 } 188 }) 189}