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 { AlbumDefine } from '../AlbumDefine'; 17import type { QueryParam } from '../BrowserDataImpl'; 18import { BrowserDataImpl } from '../BrowserDataImpl'; 19import { Log } from '../../../utils/Log'; 20import type { AsyncCallback } from '../../common/AsyncCallback'; 21import { MediaItem } from './MediaItem'; 22import type { FileAsset } from '../../../access/UserFileManagerAccess'; 23import { UserFileManagerAccess } from '../../../access/UserFileManagerAccess'; 24 25const TAG: string = 'common_PhotoDataImpl'; 26 27export class PhotoDataImpl extends BrowserDataImpl { 28 29 constructor() { 30 super() 31 } 32 33 async getDataByUri(uri: string): Promise<FileAsset> { 34 Log.debug(TAG, 'getDataByUri'); 35 try { 36 let result = await UserFileManagerAccess.getInstance().getFirstObject(AlbumDefine.getFileFetchOptByUri(uri)); 37 return result.obj; 38 } catch (error) { 39 Log.error(TAG, 'getDataByUri error'); 40 return null; 41 } 42 } 43 44 getData(callback: AsyncCallback<MediaItem[]>, param: QueryParam): void { 45 Log.info(TAG, `getMediaItem start ${JSON.stringify(param)}`); 46 if (callback == null) { 47 Log.error(TAG, 'getMediaItem, param or callback is null, return!'); 48 return; 49 } 50 51 // Querying MediaLibrary data 52 let mediaItemList: MediaItem[] = []; 53 this.getItems(param.albumUri, param.start, param.count, param?.filterMediaType).then(async (dataList: Array<FileAsset>) => { 54 Log.info(TAG, 'getMediaItem coming'); 55 if (dataList) { 56 for (let item of dataList) { 57 try { 58 let mediaItem: MediaItem = new MediaItem(item); 59 mediaItem.setThumbnail(this.getThumbnailSafe(mediaItem.uri, mediaItem.path)); 60 mediaItemList.push(mediaItem); 61 } catch (err) { 62 Log.error(TAG, `getMediaItem error: ${err}`); 63 } 64 } 65 Log.info(TAG, `getMediaItem item size: ${mediaItemList.length}`); 66 callback.callback(mediaItemList, param.albumUri); 67 } 68 }); 69 } 70 71 getFileAssets(callback: AsyncCallback<FileAsset[]>, param: QueryParam): void { 72 Log.info(TAG, `getFileAssets start ${JSON.stringify(param)}`); 73 if (callback == null) { 74 Log.error(TAG, 'getFileAssets, param or callback is null, return!'); 75 return; 76 } 77 this.getItems(param.albumUri, param.start, param.count).then((fileAssets) => { 78 if (!fileAssets) { 79 Log.warn(TAG, 'Get fileAssets failed or the result is empty.'); 80 return; 81 } 82 callback.callback(fileAssets); 83 }) 84 } 85 86 getDataCount(callback: AsyncCallback<number>, param: QueryParam): void { 87 Log.debug(TAG, `getMediaItemCount: ${JSON.stringify(param)}`); 88 this.getItemsCount(param.albumUri, param?.filterMediaType).then((count) => { 89 Log.debug(TAG, `getMediaItemCount callback: ${count}`); 90 callback.callback(count, param.albumUri); 91 }); 92 } 93 94 getDataIndexByUri(callback: AsyncCallback<number>, param: QueryParam, uri: string): void { 95 Log.debug(TAG, `getDataIndexByUri: ${uri}, ${JSON.stringify(param)}`); 96 this.getItemIndexByUri(uri, param.albumUri).then((index) => { 97 Log.debug(TAG, `getItemIndexByUri callback: ${index}`); 98 callback.callback(index); 99 }); 100 } 101 102 getThumbnail(sourceUri: string, path: string, size?): string { 103 return this.getThumbnailSafe(sourceUri, path, size); 104 } 105 106 async getDataByName(name: string, albumUri: string): Promise<FileAsset> { 107 Log.debug(TAG, 'getDataByName name:' + JSON.stringify(name) + '-' + JSON.stringify(albumUri)); 108 109 let dataList; 110 if (albumUri) { 111 dataList = await UserFileManagerAccess.getInstance().getUserAlbumObject(AlbumDefine.getAlbumFetchOptByUri(albumUri), 112 AlbumDefine.getFileFetchOptByName(name)) 113 } else { 114 dataList = await UserFileManagerAccess.getInstance().getAllObject(AlbumDefine.getFileFetchOptByName(name)) 115 } 116 117 if (dataList != null) { 118 if (dataList.length > 0) { 119 return dataList[0]; 120 } else { 121 Log.info(TAG, 'fileAsset is null'); 122 return null; 123 } 124 } 125 Log.debug(TAG, 'getFileAsset end'); 126 return null; 127 } 128}