1/* 2 * Copyright (c) 2022-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 type { BrowserOperationInterface, CreateParam } from '../../../interface/BrowserOperationInterface'; 17import { Log } from '../../../utils/Log'; 18import type { FileAsset } from '../../../access/UserFileManagerAccess'; 19import { Album, UserFileManagerAccess } from '../../../access/UserFileManagerAccess'; 20import fileio from '@ohos.fileio'; 21import { AlbumDefine } from '../AlbumDefine'; 22import { StringUtil } from '../../../utils/StringUtil'; 23 24const TAG: string = 'common_OperationImpl'; 25 26export class OperationImpl implements BrowserOperationInterface { 27 async favor(uri: string, isFavor: boolean): Promise<boolean> { 28 Log.info(TAG, `favor, id ${uri}`); 29 try { 30 let fileAsset = (await UserFileManagerAccess.getInstance().getFirstObject(AlbumDefine.getFileFetchOptByUri(uri))).obj 31 fileAsset.favorite(isFavor); 32 return true; 33 } catch (e) { 34 Log.error(TAG, `favor error ${e}`); 35 return false; 36 } 37 } 38 39 async delete(uri: string): Promise<void> { 40 await UserFileManagerAccess.getInstance().deleteToTrash(uri); 41 } 42 43 async deleteTrash(assets: Array<FileAsset>): Promise<void> { 44 await UserFileManagerAccess.getInstance().deleteFromTrash(assets); 45 } 46 47 async recoverFromTrash(assets: Array<FileAsset>): Promise<void> { 48 await UserFileManagerAccess.getInstance().recoverFromTrash(assets); 49 } 50 51 async copy(source: FileAsset, target: FileAsset): Promise<void> { 52 Log.info(TAG, `copy start: src:${source.uri} target: ${target.uri}`); 53 54 let fd = await UserFileManagerAccess.getInstance().openAsset('R', source); 55 if (fd <= 0) { 56 throw 'fd is invalid' 57 return; 58 } 59 60 let targetFd = await UserFileManagerAccess.getInstance().openAsset('RW', target); 61 if (targetFd <= 0) { 62 throw 'targetFd is invalid' 63 return; 64 } 65 66 await this.readAndWriteData(fd, targetFd); 67 68 await source.close(fd); 69 await target.close(targetFd); 70 71 Log.debug(TAG, 'copy end') 72 } 73 74 async trash(uri: string, isTrash: boolean): Promise<void> { 75 Log.debug(TAG, `trash start ${JSON.stringify(uri)}`); 76 77 let fileAsset; 78 if (isTrash) { 79 fileAsset = (await UserFileManagerAccess.getInstance().getFirstObject(AlbumDefine.getFileFetchOptByUri(uri))).obj; 80 } else { 81 fileAsset = (await UserFileManagerAccess.getInstance().getTrashObject(AlbumDefine.getFileFetchOptByUri(uri)))[0]; 82 } 83 await this.delete(fileAsset.uri); 84 Log.debug(TAG, `trash end: ${isTrash}`); 85 } 86 87 async remove(uris: Array<string>, albumUri: string): Promise<void> { 88 Log.debug(TAG, `remove start ${JSON.stringify(uris)} from ${JSON.stringify(albumUri)}`); 89 let album: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri); 90 if (album) { 91 let fileAssets: Array<FileAsset> = new Array<FileAsset>(); 92 for (let i = 0; i < uris.length; i++) { 93 let fileAsset = (await UserFileManagerAccess.getInstance().getFirstObject(AlbumDefine.getFileFetchOptByUri(uris[i]))).obj; 94 if (fileAsset) { 95 fileAssets.push(fileAsset); 96 } 97 } 98 if (fileAssets.length > 0) { 99 await album.removePhotoAssets(fileAssets); 100 } 101 } 102 Log.debug(TAG, `remove end`); 103 } 104 105 async create(param: CreateParam): Promise<FileAsset> { 106 return await UserFileManagerAccess.getInstance().createAsset(param.fileType, param.name); 107 } 108 109 async change(file: FileAsset): Promise<void> { 110 await file.commitModify(); 111 } 112 113 setName(source: FileAsset, name: string): void { 114 let displayName = source.displayName; 115 let index = displayName.lastIndexOf('.'); 116 displayName = name + displayName.slice(index); 117 118 source.displayName = displayName; 119 120 Log.info(TAG, `setName title: ${name}, displayName: ${displayName}`); 121 } 122 123 async readAndWriteData(srcFd: number, targetFd: number) { 124 Log.debug(TAG, 'readAndWriteData start!') 125 let stat = await fileio.fstat(srcFd); 126 Log.debug(TAG, `readAndWriteData read stat.size ${stat.size}`) 127 if (stat.size == 0) { 128 return; 129 } 130 let step = 10000000; 131 let last = stat.size % step; 132 let count = (stat.size - last) / step; 133 if (last > 0) { 134 count = count + 1; 135 } 136 Log.debug(TAG, `readAndWriteData read count ${count} last ${last}`) 137 138 for (let i = 0; i < count; i++) { 139 let rwSize = 0; 140 if (i == (count - 1)) { 141 rwSize = last; 142 } else { 143 rwSize = step; 144 } 145 let buf = new ArrayBuffer(rwSize); 146 let readOptions = { 147 offset: 0, 148 length: rwSize, 149 position: i * step 150 } 151 await fileio.read(srcFd, buf, readOptions); 152 let writeOptions = { 153 offset: 0, 154 length: rwSize, 155 position: i * step, 156 encoding: 'utf-8' 157 } 158 await fileio.write(targetFd, buf, writeOptions); 159 } 160 Log.debug(TAG, 'readAndWriteData end!') 161 } 162}