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 */
15 #define MLOG_TAG "Thumbnail"
16
17 #include "thumbnail_uri_utils.h"
18
19 #include <algorithm>
20 #include <map>
21
22 #include "media_column.h"
23 #include "media_file_uri.h"
24 #include "media_file_utils.h"
25 #include "media_log.h"
26 #include "medialibrary_db_const.h"
27 #include "medialibrary_errno.h"
28 #include "thumbnail_const.h"
29 #include "userfile_manager_types.h"
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace Media {
ParseFileUri(const string & uriString,string & outFileId,string & outNetworkId,string & outTableName)35 bool ThumbnailUriUtils::ParseFileUri(const string &uriString, string &outFileId, string &outNetworkId,
36 string &outTableName)
37 {
38 outFileId = MediaFileUtils::GetIdFromUri(uriString);
39 outNetworkId = MediaFileUtils::GetNetworkIdFromUri(uriString);
40 outTableName = GetTableFromUri(uriString);
41 return true;
42 }
43
ParseThumbnailInfo(const string & uriString,string & outFileId,Size & outSize,string & outPath,string & outTableName)44 bool ThumbnailUriUtils::ParseThumbnailInfo(const string &uriString, string &outFileId, Size &outSize,
45 string &outPath, string &outTableName)
46 {
47 string::size_type pos = uriString.find_last_of('?');
48 outTableName = GetTableFromUri(uriString);
49 if (pos == string::npos) {
50 return false;
51 }
52
53 MediaFileUri uri(uriString);
54 outFileId = uri.GetFileId();
55 auto &queryKey = uri.GetQueryKeys();
56 if (queryKey.count(THUMBNAIL_OPERN_KEYWORD) == 0 &&
57 (queryKey[THUMBNAIL_OPERN_KEYWORD] != MEDIA_DATA_DB_THUMBNAIL ||
58 queryKey[THUMBNAIL_OPERN_KEYWORD] != MEDIA_DATA_DB_THUMB_ASTC)) {
59 return false;
60 }
61
62 if (queryKey.count(THUMBNAIL_WIDTH) != 0) {
63 if (MediaFileUtils::IsValidInteger(queryKey[THUMBNAIL_WIDTH])) {
64 outSize.width = stoi(queryKey[THUMBNAIL_WIDTH]);
65 }
66 }
67
68 if (queryKey.count(THUMBNAIL_HEIGHT) != 0) {
69 if (MediaFileUtils::IsValidInteger(queryKey[THUMBNAIL_HEIGHT])) {
70 outSize.height = stoi(queryKey[THUMBNAIL_HEIGHT]);
71 }
72 }
73
74 if (queryKey.count(THUMBNAIL_PATH) != 0) {
75 outPath = queryKey[THUMBNAIL_PATH];
76 }
77
78 if (!CheckSize(outSize, outPath)) {
79 return false;
80 }
81
82 return true;
83 }
84
ParseKeyFrameThumbnailInfo(const string & uriString,string & outFileId,int32_t & outBeginStamp,int32_t & outType,string & outPath)85 bool ThumbnailUriUtils::ParseKeyFrameThumbnailInfo(const string &uriString, string &outFileId, int32_t &outBeginStamp,
86 int32_t &outType, string &outPath)
87 {
88 string::size_type pos = uriString.find_last_of('?');
89 if (pos == string::npos) {
90 return false;
91 }
92 MediaFileUri uri(uriString);
93 outFileId = uri.GetFileId();
94 auto &queryKey = uri.GetQueryKeys();
95
96 if (queryKey.count(THUMBNAIL_OPERN_KEYWORD) == 0 &&
97 queryKey[THUMBNAIL_OPERN_KEYWORD] != MEDIA_DATA_DB_KEY_FRAME) {
98 MEDIA_ERR_LOG("The key_word in uri id not key_frame!");
99 return false;
100 }
101
102 if (queryKey.count(THUMBNAIL_BEGIN_STAMP) != 0) {
103 if (MediaFileUtils::IsValidInteger(queryKey[THUMBNAIL_BEGIN_STAMP])) {
104 outBeginStamp = stoi(queryKey[THUMBNAIL_BEGIN_STAMP]);
105 }
106 }
107
108 if (queryKey.count(THUMBNAIL_TYPE) != 0) {
109 if (MediaFileUtils::IsValidInteger(queryKey[THUMBNAIL_TYPE])) {
110 outType = stoi(queryKey[THUMBNAIL_TYPE]);
111 }
112 }
113
114 if (queryKey.count(THUMBNAIL_PATH) != 0) {
115 outPath = queryKey[THUMBNAIL_PATH];
116 }
117 return true;
118 }
119
IsOriginalImg(const Size & outSize,const string & outPath)120 bool ThumbnailUriUtils::IsOriginalImg(const Size &outSize, const string &outPath)
121 {
122 return outSize.width == DEFAULT_ORIGINAL && outSize.height == DEFAULT_ORIGINAL;
123 }
124
CheckSize(Size & outSize,const string & outPath)125 bool ThumbnailUriUtils::CheckSize(Size &outSize, const string &outPath)
126 {
127 if (IsOriginalImg(outSize, outPath)) {
128 outSize.width = DEFAULT_LCD_SIZE;
129 outSize.height = DEFAULT_LCD_SIZE;
130 }
131
132 if (outSize.width == 0 && outSize.height == 0) {
133 outSize.width = DEFAULT_THUMB_SIZE;
134 outSize.height = DEFAULT_THUMB_SIZE;
135 }
136
137 if ((outSize.width <= 0 || outSize.height <= 0) && !IsOriginalImg(outSize, outPath)) {
138 return false;
139 }
140
141 return true;
142 }
143
GetTableFromUri(const string & uri)144 string ThumbnailUriUtils::GetTableFromUri(const string &uri)
145 {
146 string table = MediaFileUri(uri).GetTableName();
147 if (table.empty()) {
148 return MEDIALIBRARY_TABLE;
149 }
150 return table;
151 }
152
GetDateTakenFromUri(const string & uri)153 string ThumbnailUriUtils::GetDateTakenFromUri(const string &uri)
154 {
155 auto index = uri.rfind('&');
156 if (index == std::string::npos) {
157 MEDIA_ERR_LOG("GetDateTakenFromUri find index for last string failed: %{private}s", uri.c_str());
158 return "";
159 }
160
161 string pairString = uri.substr(index + 1);
162 size_t splitIndex = pairString.find('=');
163 if (splitIndex == std::string::npos) {
164 MEDIA_ERR_LOG("GetDateTakenFromUri failed to parse pairString: %{private}s", pairString.c_str());
165 return "";
166 }
167
168 if (pairString.substr(0, splitIndex) == ML_URI_DATE_TAKEN) {
169 return pairString.substr(splitIndex + 1);
170 }
171 return "";
172 }
173
GetDateModifiedFromUri(const string & uri)174 string ThumbnailUriUtils::GetDateModifiedFromUri(const string &uri)
175 {
176 size_t index = uri.find(ML_URI_DATE_MODIFIED);
177 if (index == std::string::npos) {
178 MEDIA_ERR_LOG("GetDateModifiedFromUri find index for dateModified failed: %{private}s", uri.c_str());
179 return "";
180 }
181
182 string pairString = uri.substr(index + 1);
183 size_t startIndex = pairString.find('=');
184 size_t endIndex = pairString.find('&');
185 if (startIndex == std::string::npos || endIndex == std::string::npos || endIndex - startIndex - 1 <= 0) {
186 MEDIA_ERR_LOG("GetDateModifiedFromUri failed to parse pairString: %{private}s", pairString.c_str());
187 return "";
188 }
189 return pairString.substr(startIndex + 1, endIndex - startIndex - 1);
190 }
191
GetFileUriFromUri(const string & uri)192 string ThumbnailUriUtils::GetFileUriFromUri(const string &uri)
193 {
194 auto index = uri.find('?');
195 if (index == std::string::npos) {
196 MEDIA_ERR_LOG("GetFileUriFromUri find index for string failed: %{private}s", uri.c_str());
197 return "";
198 }
199 return uri.substr(0, index);
200 }
201 } // namespace Media
202 } // namespace OHOS
203