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_impl.h"
17 
18 #include "media_log.h"
19 #include <securec.h>
20 #include "nocopyable.h"
21 #include "userfile_manager_types.h"
22 
23 using namespace OHOS::Media;
24 
25 const static int64_t MILLI_TO_SECOND = 1000;
26 const static int32_t MAX_URI_LENGTH = 256;
27 const static int32_t MAX_DISPLAY_NAME_LENGTH = MAX_URI_LENGTH + 6;
28 const static int32_t MAX_TITLE_LENGTH = 256;
29 
30 const std::unordered_map<MediaType, MediaLibrary_MediaType> g_mediaTypeMapping = {
31     {MediaType::MEDIA_TYPE_IMAGE, MediaLibrary_MediaType::MEDIA_LIBRARY_IMAGE},
32     {MediaType::MEDIA_TYPE_VIDEO, MediaLibrary_MediaType::MEDIA_LIBRARY_VIDEO},
33 };
34 
35 const std::unordered_map<PhotoSubType, MediaLibrary_MediaSubType> g_photoSubTypeMapping = {
36     {PhotoSubType::DEFAULT, MediaLibrary_MediaSubType::MEDIA_LIBRARY_DEFAULT},
37     {PhotoSubType::CAMERA, MediaLibrary_MediaSubType::MEDIA_LIBRARY_DEFAULT},
38     {PhotoSubType::MOVING_PHOTO, MediaLibrary_MediaSubType::MEDIA_LIBRARY_MOVING_PHOTO},
39     {PhotoSubType::BURST, MediaLibrary_MediaSubType::MEDIA_LIBRARY_BURST},
40 };
41 
CreateMediaAsset(std::shared_ptr<FileAsset> fileAsset)42 std::shared_ptr<MediaAsset> MediaAssetFactory::CreateMediaAsset(
43     std::shared_ptr<FileAsset> fileAsset)
44 {
45     std::shared_ptr<MediaAssetImpl> impl = std::make_shared<MediaAssetImpl>(fileAsset);
46     CHECK_AND_PRINT_LOG(impl != nullptr, "Failed to create MediaAssetManagerImpl instance.");
47 
48     return impl;
49 }
50 
MediaAssetImpl(std::shared_ptr<FileAsset> fileAsset)51 MediaAssetImpl::MediaAssetImpl(std::shared_ptr<FileAsset> fileAsset)
52 {
53     MEDIA_DEBUG_LOG("MediaAssetImpl Constructor is called.");
54     fileAsset_ = fileAsset;
55     uri_ = new char[MAX_URI_LENGTH];
56     displayName_ = new char[MAX_DISPLAY_NAME_LENGTH];
57     title_ = new char[MAX_TITLE_LENGTH];
58 }
59 
~MediaAssetImpl()60 MediaAssetImpl::~MediaAssetImpl()
61 {
62     if (fileAsset_ != nullptr) {
63         fileAsset_ = nullptr;
64     }
65 
66     if (uri_ != nullptr) {
67         delete[] uri_;
68         uri_ = nullptr;
69     }
70 
71     if (displayName_ != nullptr) {
72         delete[] displayName_;
73         displayName_ = nullptr;
74     }
75 
76     if (title_ != nullptr) {
77         delete[] title_;
78         title_ = nullptr;
79     }
80 }
81 
GetUri(const char ** uri)82 MediaLibrary_ErrorCode MediaAssetImpl::GetUri(const char** uri)
83 {
84     if (uri_ == nullptr) {
85         uri_ = new(std::nothrow) char[MAX_URI_LENGTH];
86         CHECK_AND_RETURN_RET_LOG(uri_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "alloc memory failed!");
87     }
88 
89     const std::string fileUri = fileAsset_->GetUri();
90     int32_t uriLen = static_cast<int32_t>(fileUri.length());
91     int32_t len = uriLen < MAX_URI_LENGTH ? uriLen : MAX_URI_LENGTH - 1;
92     strncpy_s(uri_, MAX_URI_LENGTH, fileUri.c_str(), len);
93     MEDIA_INFO_LOG("MediaAssetImpl::GetUri, uri: %{public}s, return uri: %{public}s",
94         fileUri.c_str(), uri_);
95     *uri = uri_;
96     return MEDIA_LIBRARY_OK;
97 }
98 
GetMediaType(MediaLibrary_MediaType * mediaType)99 MediaLibrary_ErrorCode MediaAssetImpl::GetMediaType(MediaLibrary_MediaType* mediaType)
100 {
101     MediaType type = fileAsset_->GetMediaType();
102     MEDIA_INFO_LOG("GetMediaType type: %{public}d", static_cast<int32_t>(type));
103     auto itr = g_mediaTypeMapping.find(type);
104     if (itr != g_mediaTypeMapping.end()) {
105         *mediaType = itr->second;
106         return MEDIA_LIBRARY_OK;
107     }
108     return MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR;
109 }
110 
GetMediaSubType(MediaLibrary_MediaSubType * mediaSubType)111 MediaLibrary_ErrorCode MediaAssetImpl::GetMediaSubType(MediaLibrary_MediaSubType* mediaSubType)
112 {
113     PhotoSubType subType = static_cast<PhotoSubType>(fileAsset_->GetPhotoSubType());
114     MEDIA_INFO_LOG("GetMediaSubType subType: %{public}d", static_cast<int32_t>(subType));
115     auto itr = g_photoSubTypeMapping.find(subType);
116     if (itr != g_photoSubTypeMapping.end()) {
117         *mediaSubType = itr->second;
118         return MEDIA_LIBRARY_OK;
119     }
120     return MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR;
121 }
122 
GetDisplayName(const char ** displayName)123 MediaLibrary_ErrorCode MediaAssetImpl::GetDisplayName(const char** displayName)
124 {
125     if (displayName_ == nullptr) {
126         displayName_ = new(std::nothrow) char[MAX_DISPLAY_NAME_LENGTH];
127         CHECK_AND_RETURN_RET_LOG(displayName_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "alloc memory failed!");
128     }
129 
130     const std::string display = fileAsset_->GetDisplayName();
131     int32_t displayNameLen = static_cast<int32_t>(display.length());
132     int32_t len = displayNameLen < MAX_DISPLAY_NAME_LENGTH ? displayNameLen : MAX_DISPLAY_NAME_LENGTH - 1;
133     strncpy_s(displayName_, MAX_DISPLAY_NAME_LENGTH, display.c_str(), len);
134     MEDIA_INFO_LOG("MediaAssetImpl::GetDisplayName, display name: %{public}s, return display name: %{public}s",
135         display.c_str(), displayName_);
136     *displayName = displayName_;
137     return MEDIA_LIBRARY_OK;
138 }
139 
GetSize(uint32_t * size)140 MediaLibrary_ErrorCode MediaAssetImpl::GetSize(uint32_t* size)
141 {
142     *size = static_cast<uint32_t>(fileAsset_->GetSize());
143     return MEDIA_LIBRARY_OK;
144 }
145 
GetDateAdded(uint32_t * dateAdded)146 MediaLibrary_ErrorCode MediaAssetImpl::GetDateAdded(uint32_t* dateAdded)
147 {
148     *dateAdded = static_cast<uint32_t>(fileAsset_->GetDateAdded() / MILLI_TO_SECOND);
149     return MEDIA_LIBRARY_OK;
150 }
151 
GetDateModified(uint32_t * dateModified)152 MediaLibrary_ErrorCode MediaAssetImpl::GetDateModified(uint32_t* dateModified)
153 {
154     *dateModified = static_cast<uint32_t>(fileAsset_->GetDateModified() / MILLI_TO_SECOND);
155     return MEDIA_LIBRARY_OK;
156 }
157 
GetDateAddedMs(uint32_t * dateAddedMs)158 MediaLibrary_ErrorCode MediaAssetImpl::GetDateAddedMs(uint32_t* dateAddedMs)
159 {
160     *dateAddedMs = static_cast<uint32_t>(fileAsset_->GetDateModified());
161     return MEDIA_LIBRARY_OK;
162 }
163 
GetDateModifiedMs(uint32_t * dateModifiedMs)164 MediaLibrary_ErrorCode MediaAssetImpl::GetDateModifiedMs(uint32_t* dateModifiedMs)
165 {
166     *dateModifiedMs = static_cast<uint32_t>(fileAsset_->GetDateModified());
167     return MEDIA_LIBRARY_OK;
168 }
169 
GetDateTaken(uint32_t * dateTaken)170 MediaLibrary_ErrorCode MediaAssetImpl::GetDateTaken(uint32_t* dateTaken)
171 {
172     *dateTaken = static_cast<uint32_t>(fileAsset_->GetDateTaken());
173     return MEDIA_LIBRARY_OK;
174 }
175 
GetFileAssetInstance() const176 std::shared_ptr<FileAsset> MediaAssetImpl::GetFileAssetInstance() const
177 {
178     return fileAsset_;
179 }
180 
GetDuration(uint32_t * duration)181 MediaLibrary_ErrorCode MediaAssetImpl::GetDuration(uint32_t* duration)
182 {
183     *duration  = static_cast<uint32_t>(fileAsset_->GetDuration());
184     return MEDIA_LIBRARY_OK;
185 }
186 
GetWidth(uint32_t * width)187 MediaLibrary_ErrorCode MediaAssetImpl::GetWidth(uint32_t* width)
188 {
189     *width = static_cast<uint32_t>(fileAsset_->GetWidth());
190     return MEDIA_LIBRARY_OK;
191 }
192 
GetHeight(uint32_t * height)193 MediaLibrary_ErrorCode MediaAssetImpl::GetHeight(uint32_t* height)
194 {
195     *height = static_cast<uint32_t>(fileAsset_->GetHeight());
196     return MEDIA_LIBRARY_OK;
197 }
198 
GetOrientation(uint32_t * orientation)199 MediaLibrary_ErrorCode MediaAssetImpl::GetOrientation(uint32_t* orientation)
200 {
201     *orientation = static_cast<uint32_t>(fileAsset_->GetOrientation());
202     return MEDIA_LIBRARY_OK;
203 }
204 
IsFavorite(uint32_t * favorite)205 MediaLibrary_ErrorCode MediaAssetImpl::IsFavorite(uint32_t* favorite)
206 {
207     *favorite = static_cast<uint32_t>(fileAsset_->IsFavorite());
208     return MEDIA_LIBRARY_OK;
209 }
210 
GetTitle(const char ** title)211 MediaLibrary_ErrorCode MediaAssetImpl::GetTitle(const char** title)
212 {
213     if (title_ == nullptr) {
214         title_ = new(std::nothrow) char[MAX_TITLE_LENGTH];
215         CHECK_AND_RETURN_RET_LOG(title_ != nullptr, MEDIA_LIBRARY_INTERNAL_SYSTEM_ERROR, "alloc memory failed!");
216     }
217 
218     const std::string titleStr = fileAsset_->GetTitle();
219     int32_t titleLen = static_cast<int32_t>(titleStr.length());
220     int32_t len = titleLen < MAX_TITLE_LENGTH ? titleLen : MAX_TITLE_LENGTH - 1;
221     strncpy_s(title_, MAX_TITLE_LENGTH, titleStr.c_str(), len);
222     MEDIA_INFO_LOG("GetTitle, title: %{public}s, return title: %{public}s", titleStr.c_str(), title_);
223     *title = title_;
224     return MEDIA_LIBRARY_OK;
225 }
226