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