1/* 2 * Copyright (c) 2022 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 {LogUtil} from './LogUtil' 17import {MediaLibraryAccess} from '../access/MediaLibraryAccess'; 18import fileio from '@ohos.fileio'; 19 20const TAG = "OperationUtils" 21export class OperationUtils { 22 23 24 async copy(uri: string):Promise<string> { 25 try { 26 let numId = this.getIdByUri(uri) 27 var context = globalThis.noteContext 28 let fileAsset = await this.getFileAssetById(numId,context); 29 if (fileAsset == null || fileAsset == undefined) { 30 LogUtil.warn(TAG, "Failed to get fileAsset") 31 return; 32 } 33 34 let fd = await MediaLibraryAccess.openAsset('RW', fileAsset); 35 if (fd <= 0) { 36 LogUtil.warn(TAG, "fd is invaild") 37 return; 38 } 39 LogUtil.info(TAG, 'copyAsset read end!') 40 41 // 通过Uri拷贝图片到指定指定的包路径下 42 // 1、获取本地路径 43 var dir = context.filesDir 44 // 2、生成本地文件名 45 var time = new Date().getTime() 46 var imagePath = dir + "/" + time.toString() + "_note" + ".jpg" 47 // 3、拷贝 48 await fileio.copyFile(fd, imagePath) 49 // 4、关闭安fd,Asset 50 await MediaLibraryAccess.closeAsset(fd, fileAsset); 51 } catch (error) { 52 LogUtil.info(TAG, "create error: " + error); 53 return; 54 } 55 LogUtil.debug(TAG, 'copy end'); 56 57 return imagePath.toString() 58 } 59 60 saveImageData(dataArray: Uint8Array, imageType: string): string { 61 try { 62 var context = globalThis.noteContext 63 // 1、获取本地路径 64 var dir = context.filesDir 65 var time = new Date().getTime() 66 var imagePath = dir + "/" + time.toString() + "_note" + "." + imageType 67 let fd = fileio.openSync(imagePath, 0o100 | 0o2, 0o666) 68 // 3、把image数据写入本地图片文件中 69 fileio.writeSync(fd, dataArray.buffer) 70 } catch (error) { 71 LogUtil.info(TAG, "create error: " + error); 72 return; 73 } 74 LogUtil.info(TAG, 'save image end'); 75 76 return imagePath.toString() 77 } 78 79 saveImage(imageData: string, imageType: string): string { 80 try { 81 var context = globalThis.noteContext 82 // 1、获取本地路径 83 var dir = context.filesDir 84 // 2、生成本地文件名 85 var time = new Date().getTime() 86 var imagePath = dir + "/" + time.toString() + "_note" + "." + imageType 87 let fd = fileio.openSync(imagePath, 0o100 | 0o2, 0o666) 88 let arrayBuffer = new ArrayBuffer(imageData.length) 89 let ia = new Uint8Array(arrayBuffer) 90 for(let i = 0; i < imageData.length; i++) { 91 ia[i] = imageData.charCodeAt(i) 92 } 93 94 // 3、把image数据写入本地图片文件中 95 fileio.writeSync(fd, ia.buffer) 96 97 } catch (error) { 98 LogUtil.info(TAG, "create error: " + error); 99 return; 100 } 101 LogUtil.debug(TAG, 'save image end'); 102 103 return imagePath.toString() 104 } 105 106 getIdByUri(uri:string): number { 107 let srcIndex = uri.lastIndexOf('/'); 108 let srcEnd = uri.length; 109 let srcId = uri.substring(srcIndex + 1, srcEnd); 110 let numId = new Number(srcId); 111 LogUtil.debug(TAG, `getIdByUri numId: ${numId}`); 112 return numId.valueOf(); 113 } 114 115 async getFileAssetById(id,context){ 116 LogUtil.info(TAG, 'getFileAssetById', id); 117 let fetchOpt = { 118 selections : 'file_id =?', 119 selectionArgs : [`${id}`], 120 } 121 122 try { 123 let result = await MediaLibraryAccess.getFirstObject(fetchOpt,context); 124 if (result == null) { 125 LogUtil.info(TAG, 'getFileAssetByUri fail'); 126 return null; 127 } 128 return result.obj; 129 } catch (error) { 130 LogUtil.info(TAG, 'getFileAssetById error'); 131 return null; 132 } 133 } 134} 135 136let mOperationUtils = new OperationUtils(); 137export default mOperationUtils as OperationUtils; 138