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