• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { toast } from '../../base/utils/Common'
17import { FileMimeTypeUtil } from '../../base/utils/FileMimeTypeUtil'
18import { FILE_SUFFIX, SELECT_MODE } from '../constants/Constant';
19import Logger from '../log/Logger';
20import ObjectUtil from './ObjectUtil';
21
22/**
23 * 文件选择器文件状态
24 *
25 * @param item 文件对象
26 * @param checkedNum 选中数量
27 * @return 是否超限  选择类型是否不匹配
28 */
29export function pickerStatus(item, checkedNum) {
30  return {
31    // 选择是否超限
32    exceedLimit: globalThis.filePickerViewFlag && (checkedNum >= globalThis.filePickNum && !item.isChecked),
33    // 选择类型是否不匹配
34    differentTypes: !checkFileSelectable(item)
35  }
36}
37
38/**
39 * 根据文件后缀判断文件是否可选
40 * @param item
41 */
42function checkFileSelectable(item): boolean {
43  // 非文件选择器场景
44  if (!globalThis.filePickerViewFlag) {
45    return true;
46  }
47
48  // selectMode检查
49  let selectMode: number = globalThis.keySelectMode;
50  let isFolder = false;
51  if (ObjectUtil.hasKey(item, 'isFolder')) {
52    isFolder = item.isFolder;
53  }
54  // 文件夹模式,直接返回
55  if(selectMode === SELECT_MODE.FOLDER){
56    return isFolder;
57  }
58
59  if (isFolder) {
60    // 混选模式下,文件夹直接返回
61    if (selectMode === SELECT_MODE.MIX) {
62      return true;
63    }
64    // 文件模式下,文件夹直接返回false
65    return false;
66  }
67  // 后缀检查
68  let keyFileSuffixFilter: Array<string> = globalThis.keyFileSuffixFilter;
69  if (Array.isArray(keyFileSuffixFilter) && keyFileSuffixFilter.length > 0) {
70    return checkFileSuffix(item.fileName, keyFileSuffixFilter);
71  }
72
73  // mimeType检查
74  return checkFileMimetype(item.fileName);
75}
76
77/**
78 * 校验选中的文件后缀
79 *
80 * @param fileName 文件名称
81 * @param keyFileSuffixFilter 指定后缀
82 * @return 如果文件后缀满足三方指定,则返回true
83 */
84function checkFileSuffix(fileName: string, keyFileSuffixFilter: Array<string>): boolean {
85  if (keyFileSuffixFilter) {
86    if (fileName) {
87      const suffix = FILE_SUFFIX.SUFFIX_START + FileMimeTypeUtil.getFileSuffix(fileName);
88      if (keyFileSuffixFilter.includes(suffix)) {
89        return true;
90      }
91    }
92    return false;
93  }
94  return true;
95}
96
97/**
98 * 校验选中的文件mimetype
99 *
100 * @param fileName 文件名称
101 * @return 条件满足返回true
102 */
103function checkFileMimetype(fileName: string): boolean {
104  if (!fileName) {
105    return false
106  }
107  let keyPickTypeList: Array<string> = globalThis.keyPickTypeList
108  // 输入的类型全转换成小写,避免大小敏感问题
109  keyPickTypeList.forEach(item => item.toLowerCase())
110  // 类型列表为空或包含*或*/*时,可选择所有文件
111  if (!keyPickTypeList || keyPickTypeList.length === 0 || keyPickTypeList.includes('*') || keyPickTypeList.includes('*/*')) {
112    return true
113  }
114
115  const mimeTypeObj = FileMimeTypeUtil.getFileMimeType(fileName)
116  const mimeType = mimeTypeObj.getMimeType()
117
118  // mimeType未知不可选
119  if (!mimeType) {
120    return false
121  }
122
123  // mimeType完全匹配
124  if (keyPickTypeList.includes(mimeType)) {
125    return true
126  }
127
128  let fileCategory = mimeType
129  const index = mimeType.indexOf('/')
130  if (index > 0) {
131    fileCategory = mimeType.substring(0, index)
132  }
133  // 某一类文件
134  if (keyPickTypeList.includes(fileCategory) || keyPickTypeList.includes(`${fileCategory}/*`)) {
135    return true
136  }
137
138  return false
139}
140
141/**
142 * 文件选择器 选择超限提示
143 *
144 * @param isImmersion 是否沉浸式
145 */
146export const filePickerTip = () => {
147  globalThis.abilityContext.resourceManager.getPluralString($r('app.plural.filePickerTip').id, globalThis.filePickNum).then((value) => {
148    toast(value)
149  })
150}
151
152
153