• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 */
15import type { BrowserDataInterface } from '../../interface/BrowserDataInterface';
16import { AlbumDefine } from './AlbumDefine';
17import { Log } from '../../utils/Log';
18import type { FileAsset, FileAssetEx } from '../../access/UserFileManagerAccess';
19import { Album, UserFileManagerAccess } from '../../access/UserFileManagerAccess';
20import type { AsyncCallback } from '../common/AsyncCallback';
21import { Constants } from '../common/Constants';
22
23const TAG: string = 'BrowserDataImpl';
24
25export type QueryParam = {
26  albumUri: string,
27  start: number,
28  count: number,
29  filterMediaType?: string
30};
31
32export abstract class BrowserDataImpl implements BrowserDataInterface {
33  static readonly THUMBNAIL_WIDTH = 256;
34
35  abstract getData(callback: AsyncCallback<unknown> | Function, param: QueryParam): void;
36
37  abstract getDataCount(callback: AsyncCallback<unknown> | Function, param: unknown): void;
38
39  abstract getDataByUri(uri: unknown): unknown;
40
41  abstract getDataByName(name: string, albumUri: string): unknown;
42
43  abstract getDataIndexByUri(callback: AsyncCallback<unknown> | Function, param: QueryParam, uri: string): void;
44
45  async getAllObject(fetchOpt): Promise<Array<FileAsset>> {
46    Log.debug(TAG, `getAllObject ${fetchOpt}`);
47    let allObject = await UserFileManagerAccess.getInstance().getAllObject(fetchOpt);
48    return allObject;
49  }
50
51  async getCount(fetchOpt): Promise<number> {
52    let count = await UserFileManagerAccess.getInstance().getCount(fetchOpt);
53    return count;
54  }
55
56  async getFirstObject(fetchOpt): Promise<FileAssetEx> {
57    Log.debug(TAG, 'getFirstObject');
58    let firstObject: FileAssetEx = await UserFileManagerAccess.getInstance().getFirstObject(fetchOpt);
59    return firstObject;
60  }
61
62  async getObject(fetchOpt): Promise<FileAsset> {
63    Log.debug(TAG, 'getFirstObject');
64    let object: FileAsset = await UserFileManagerAccess.getInstance().getObject(fetchOpt);
65    return object;
66  }
67
68  async getItems(albumUri?: string, startIndex?: number, count?: number, filterMediaType?: string): Promise<Array<FileAsset>> {
69    let result: Array<FileAsset> = null;
70
71    // albumUri不为空,则从目标相册中获取;否则默认从所有媒体资源中获取
72    if (albumUri) {
73      let album: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri);
74      let fetchOpt = AlbumDefine.getFileFetchOpt(startIndex, count, filterMediaType);
75      if (album) {
76        let fetchResult = await album.getPhotoAssets(fetchOpt);
77        result = await fetchResult.getAllObject();
78        fetchResult.close();
79      }
80    } else {
81      let fetchOpt = AlbumDefine.getFileFetchOpt(startIndex, count, filterMediaType);
82      Log.debug(TAG, `getMediaItem start ${JSON.stringify(fetchOpt)}`);
83      result = await UserFileManagerAccess.getInstance().getAllObject(fetchOpt);
84    }
85    return result;
86  }
87
88  async getItemsCountOfAlbum(album: Album, filterMediaType?: string): Promise<number> {
89    let count = 0;
90    // 当前相册count始终为0,先通过查全部图片获取count
91    let fetchOpt = AlbumDefine.getFileFetchOptWithEmptyColumn(Constants.INVALID, Constants.INVALID, filterMediaType);
92    let fetchResult = await album.getPhotoAssets(fetchOpt);
93    count = fetchResult.getCount();
94    fetchResult.close();
95    return count;
96  }
97
98  async getItemsCount(albumUri?: string, filterMediaType?: string): Promise<number> {
99    let count = 0;
100    if (albumUri) {
101      let album: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri);
102      // 当前相册count始终为0,先通过查全部图片获取count
103      let fetchOpt = AlbumDefine.getFileFetchOpt(Constants.INVALID, Constants.INVALID, filterMediaType);
104      let fetchResult = await album.getPhotoAssets(fetchOpt);
105      count = fetchResult.getCount();
106      fetchResult.close();
107    } else {
108      let fetchOpt = AlbumDefine.getFileFetchOpt(undefined, undefined, filterMediaType);
109      count = await this.getCount(fetchOpt);
110    }
111    return count;
112  }
113
114  async getItemIndexByUri(uri: string, albumUri?: string): Promise<number> {
115    let index: number = Constants.INVALID;
116    let realUri: string = uri;
117    let realAlbumUri: string = albumUri;
118    let allObject: Array<FileAsset> = null;
119    Log.debug(TAG, `getItemIndexByUri uri: ${uri}, albumUri: ${albumUri}`);
120    if (!uri.startsWith(UserFileManagerAccess.REGISTER_TYPE_ALL_PHOTOS)) {
121      let targetObject: FileAsset = await this.getItemByUri(uri);
122      if (targetObject) {
123        Log.debug(TAG, `find photo for uri: ${realUri}=>${targetObject.uri}`);
124        realUri = targetObject.uri;
125      }
126    }
127    Log.debug(TAG, `getItemIndexByUri real uri: ${realUri}`);
128    if (albumUri && albumUri.length > 0) {
129      if (!albumUri.startsWith(UserFileManagerAccess.REGISTER_TYPE_ALL_ALBUMS)) {
130        let targetAlbum: Album = await UserFileManagerAccess.getInstance().getAlbumByUri(albumUri);
131        if (targetAlbum) {
132          Log.debug(TAG, `find album for uri: ${realAlbumUri}=>${targetAlbum.albumUri}`);
133          realAlbumUri = targetAlbum.albumUri;
134        }
135      }
136    } else {
137      realAlbumUri = "";
138    }
139    Log.debug(TAG, `getItemIndexByUri real album uri: ${realAlbumUri}`);
140    allObject = await this.getItems(realAlbumUri);
141    if (allObject) {
142      Log.debug(TAG, `getItemIndexByUri count: ${allObject.length}`);
143      index = allObject.findIndex((item: FileAsset) => item.uri == realUri);
144    }
145    return index;
146  }
147
148  async getItemByUri(uri: string): Promise<FileAsset> {
149    let object: FileAsset = null;
150    let fetchOpt = AlbumDefine.getFileFetchOptByUri(uri);
151    object = await this.getObject(fetchOpt);
152    return object;
153  }
154
155  getThumbnailSafe(sourceUri: string, path: string, size?): string {
156    try {
157      if (size) {
158        if (size.width != 0 && size.height != 0) {
159          return `${sourceUri}?oper=thumbnail&width=${size.width}&height=${size.height}&path=${path}`;
160        } else {
161          Log.warn(TAG, 'getThumbnailSafe with width==0 and height==0, so do not use thumbnail' + JSON.stringify(size));
162          return `${sourceUri}`;
163        }
164      } else {
165        return `${sourceUri}?oper=thumbnail&width=${BrowserDataImpl.THUMBNAIL_WIDTH}&height=${BrowserDataImpl.THUMBNAIL_WIDTH}&path=${path}`;
166      }
167    } catch (err) {
168      Log.warn(TAG, `get Thumbnail Failed! msg:${err}`);
169      return null;
170    }
171  }
172}