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_file_uri.h"
23 #include "medialibrary_db_const.h"
24 #include "medialibrary_errno.h"
25 #include "media_column.h"
26 #include "media_log.h"
27 #include "thumbnail_const.h"
28 #include "userfile_manager_types.h"
29
30 using namespace std;
31
32 namespace OHOS {
33 namespace Media {
ParseFileUri(const string & uriString,string & outFileId,string & outNetworkId,string & outTableName)34 bool ThumbnailUriUtils::ParseFileUri(const string &uriString, string &outFileId, string &outNetworkId,
35 string &outTableName)
36 {
37 outFileId = GetIdFromUri(uriString);
38 outNetworkId = GetNetworkIdFromUri(uriString);
39 outTableName = GetTableFromUri(uriString);
40 return true;
41 }
42
ParseThumbnailInfo(const string & uriString,string & outFileId,Size & outSize,string & outPath,string & outTableName)43 bool ThumbnailUriUtils::ParseThumbnailInfo(const string &uriString, string &outFileId, Size &outSize,
44 string &outPath, string &outTableName)
45 {
46 string::size_type pos = uriString.find_last_of('?');
47 outTableName = GetTableFromUri(uriString);
48 if (pos == string::npos) {
49 return false;
50 }
51
52 MediaFileUri uri(uriString);
53 outFileId = uri.GetFileId();
54 auto &queryKey = uri.GetQueryKeys();
55 if (queryKey.count(THUMBNAIL_OPERN_KEYWORD) == 0 &&
56 queryKey[THUMBNAIL_OPERN_KEYWORD] != MEDIA_DATA_DB_THUMBNAIL) {
57 return false;
58 }
59
60 if (queryKey.count(THUMBNAIL_WIDTH) != 0) {
61 outSize.width = stoi(queryKey[THUMBNAIL_WIDTH]);
62 }
63
64 if (queryKey.count(THUMBNAIL_HEIGHT) != 0) {
65 outSize.height = stoi(queryKey[THUMBNAIL_HEIGHT]);
66 }
67
68 if (queryKey.count(THUMBNAIL_PATH) != 0) {
69 outPath = queryKey[THUMBNAIL_PATH];
70 }
71
72 if (outSize.width <= 0 || outSize.height <= 0) {
73 return false;
74 }
75
76 return true;
77 }
78
GetNetworkIdFromUri(const string & uri)79 string ThumbnailUriUtils::GetNetworkIdFromUri(const string &uri)
80 {
81 return MediaFileUri(uri).GetNetworkId();
82 }
83
GetIdFromUri(const string & uri)84 string ThumbnailUriUtils::GetIdFromUri(const string &uri)
85 {
86 return MediaFileUri(uri).GetFileId();
87 }
88
GetTableFromUri(const string & uri)89 string ThumbnailUriUtils::GetTableFromUri(const string &uri)
90 {
91 static map<string, string> TYPE_TO_TABLE_MAP = {
92 { PhotoColumn::PHOTO_TYPE_URI, PhotoColumn::PHOTOS_TABLE },
93 { AudioColumn::AUDIO_TYPE_URI, AudioColumn::AUDIOS_TABLE },
94 #ifdef MEDIALIBRARY_COMPATIBILITY
95 { MEDIALIBRARY_TYPE_IMAGE_URI, PhotoColumn::PHOTOS_TABLE },
96 { MEDIALIBRARY_TYPE_VIDEO_URI, PhotoColumn::PHOTOS_TABLE },
97 { MEDIALIBRARY_TYPE_AUDIO_URI, AudioColumn::AUDIOS_TABLE }
98 #else
99 { MEDIALIBRARY_TYPE_IMAGE_URI, MEDIALIBRARY_TABLE },
100 { MEDIALIBRARY_TYPE_VIDEO_URI, MEDIALIBRARY_TABLE },
101 { MEDIALIBRARY_TYPE_AUDIO_URI, MEDIALIBRARY_TABLE }
102 #endif
103 };
104 string table = MEDIALIBRARY_TABLE;
105 for (const auto &iter : TYPE_TO_TABLE_MAP) {
106 if (uri.find(iter.first) != string::npos) {
107 table = iter.second;
108 break;
109 }
110 }
111 return table;
112 }
113 } // namespace Media
114 } // namespace OHOS
115