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