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 { 17 BigDataConstants, 18 BrowserDataFactory, 19 Log, 20 MediaItem, 21 ReportToBigDataUtil, 22 UserFileManagerAccess 23} from '@ohos/common'; 24import { ImageFilterStack } from './ImageFilterStack'; 25import image from '@ohos.multimedia.image'; 26import fileIO from '@ohos.fileio'; 27import { DateUtil } from './utils/DateUtil'; 28import { Loader } from './Loader'; 29import userFileManager from '@ohos.filemanagement.userFileManager'; 30 31const TAG: string = 'editor_Save'; 32 33export class Save { 34 private static readonly QUALITY_100: number = 100; 35 36 constructor() { 37 } 38 39 public static async save(item: MediaItem, optStack: ImageFilterStack, isReplace: Boolean, 40 callback: Function): Promise<void> { 41 Log.info(TAG, `${JSON.stringify(item)} ${isReplace}`); 42 try { 43 let wrapper = await Loader.loadPixelMapWrapper(item); 44 wrapper = optStack.apply(wrapper); 45 Log.debug(TAG, 'Edit and restore operation execution end.'); 46 47 let fileAsset = await this.createFileAsset(item.uri, isReplace); 48 Log.info(TAG, `create fileAsset succeed: [${fileAsset}]`); 49 let fd = await UserFileManagerAccess.getInstance().openAsset('RW', fileAsset); 50 if (fd < 0) { 51 Log.error(TAG, 'open asset failed.'); 52 return; 53 } 54 55 let options = { 56 format: 'image/jpeg', 57 quality: Save.QUALITY_100 58 }; 59 let packer = image.createImagePacker(); 60 let buffer = await packer.packing(wrapper.pixelMap, options); 61 Log.info(TAG, 'Format pixelMap data to jpg data end.'); 62 63 await fileIO.write(fd, buffer); 64 Log.info(TAG, 'write jpg file end.'); 65 let newUri = fileAsset.uri; 66 await UserFileManagerAccess.getInstance().closeAsset(fd, fileAsset); 67 wrapper && wrapper.release(); 68 await packer.release(); 69 Log.info(TAG, `New saved file: ${newUri}`); 70 callback && callback(newUri); 71 } catch (e) { 72 Log.error(TAG, `save catch error: ${JSON.stringify(e)}`); 73 let msg = { 74 'CatchError': JSON.stringify(e) 75 } 76 ReportToBigDataUtil.errEventReport(BigDataConstants.PHOTO_EDIT_SAVE_ERROR_ID, msg); 77 callback && callback(undefined); 78 } 79 ; 80 } 81 82 private static async createFileAsset(uri: string, isReplace: Boolean) { 83 let dataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_PHOTO); 84 let fileAsset = await dataImpl.getDataByUri(uri); 85 86 if (!fileAsset) { 87 Log.error(TAG, 'get file error'); 88 return null; 89 } 90 let title = DateUtil.nameByDate(isReplace, fileAsset.displayName); 91 if (null == title) { 92 Log.error(TAG, `create picture name failed.`); 93 return null; 94 } 95 let displayName = title + '.jpg'; 96 Log.debug(TAG, `file displayname = ${displayName}`); 97 let favorite: boolean = false; 98 if (isReplace) { 99 favorite = Boolean(await fileAsset.get(userFileManager.ImageVideoKey.FAVORITE.toString())); 100 await UserFileManagerAccess.getInstance().deleteToTrash(uri); 101 Log.debug(TAG, `trash picture file uri ${uri} end.`); 102 } 103 fileAsset = await UserFileManagerAccess.getInstance().createAsset(fileAsset.mediaType, displayName); 104 await fileAsset.favorite(favorite); 105 return fileAsset; 106 } 107}