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 { getFileIcon, randomId, formatSuffix } from '../../base/utils/Tools'; 17import { MILLISECOND } from '../../base/constants/Constant'; 18import fileAccess from '@ohos.file.fileAccess'; 19import { ThumbnailSize } from '../../base/constants/UiConstant'; 20import { MimeType } from './MimeType'; 21import { BasicDataSource } from './BasicDataSource'; 22import AbilityCommonUtil, { ResultCodePicker } from '../../base/utils/AbilityCommonUtil'; 23import { FileUtil } from '../../base/utils/FileUtil'; 24import { ArrayUtil } from '../../base/utils/ArrayUtil'; 25import { StartModeOptions } from '../../base/model/StartModeOptions'; 26 27export class BreadData { 28 public title: string = ''; 29 public url: string = ''; 30 public fileIterator?: fileAccess.FileInfo; 31} 32 33export interface TransList { 34 fileList: FilesData[]; 35 folderList: FilesData[] 36} 37 38export class MkDirItem { 39 mkdirName: string = ''; 40 path: string = '' 41} 42 43export class MoveCallBackParams { 44 cancel: boolean = false; 45 fileName: string = ''; 46 selectUri: string = '' 47} 48 49export class FileDataSource extends BasicDataSource { 50 private dataArray: FilesData[] = []; 51 public dataCount: number = 0; 52 53 public totalCount(): number { 54 return this.dataArray.length; 55 } 56 57 public getDataArray(): FilesData[] { 58 return this.dataArray; 59 } 60 61 public getFileList(): FilesData[] { 62 return this.dataArray.filter(item =>!item.isFolder); 63 } 64 65 public setData(data: FilesData[]): void { 66 this.dataArray = [...data]; 67 this.dataCount = this.dataArray.length; 68 this.notifyDataReload(); 69 } 70 71 public getData(index: number): FilesData { 72 return this.dataArray[index]; 73 } 74 75 public selectAll(isSelected: boolean): void { 76 this.dataArray.forEach(item => { 77 item.isChecked = isSelected; 78 }); 79 } 80 81 public getIndex(uri: string): number { 82 return this.dataArray.findIndex(item => item.uri === uri); 83 } 84 85 public getSelectedFileList(): FilesData[] { 86 return this.dataArray.filter(item => item.isChecked); 87 } 88 89 public replaceData(index: number, data: FilesData): void { 90 this.dataArray.splice(index, 1, data); 91 this.notifyDataChange(index); 92 } 93 94 public addData(index: number, data: FilesData): void { 95 this.dataArray.splice(index, 0, data); 96 this.dataCount = this.dataArray.length; 97 this.notifyDataAdd(index); 98 } 99 100 public pushData(data: FilesData): void { 101 this.dataArray.push(data); 102 this.dataCount = this.dataArray.length; 103 this.notifyDataAdd(this.dataArray.length - 1); 104 } 105 106 public deleteData(index: number): void { 107 this.dataArray.splice(index, 1); 108 this.dataCount = this.dataArray.length; 109 this.notifyDataDelete(index); 110 } 111} 112 113export class FilesData { 114 public id: string = ''; 115 public uri: string = ''; 116 public thumbUri: string = ''; 117 public displayName: string = ''; 118 public deviceId: string = ''; 119 public flags: number = 0; 120 public name: string =''; 121 public fileName: string = ''; 122 public mode: string = ''; 123 public size: number = 0; 124 public mtime: number = 0; 125 public suffix: string | undefined; 126 public mimeType: string = ''; 127 public icon: ESObject; 128 public gridIcon: ESObject; 129 public localGridIcon: ESObject; 130 public isChecked: boolean = false; 131 public path?: string; 132 public relativePath: string = ''; 133 public sub?: number; 134 public scale?: number; 135 public angle?: number; 136 public offsetX?: number | string; 137 public offsetY?: number | string; 138 public picWidth?: number | string; 139 public picHeight?: number | string; 140 public fileIterator?: fileAccess.FileInfo; 141 public isMedia: boolean = false; 142 public isImage: boolean = false; 143 public isVideo: boolean = false; 144 public isAudio: boolean = false; 145 public isFolder: boolean = false; 146 public duration: number = 0; 147 public mimeTypeObj: MimeType | undefined = undefined; 148 public subFolderList: FilesData[] = []; 149 public subFileList: FilesData[] = []; 150 public layer: number = 0; 151 public autoShow: boolean = false; 152 public currentDir?: string; 153 154 constructor(obj?: FilesData) { 155 if (obj === undefined) { 156 return; 157 } 158 this.isFolder = obj.isFolder || FileUtil.isFolder(parseInt(obj.mode)); 159 this.id = obj.id || randomId(); 160 this.uri = obj.uri || ''; 161 this.deviceId = obj.deviceId || ''; 162 this.flags = obj.flags || 0; 163 this.name = obj.name || ''; 164 this.fileName = obj.fileName || obj.displayName || ''; 165 if (formatSuffix(this.fileName) !== undefined) { 166 this.suffix = formatSuffix(this.fileName); 167 } 168 this.mode = obj.mode || ''; 169 this.size = obj.size || 0; 170 this.mtime = obj.mtime * MILLISECOND.ONE_SECOND || 0; 171 this.mimeType = obj.mimeType || ''; 172 this.isChecked = false; 173 this.path = obj.path || obj.relativePath || ''; 174 this.sub = obj.sub || 0; 175 this.scale = obj.scale || 1; 176 this.angle = obj.angle || 0; 177 this.offsetX = obj.offsetX || 0; 178 this.offsetY = obj.offsetY || 0; 179 this.picWidth = obj.picWidth || '100%'; 180 this.picHeight = obj.picHeight || '100%'; 181 this.fileIterator = obj.fileIterator; 182 this.duration = obj.duration || 0; 183 this.mimeTypeObj = getFileIcon(this.fileName, this.isFolder); 184 this.icon = this.mimeTypeObj?.getResID(); 185 this.gridIcon = this.mimeTypeObj?.getGridResID(); 186 this.localGridIcon = this.mimeTypeObj?.getLocalGridResID(); 187 if (this.mimeTypeObj?.isMedia()) { 188 this.thumbUri = this.uri; 189 } 190 if (this.isFolder && this.fileIterator !== undefined) { 191 this.sub = getSubFileNum(this.fileIterator); 192 } 193 this.currentDir = FileUtil.getCurrentDir(this.path, this.isFolder); 194 } 195 196 setFileName(fileName: string): void { 197 this.fileName = fileName; 198 this.mimeTypeObj = getFileIcon(this.fileName); 199 this.icon = this.mimeTypeObj?.getResID(); 200 this.gridIcon = this.mimeTypeObj?.getGridResID(); 201 this.localGridIcon = this.mimeTypeObj?.getLocalGridResID(); 202 if (this.mimeTypeObj?.isMedia()) { 203 this.thumbUri = `${this.uri}/thumbnail/${ThumbnailSize.WIDTH}/${ThumbnailSize.HEIGHT}`; 204 } 205 } 206 207 pickFile(startModeOptions: StartModeOptions): void { 208 AbilityCommonUtil.terminateFilePicker([this.uri], ResultCodePicker.SUCCESS, startModeOptions); 209 } 210 211 setSubFolderList(subFolderList: FilesData[]) { 212 this.subFolderList = subFolderList; 213 } 214 215 setSubList(subList: FilesData[]) { 216 if (!ArrayUtil.isEmpty(subList)) { 217 let folderList: FilesData[] =[]; 218 let fileList: FilesData[] = []; 219 for (let i = 0; i < subList.length; i++) { 220 let fileData: FilesData = subList[i]; 221 if (fileData.isFolder) { 222 folderList.push(fileData); 223 } else { 224 fileList.push(fileData); 225 } 226 } 227 this.subFolderList = folderList; 228 this.subFileList = fileList; 229 } 230 } 231 232 hasSubFolderList(): boolean { 233 if (ArrayUtil.isEmpty(this.subFolderList)) { 234 return false; 235 } 236 return this.subFolderList.length > 0; 237 } 238 239 getSubFolderList(): FilesData[] { 240 return this.subFolderList; 241 } 242 243 setLayer(layer: number) { 244 this.layer = layer; 245 } 246 247 getLayer(): number { 248 if (this.layer) { 249 return this.layer; 250 } 251 return 1; 252 } 253} 254 255export function getSubFileNum(fileInfo: fileAccess.FileInfo): number { 256 let subFileNum = 0; 257 if (!fileInfo) { 258 return subFileNum; 259 } 260 let fileIterator = fileInfo.listFile(); 261 if (!fileIterator) { 262 return subFileNum; 263 } 264 let result = fileIterator.next(); 265 let isDone = result.done; 266 while (!isDone) { 267 subFileNum += 1; 268 result = fileIterator.next(); 269 isDone = result.done; 270 } 271 return subFileNum; 272} 273 274export class SelectedFileData { 275 public id?: string; 276 public uri: string; 277 public mimeType: string; 278 public thumbUri: string; 279 public title: string; 280 public fileName: string; 281 public mode: string; 282 public relativePath: string; 283 public size: number; 284 public albumUri: string; 285 public albumName: string; 286 public isFolder: boolean; 287 public displayName?: string; 288 289 constructor(obj: SelectedFileData) { 290 this.id = obj.id || '-1'; 291 this.uri = obj.uri || ''; 292 this.mimeType = obj.mimeType || ''; 293 this.thumbUri = obj.thumbUri || ''; 294 this.title = obj.title || ''; 295 this.fileName = obj.fileName || obj.displayName || ''; 296 this.mode = obj.mode || ''; 297 this.isFolder = obj.isFolder; 298 this.relativePath = obj.relativePath || ''; 299 this.size = obj.size || 0; 300 this.albumUri = obj.albumUri || ''; 301 this.albumName = obj.albumName || ''; 302 } 303} 304 305export class FileData { 306 public id: string; 307 public fileName: string; 308 public thumbnail: string; 309 public editedTime: string; 310 public subItemSize: number; 311 public parentFolder: string; 312 public fileType: string; 313 public fileSize: number; 314 public isChecked: boolean; 315 public children: []; 316 317 constructor(id: string, fileName: string, thumbnail: string, editedTime: string, subItemSize: number, 318 parentFolder: string, fileType: string, fileSize: number, isChecked: boolean, children: []) { 319 this.id = id; 320 this.fileName = fileName; 321 this.thumbnail = thumbnail; 322 this.editedTime = editedTime; 323 this.subItemSize = subItemSize; 324 this.parentFolder = parentFolder; 325 this.fileType = fileType; 326 this.fileSize = fileSize; 327 this.isChecked = isChecked; 328 this.children = children; 329 } 330} 331 332export class FileDetail { 333 public key: string | Resource; 334 public value: string | Resource; 335 public path?: string = ''; 336 public params?: ESObject; 337 338 constructor(key: string, value: string, path: string, params: ESObject) { 339 this.key = key; 340 this.value = value; 341 this.path = path; 342 this.params = params; 343 } 344} 345 346export class BottomOptions { 347 public optionName: string; 348 public name: string; 349 public icon: Resource; 350 public disabled: boolean = false; 351 public display: boolean = true; 352 353 constructor(optionName: string, name: string, icon: Resource, disabled: boolean, display: boolean) { 354 this.optionName = optionName; 355 this.name = name; 356 this.icon = icon; 357 this.disabled = disabled; 358 this.display = display; 359 } 360}