1 /*
2 * Copyright (C) 2021-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
16 #include "medialibrary_data_manager_utils.h"
17
18 #include "media_file_uri.h"
19 #include "media_file_utils.h"
20 #include "media_log.h"
21 #include "medialibrary_common_utils.h"
22 #include "medialibrary_db_const.h"
23 #include "photo_album_column.h"
24
25 using namespace std;
26
27 namespace OHOS {
28 namespace Media {
IsNumber(const string & str)29 bool MediaLibraryDataManagerUtils::IsNumber(const string &str)
30 {
31 if (str.empty()) {
32 MEDIA_ERR_LOG("IsNumber input is empty ");
33 return false;
34 }
35
36 for (char const &c : str) {
37 if (isdigit(c) == 0) {
38 return false;
39 }
40 }
41 return true;
42 }
43
GetOperationType(const string & uri)44 string MediaLibraryDataManagerUtils::GetOperationType(const string &uri)
45 {
46 string oprn;
47 size_t found = uri.rfind('/');
48 if (found != string::npos) {
49 oprn = uri.substr(found + 1);
50 }
51
52 return oprn;
53 }
54
GetDisPlayNameFromPath(const std::string & path)55 string MediaLibraryDataManagerUtils::GetDisPlayNameFromPath(const std::string &path)
56 {
57 string displayName;
58 size_t lastSlashPosition = path.rfind("/");
59 if (lastSlashPosition != string::npos) {
60 displayName = path.substr(lastSlashPosition + 1);
61 }
62 return displayName;
63 }
64
ObtionCondition(string & strQueryCondition,const vector<string> & whereArgs)65 string MediaLibraryDataManagerUtils::ObtionCondition(string &strQueryCondition, const vector<string> &whereArgs)
66 {
67 for (string args : whereArgs) {
68 size_t pos = strQueryCondition.find('?');
69 if (pos != string::npos) {
70 strQueryCondition.replace(pos, 1, "'" + args + "'");
71 }
72 }
73 return strQueryCondition;
74 }
75
GetTypeUriByUri(std::string & uri)76 std::string MediaLibraryDataManagerUtils::GetTypeUriByUri(std::string &uri)
77 {
78 string typeUri;
79 if (uri.find(PhotoColumn::PHOTO_URI_PREFIX) != string::npos) {
80 typeUri = PhotoColumn::PHOTO_URI_PREFIX;
81 } else if (uri.find(PhotoAlbumColumns::ALBUM_URI_PREFIX) != string::npos) {
82 typeUri = PhotoAlbumColumns::ALBUM_URI_PREFIX;
83 } else if (uri.find(AudioColumn::AUDIO_URI_PREFIX) != string::npos) {
84 typeUri = AudioColumn::AUDIO_URI_PREFIX;
85 }
86 return typeUri;
87 }
88
GetFileIdFromPhotoUri(const std::string & uri)89 std::string MediaLibraryDataManagerUtils::GetFileIdFromPhotoUri(const std::string &uri)
90 {
91 auto startIndex = uri.find(PhotoColumn::PHOTO_URI_PREFIX);
92 if (startIndex == std::string::npos) {
93 return "";
94 }
95 auto endIndex = uri.find("/", startIndex + PhotoColumn::PHOTO_URI_PREFIX.length());
96 if (endIndex == std::string::npos) {
97 return uri.substr(startIndex + PhotoColumn::PHOTO_URI_PREFIX.length());
98 }
99 return uri.substr(startIndex + PhotoColumn::PHOTO_URI_PREFIX.length(),
100 endIndex - startIndex - PhotoColumn::PHOTO_URI_PREFIX.length());
101 }
102
GetFileIdNumFromPhotoUri(const std::string & uri)103 int32_t MediaLibraryDataManagerUtils::GetFileIdNumFromPhotoUri(const std::string &uri)
104 {
105 auto strFileId = MediaLibraryDataManagerUtils::GetFileIdFromPhotoUri(uri);
106 if (strFileId.empty()) {
107 MEDIA_ERR_LOG("empty uri");
108 return 0;
109 }
110 for (size_t index = 0; index < strFileId.size(); ++index) {
111 if (!std::isdigit(static_cast<unsigned char>(strFileId[index]))) {
112 MEDIA_ERR_LOG("wrong uri: %{public}s", uri.c_str());
113 return 0;
114 }
115 }
116 return stoi(strFileId);
117 }
118
119 } // namespace Media
120 } // namespace OHOS
121