1 /*
2 * Copyright (C) 2024 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
16 #include "media_asset_helper_impl.h"
17
18 #include "media_file_utils.h"
19 #include "media_log.h"
20 #include "media_userfile_client.h"
21 #include "media_column.h"
22 #include "medialibrary_errno.h"
23 #include "userfilemgr_uri.h"
24 #include "oh_media_asset.h"
25
26 namespace OHOS {
27 namespace Media {
28
CreateMediaAssetHelper()29 std::shared_ptr<MediaAssetHelper> MediaAssetHelperFactory::CreateMediaAssetHelper()
30 {
31 std::shared_ptr<MediaAssetHelperImpl> impl = std::make_shared<MediaAssetHelperImpl>();
32 CHECK_AND_PRINT_LOG(impl != nullptr, "Failed to create MediaAssetHelperImpl instance.");
33
34 return impl;
35 }
36
MediaAssetHelperImpl()37 MediaAssetHelperImpl::MediaAssetHelperImpl() {}
38
~MediaAssetHelperImpl()39 MediaAssetHelperImpl::~MediaAssetHelperImpl() {}
40
GetMediaAsset(std::string uri,int32_t cameraShotType,std::string burstKey)41 OH_MediaAsset* MediaAssetHelperImpl::GetMediaAsset(std::string uri, int32_t cameraShotType, std::string burstKey)
42 {
43 std::shared_ptr<FileAsset> fileAsset = std::make_shared<FileAsset>();
44 CHECK_AND_RETURN_RET_LOG(fileAsset != nullptr, nullptr, "create file asset failed");
45
46 fileAsset->SetUri(uri);
47 std::string fileId = MediaFileUtils::GetIdFromUri(uri);
48 if (!fileId.empty()) {
49 fileAsset->SetId(stoi(fileId));
50 }
51 fileAsset->SetDisplayName(MediaFileUtils::GetFileName(uri));
52 if (cameraShotType == static_cast<int32_t>(Media::CameraShotType::IMAGE)) {
53 fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::CAMERA));
54 fileAsset->SetMediaType(Media::MediaType::MEDIA_TYPE_IMAGE);
55 } else if (cameraShotType == static_cast<int32_t>(CameraShotType::MOVING_PHOTO)) {
56 fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::MOVING_PHOTO));
57 fileAsset->SetMediaType(Media::MediaType::MEDIA_TYPE_IMAGE);
58 } else if (cameraShotType == static_cast<int32_t>(CameraShotType::BURST)) {
59 fileAsset->SetPhotoSubType(static_cast<int32_t>(PhotoSubType::BURST));
60 fileAsset->SetMediaType(Media::MediaType::MEDIA_TYPE_IMAGE);
61 fileAsset->SetBurstKey(burstKey);
62 } else {
63 MEDIA_INFO_LOG("invalid cameraShotKey: %{public}d", cameraShotType);
64 }
65
66 InitFileAsset(fileAsset);
67 auto mediaAssetObj = MediaAssetFactory::CreateMediaAsset(fileAsset);
68 auto mediaAsset = new OH_MediaAsset(mediaAssetObj);
69 CHECK_AND_RETURN_RET_LOG(mediaAsset != nullptr, nullptr, "create media asset failed");
70 return mediaAsset;
71 }
72
InitFileAsset(std::shared_ptr<FileAsset> fileAsset)73 void MediaAssetHelperImpl::InitFileAsset(std::shared_ptr<FileAsset> fileAsset)
74 {
75 auto resultSet = QueryFileAsset(fileAsset->GetId());
76 CHECK_AND_RETURN_LOG(resultSet != nullptr, "query resultSet is nullptr");
77
78 int indexPos = -1;
79 resultSet->GetColumnIndex(PhotoColumn::MEDIA_SIZE, indexPos);
80 int64_t size = 0;
81 resultSet->GetLong(indexPos, size);
82 fileAsset->SetSize(size);
83 MEDIA_INFO_LOG("init file asset, query Size: %{public}ld", static_cast<long>(size));
84
85 indexPos = -1;
86 resultSet->GetColumnIndex(PhotoColumn::MEDIA_DATE_MODIFIED, indexPos);
87 int64_t dateMmodified = 0;
88 resultSet->GetLong(indexPos, dateMmodified);
89 fileAsset->SetDateModified(dateMmodified);
90 MEDIA_INFO_LOG("init file asset, query dateMmodified: %{public}ld", static_cast<long>(dateMmodified));
91
92 indexPos = -1;
93 resultSet->GetColumnIndex(PhotoColumn::PHOTO_WIDTH, indexPos);
94 int32_t width = 0;
95 resultSet->GetInt(indexPos, width);
96 fileAsset->SetWidth(width);
97 MEDIA_INFO_LOG("init file asset, query width: %{public}d", width);
98
99 indexPos = -1;
100 resultSet->GetColumnIndex(PhotoColumn::PHOTO_HEIGHT, indexPos);
101 int32_t height = 0;
102 resultSet->GetInt(indexPos, height);
103 fileAsset->SetHeight(height);
104 MEDIA_INFO_LOG("init file asset, query height: %{public}d", height);
105
106 UpdateFileAsset(resultSet, fileAsset);
107 return;
108 }
109
QueryFileAsset(int32_t mediaId)110 std::shared_ptr<DataShare::DataShareResultSet> MediaAssetHelperImpl::QueryFileAsset(int32_t mediaId)
111 {
112 if (!UserFileClient::IsValid()) {
113 UserFileClient::Init();
114 }
115
116 DataShare::DataSharePredicates predicates;
117 predicates.EqualTo(MediaColumn::MEDIA_ID, mediaId);
118 std::vector<std::string> fetchColumn {
119 PhotoColumn::MEDIA_SIZE,
120 PhotoColumn::MEDIA_DATE_MODIFIED,
121 PhotoColumn::PHOTO_WIDTH,
122 PhotoColumn::PHOTO_HEIGHT,
123 PhotoColumn::PHOTO_ORIENTATION,
124 PhotoColumn::MEDIA_DATE_ADDED,
125 PhotoColumn::MEDIA_DATE_TAKEN,
126 PhotoColumn::MEDIA_DURATION,
127 PhotoColumn::MEDIA_IS_FAV,
128 PhotoColumn::MEDIA_TITLE
129 };
130 Uri uri(PAH_QUERY_PHOTO);
131 int errCode;
132 auto resultSet = UserFileClient::Query(uri, predicates, fetchColumn, errCode);
133 if (resultSet == nullptr || resultSet->GoToFirstRow() != E_OK) {
134 MEDIA_ERR_LOG("init file asset failed, query resultSet is nullptr");
135 return nullptr;
136 }
137 return resultSet;
138 }
139
UpdateFileAsset(std::shared_ptr<DataShare::DataShareResultSet> resultSet,std::shared_ptr<FileAsset> fileAsset)140 void MediaAssetHelperImpl::UpdateFileAsset(std::shared_ptr<DataShare::DataShareResultSet> resultSet,
141 std::shared_ptr<FileAsset> fileAsset)
142 {
143 int indexPos = -1;
144 resultSet->GetColumnIndex(PhotoColumn::PHOTO_ORIENTATION, indexPos);
145 int32_t orientation = 0;
146 resultSet->GetInt(indexPos, orientation);
147 fileAsset->SetOrientation(orientation);
148 MEDIA_INFO_LOG("init file asset, query orientation: %{public}d", orientation);
149
150 indexPos = -1;
151 resultSet->GetColumnIndex(PhotoColumn::MEDIA_DATE_ADDED, indexPos);
152 int64_t dateAdded = 0;
153 resultSet->GetLong(indexPos, dateAdded);
154 fileAsset->SetDateAdded(dateAdded);
155 MEDIA_INFO_LOG("init file asset, query dateAdded: %{public}ld", static_cast<long>(dateAdded));
156
157 indexPos = -1;
158 resultSet->GetColumnIndex(PhotoColumn::MEDIA_DATE_TAKEN, indexPos);
159 int64_t dateTaken = 0;
160 resultSet->GetLong(indexPos, dateTaken);
161 fileAsset->SetDateTaken(dateTaken);
162 MEDIA_INFO_LOG("init file asset, query dateTaken: %{public}ld", static_cast<long>(dateTaken));
163
164 indexPos = -1;
165 resultSet->GetColumnIndex(PhotoColumn::MEDIA_DURATION, indexPos);
166 int32_t duration = 0;
167 resultSet->GetInt(indexPos, duration);
168 fileAsset->SetDuration(duration);
169 MEDIA_INFO_LOG("init file asset, query duration: %{public}d", duration);
170
171 indexPos = -1;
172 resultSet->GetColumnIndex(PhotoColumn::MEDIA_IS_FAV, indexPos);
173 int32_t isFav = 0;
174 resultSet->GetInt(indexPos, isFav);
175 fileAsset->SetFavorite(isFav);
176 MEDIA_INFO_LOG("init file asset, query isFav: %{public}d", isFav);
177
178 indexPos = -1;
179 resultSet->GetColumnIndex(PhotoColumn::MEDIA_TITLE, indexPos);
180 std::string title;
181 resultSet->GetString(indexPos, title);
182 fileAsset->SetTitle(title);
183 MEDIA_INFO_LOG("init file asset, query title: %{public}s", title.c_str());
184
185 return;
186 }
187
188 }
189 }