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 */
15import userFileManager from '@ohos.filemanagement.userFileManager';
16import { Log } from '../../../utils/Log';
17import { FileAsset, UserFileManagerAccess } from '../../../access/UserFileManagerAccess';
18import { DateUtil } from '../../../utils/DateUtil';
19import { Constants } from '../../common/Constants';
20import { AlbumDefine } from '../AlbumDefine';
21import type photoAccessHelper from '@ohos.file.photoAccessHelper';
22
23const TAG: string = 'common_MediaItem';
24
25export const isPhotoAsset =
26  (assets: FileAsset | photoAccessHelper.PhotoAsset): assets is photoAccessHelper.PhotoAsset => (<photoAccessHelper.PhotoAsset> assets).photoType !== undefined;
27
28export class MediaItem {
29  index?: number;
30  mediaType: number;
31  uri: string;
32  thumbnail: string;
33  duration: number;
34  private title: string;
35  size: number;
36  private dateTaken: number;
37  private dateModified: number;
38  orientation: number;
39  width: number;
40  height: number;
41  imgWidth: number;
42  imgHeight: number;
43  isFavor: boolean;
44  displayName: string;
45  dateTrashed: number;
46  private position: userFileManager.PositionType;
47  hashCode: string;
48  private data: userFileManager.FileAsset | photoAccessHelper.PhotoAsset;
49  path: string;
50
51  constructor(data?: userFileManager.FileAsset | photoAccessHelper.PhotoAsset) {
52    this.initialize(data);
53  }
54
55  initialize(data?: userFileManager.FileAsset | photoAccessHelper.PhotoAsset): void {
56    if (!data) {
57      return;
58    }
59
60    this.mediaType = isPhotoAsset(data) ? data.photoType : data.fileType;
61    this.displayName = data.displayName;
62    this.data = data;
63    if (this.mediaType === UserFileManagerAccess.MEDIA_TYPE_VIDEO) {
64      this.duration = Number(data.get(userFileManager.ImageVideoKey.DURATION.toString()));
65    }
66    this.size = Number(data.get('size'));
67    this.orientation = Number(data.get(userFileManager.ImageVideoKey.ORIENTATION.toString()));
68    if (this.orientation === Constants.ANGLE_90 || this.orientation === Constants.ANGLE_270) {
69      this.width = Number(data.get(userFileManager.ImageVideoKey.HEIGHT.toString()));
70      this.height = Number(data.get(userFileManager.ImageVideoKey.WIDTH.toString()));
71    } else {
72      this.width = Number(data.get(userFileManager.ImageVideoKey.WIDTH.toString()));
73      this.height = Number(data.get(userFileManager.ImageVideoKey.HEIGHT.toString()));
74    }
75    this.uri = data.uri;
76    this.path = String(data.get(Constants.KEY_FILE_DATA));
77    this.imgWidth = this.width;
78    this.imgHeight = this.height;
79    this.dateTrashed = Number(data.get(userFileManager.ImageVideoKey.DATE_TRASHED.toString()));
80    this.isFavor = Boolean(data.get(userFileManager.ImageVideoKey.FAVORITE.toString()));
81    this.hashCode = `${this.uri}_${this.size}_${this.orientation}_${this.isFavor}`;
82  }
83
84  async getObject(fetchOpt): Promise<FileAsset> {
85    let object: FileAsset = await UserFileManagerAccess.getInstance().getObject(fetchOpt);
86    return object;
87  }
88
89  async getItemByUri(uri: string): Promise<FileAsset> {
90    let object: FileAsset = null;
91    let fetchOpt = AlbumDefine.getFileFetchOptByUri(uri);
92    object = await this.getObject(fetchOpt);
93    return object;
94  }
95
96  setThumbnail(thumbnail: string) {
97    this.thumbnail = thumbnail;
98  }
99
100  setFavorite(isFavorite: boolean) {
101    this.isFavor = isFavorite;
102  }
103
104  getHashCode(): string {
105    return `${this.uri}_${this.size}_${this.orientation}_${this.isFavor}`
106  }
107
108  getTitle(): string {
109    if (!this.data) {
110      return undefined;
111    }
112    if (!this.title) {
113      this.title = String(this.data.get(userFileManager.ImageVideoKey.TITLE.toString()));
114    }
115    return this.title;
116  }
117
118  setTitle(title: string) {
119    this.title = title;
120  }
121
122  getDataTaken(): number {
123    if (!this.data) {
124      return undefined;
125    }
126    if (!this.dateTaken) {
127      this.dateTaken = Number(this.data.get(userFileManager.ImageVideoKey.DATE_ADDED.toString())) *
128      DateUtil.MILLISECONDS_PER_SECOND; // Waiting: dateTaken is not supported, use dateAdded
129    }
130    return this.dateTaken;
131  }
132
133  setDataTaken(dateTaken: number) {
134    this.dateTaken = dateTaken;
135  }
136
137  getDateModified(): number {
138    if (!this.data) {
139      return undefined;
140    }
141    if (!this.dateModified) {
142      this.dateModified = Number(this.data.get(userFileManager.ImageVideoKey.DATE_MODIFIED.toString())) *
143      DateUtil.MILLISECONDS_PER_SECOND;
144    }
145    return this.dateModified;
146  }
147
148  getPosition(): userFileManager.PositionType {
149    if (!this.data) {
150      return undefined;
151    }
152    if (!this.position) {
153      this.position = this.data.get(userFileManager.ImageVideoKey.POSITION.toString()) as userFileManager.PositionType;
154    }
155    return this.position;
156  }
157}