• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_utils.h"
19 #include "media_log.h"
20 #include "medialibrary_common_utils.h"
21 #include "medialibrary_db_const.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26 namespace Media {
GetFileName(const string & path)27 string MediaLibraryDataManagerUtils::GetFileName(const string &path)
28 {
29     string name;
30     size_t slashIndex = path.rfind("/");
31     if (slashIndex != string::npos) {
32         name = path.substr(slashIndex + 1);
33     }
34     return name;
35 }
36 
GetParentPath(const string & path)37 string MediaLibraryDataManagerUtils::GetParentPath(const string &path)
38 {
39     string name;
40     size_t slashIndex = path.rfind("/");
41     if (slashIndex != string::npos) {
42         name = path.substr(0, slashIndex);
43     }
44 
45     return name;
46 }
47 
IsNumber(const string & str)48 bool MediaLibraryDataManagerUtils::IsNumber(const string &str)
49 {
50     if (str.empty()) {
51         MEDIA_ERR_LOG("IsNumber input is empty ");
52         return false;
53     }
54 
55     for (char const &c : str) {
56         if (isdigit(c) == 0) {
57             return false;
58         }
59     }
60     return true;
61 }
62 
GetFileTitle(const std::string & displayName)63 std::string MediaLibraryDataManagerUtils::GetFileTitle(const std::string &displayName)
64 {
65     std::string title = "";
66     if (!displayName.empty()) {
67         std::string::size_type pos = displayName.find_first_of('.');
68         if (pos == displayName.length()) {
69             return displayName;
70         }
71         title = displayName.substr(0, pos);
72         MEDIA_DEBUG_LOG("title substr = %{private}s", title.c_str());
73     }
74     return title;
75 }
76 
GetOperationType(const string & uri)77 string MediaLibraryDataManagerUtils::GetOperationType(const string &uri)
78 {
79     string oprn;
80     size_t found = uri.rfind('/');
81     if (found != string::npos) {
82         oprn = uri.substr(found + 1);
83     }
84 
85     return oprn;
86 }
87 
GetIdFromUri(const string & uri)88 string MediaLibraryDataManagerUtils::GetIdFromUri(const string &uri)
89 {
90     string rowNum = "-1";
91 
92     size_t pos = uri.rfind('/');
93     if (pos != std::string::npos) {
94         rowNum = uri.substr(pos + 1);
95     }
96 
97     return rowNum;
98 }
99 
GetNetworkIdFromUri(const string & uri)100 string MediaLibraryDataManagerUtils::GetNetworkIdFromUri(const string &uri)
101 {
102     string networkId;
103     if (uri.empty()) {
104         return networkId;
105     }
106     size_t pos = uri.find(MEDIALIBRARY_DATA_ABILITY_PREFIX);
107     if (pos == string::npos) {
108         return networkId;
109     }
110     string tempUri = uri.substr(MEDIALIBRARY_DATA_ABILITY_PREFIX.length());
111     if (tempUri.empty()) {
112         return networkId;
113     }
114     pos = tempUri.find_first_of('/');
115     if (pos == 0 || pos == string::npos) {
116         return networkId;
117     }
118     networkId = tempUri.substr(0, pos);
119 
120     return networkId;
121 }
122 
GetDisPlayNameFromPath(const std::string & path)123 string MediaLibraryDataManagerUtils::GetDisPlayNameFromPath(const std::string &path)
124 {
125     string displayName;
126     size_t lastSlashPosition = path.rfind("/");
127     if (lastSlashPosition != string::npos) {
128         displayName = path.substr(lastSlashPosition + 1);
129     }
130     return displayName;
131 }
132 
GetMediaTypeUri(MediaType mediaType)133 string MediaLibraryDataManagerUtils::GetMediaTypeUri(MediaType mediaType)
134 {
135     switch (mediaType) {
136         case MEDIA_TYPE_AUDIO:
137             return MEDIALIBRARY_AUDIO_URI;
138         case MEDIA_TYPE_VIDEO:
139             return MEDIALIBRARY_VIDEO_URI;
140         case MEDIA_TYPE_IMAGE:
141             return MEDIALIBRARY_IMAGE_URI;
142         case MEDIA_TYPE_SMARTALBUM:
143             return MEDIALIBRARY_SMARTALBUM_CHANGE_URI;
144         case MEDIA_TYPE_DEVICE:
145             return MEDIALIBRARY_DEVICE_URI;
146         case MEDIA_TYPE_FILE:
147         default:
148             return MEDIALIBRARY_FILE_URI;
149     }
150 }
151 
SplitKeyValue(const string & keyValue,string & key,string & value)152 void MediaLibraryDataManagerUtils::SplitKeyValue(const string &keyValue, string &key, string &value)
153 {
154     string::size_type pos = keyValue.find('=');
155     if (string::npos != pos) {
156         key = keyValue.substr(0, pos);
157         value = keyValue.substr(pos + 1);
158     }
159 }
160 
SplitKeys(const string & query,vector<string> & keys)161 void MediaLibraryDataManagerUtils::SplitKeys(const string &query, vector<string> &keys)
162 {
163     string::size_type pos1 = 0;
164     string::size_type pos2 = query.find('&');
165     while (string::npos != pos2) {
166         keys.push_back(query.substr(pos1, pos2 - pos1));
167         pos1 = pos2 + 1;
168         pos2 = query.find('&', pos1);
169     }
170     if (pos1 != query.length()) {
171         keys.push_back(query.substr(pos1));
172     }
173 }
174 
ObtionCondition(string & strQueryCondition,const vector<string> & whereArgs)175 string MediaLibraryDataManagerUtils::ObtionCondition(string &strQueryCondition, const vector<string> &whereArgs)
176 {
177     for (string args : whereArgs) {
178         size_t pos = strQueryCondition.find('?');
179         if (pos != string::npos) {
180             strQueryCondition.replace(pos, 1, "'" + args + "'");
181         }
182     }
183     return strQueryCondition;
184 }
185 
RemoveTypeValueFromUri(std::string & uri)186 void MediaLibraryDataManagerUtils::RemoveTypeValueFromUri(std::string &uri)
187 {
188     size_t typeIndex = uri.find('#');
189     if (typeIndex != std::string::npos) {
190         uri = uri.substr(0, typeIndex);
191     }
192 }
193 } // namespace Media
194 } // namespace OHOS
195