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 outSize.width = stoi(queryKey[THUMBNAIL_WIDTH]);
64 }
65
66 if (queryKey.count(THUMBNAIL_HEIGHT) != 0) {
67 outSize.height = stoi(queryKey[THUMBNAIL_HEIGHT]);
68 }
69
70 if (queryKey.count(THUMBNAIL_PATH) != 0) {
71 outPath = queryKey[THUMBNAIL_PATH];
72 }
73
74 if (!CheckSize(outSize, outPath)) {
75 return false;
76 }
77
78 return true;
79 }
80
IsOriginalImg(const Size & outSize,const string & outPath)81 bool ThumbnailUriUtils::IsOriginalImg(const Size &outSize, const string &outPath)
82 {
83 return outSize.width == DEFAULT_ORIGINAL && outSize.height == DEFAULT_ORIGINAL;
84 }
85
CheckSize(Size & outSize,const string & outPath)86 bool ThumbnailUriUtils::CheckSize(Size &outSize, const string &outPath)
87 {
88 if (IsOriginalImg(outSize, outPath)) {
89 outSize.width = DEFAULT_LCD_SIZE;
90 outSize.height = DEFAULT_LCD_SIZE;
91 }
92
93 if (outSize.width == 0 && outSize.height == 0) {
94 outSize.width = DEFAULT_THUMB_SIZE;
95 outSize.height = DEFAULT_THUMB_SIZE;
96 }
97
98 if ((outSize.width <= 0 || outSize.height <= 0) && !IsOriginalImg(outSize, outPath)) {
99 return false;
100 }
101
102 return true;
103 }
104
GetTableFromUri(const string & uri)105 string ThumbnailUriUtils::GetTableFromUri(const string &uri)
106 {
107 string table = MediaFileUri(uri).GetTableName();
108 if (table.empty()) {
109 return MEDIALIBRARY_TABLE;
110 }
111 return table;
112 }
113
GetDateTakenFromUri(const string & uri)114 string ThumbnailUriUtils::GetDateTakenFromUri(const string &uri)
115 {
116 auto index = uri.rfind('&');
117 if (index == std::string::npos) {
118 MEDIA_ERR_LOG("GetDateTakenFromUri find index for last string failed: %{private}s", uri.c_str());
119 return "";
120 }
121
122 string pairString = uri.substr(index + 1);
123 size_t splitIndex = pairString.find('=');
124 if (splitIndex == std::string::npos) {
125 MEDIA_ERR_LOG("GetDateTakenFromUri failed to parse pairString: %{private}s", pairString.c_str());
126 return "";
127 }
128
129 if (pairString.substr(0, splitIndex) == ML_URI_DATE_TAKEN) {
130 return pairString.substr(splitIndex + 1);
131 }
132 return "";
133 }
134
GetFileUriFromUri(const string & uri)135 string ThumbnailUriUtils::GetFileUriFromUri(const string &uri)
136 {
137 auto index = uri.find('?');
138 if (index == std::string::npos) {
139 MEDIA_ERR_LOG("GetFileUriFromUri find index for string failed: %{private}s", uri.c_str());
140 return "";
141 }
142 return uri.substr(0, index);
143 }
144 } // namespace Media
145 } // namespace OHOS
146