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