1 /*
2 * Copyright (C) 2024 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 "dfx_utils.h"
17
18 #include <chrono>
19 #include <iomanip>
20 #include <sstream>
21
22 #include "dfx_const.h"
23 #include "media_file_utils.h"
24 #include "media_log.h"
25 using namespace std;
26 namespace OHOS {
27 namespace Media {
28 namespace {
29 constexpr int ONE_MORE = 1;
30 constexpr int ONE_SECOND = 1;
31 constexpr int32_t BASEYEAR = 1900;
32 constexpr int32_t LENGTH_TWO = 2;
33 constexpr int32_t LENGTH_THREE = 3;
34 }
Split(string & input,const string & pattern)35 vector<string> DfxUtils::Split(string &input, const string &pattern)
36 {
37 vector<string> result;
38 if (input == "") {
39 return result;
40 }
41 string strs = input + pattern;
42 size_t pos = strs.find(pattern);
43 while (pos != strs.npos) {
44 string temp = strs.substr(0, pos);
45 result.push_back(temp);
46 strs = strs.substr(pos + 1, strs.size());
47 pos = strs.find(pattern);
48 }
49 return result;
50 }
51
GetSafePath(const string & path)52 string DfxUtils::GetSafePath(const string &path)
53 {
54 string safePath = path;
55 if (path == "") {
56 return safePath;
57 }
58 if (MediaFileUtils::StartsWith(path, CLOUD_PHOTO_PATH)) {
59 return safePath.replace(0, CLOUD_PHOTO_PATH.length(), GARBLE);
60 }
61 safePath = safePath.replace(0, CLOUD_FILE_PATH.length(), GARBLE);
62 size_t splitIndex = safePath.find_last_of(SPLIT_PATH);
63 string displayName;
64 if (splitIndex == string::npos) {
65 displayName = "";
66 } else {
67 displayName = safePath.substr(splitIndex + 1);
68 }
69 string safeDisplayName = GetSafeDiaplayName(displayName);
70 safePath = safePath.substr(0, splitIndex) + safeDisplayName;
71 return safePath;
72 }
73
GetSafeUri(const string & uri)74 string DfxUtils::GetSafeUri(const string &uri)
75 {
76 string safeUri = uri;
77 if (uri == "") {
78 return safeUri;
79 }
80 size_t splitIndex = safeUri.find_last_of(SPLIT_PATH);
81 string displayName;
82 if (splitIndex == string::npos) {
83 return safeUri;
84 } else {
85 displayName = safeUri.substr(splitIndex + 1);
86 }
87 string safeDisplayName = GetSafeDiaplayName(displayName);
88 safeUri = safeUri.substr(0, splitIndex) + "/" + safeDisplayName;
89 return safeUri;
90 }
91
GetSafeDiaplayName(string & displayName)92 string DfxUtils::GetSafeDiaplayName(string &displayName)
93 {
94 if (displayName == "") {
95 return displayName;
96 }
97 string extension;
98 size_t splitIndex = displayName.find_last_of(DOT);
99 if (splitIndex == string::npos) {
100 extension = "";
101 } else {
102 extension = displayName.substr(splitIndex);
103 }
104 string title = MediaFileUtils::GetTitleFromDisplayName(displayName);
105 if (title == "") {
106 return title;
107 }
108 uint32_t length = title.size();
109 string safeDisplayName;
110 if (length <= GARBLE_SMALL) {
111 safeDisplayName = GARBLE + title.substr(length - GARBLE_LAST_ONE) + extension;
112 } else if (length > GARBLE_LARGE) {
113 safeDisplayName = GARBLE + title.substr(GARBLE_LARGE) + extension;
114 } else {
115 safeDisplayName = GARBLE + title.substr(length - GARBLE_LAST_TWO) + extension;
116 }
117 return safeDisplayName;
118 }
119
GetCurrentDate()120 string DfxUtils::GetCurrentDate()
121 {
122 auto now = std::chrono::system_clock::now();
123 auto time = std::chrono::system_clock::to_time_t(now);
124 std::tm* tmPtr = std::localtime(&time);
125 if (tmPtr == nullptr) {
126 MEDIA_ERR_LOG("GetCurrentDate failed: tmPtr is nullptr");
127 return "";
128 }
129 std::stringstream ss;
130 ss << std::put_time(tmPtr, "%Y-%m-%d");
131 return ss.str();
132 }
133
GetCurrentDateMillisecond()134 string DfxUtils::GetCurrentDateMillisecond()
135 {
136 auto now = std::chrono::system_clock::now();
137 std::time_t now_time = std::chrono::system_clock::to_time_t(now);
138 std::tm* now_tm = std::localtime(&now_time);
139 if (now_tm == nullptr) {
140 MEDIA_ERR_LOG("GetCurrentDateMillisecond failed: now_tm is nullptr");
141 return "";
142 }
143 auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
144 std::chrono::duration<int, std::milli> ms_part = now_ms.time_since_epoch() % std::chrono::seconds(ONE_SECOND);
145
146 std::stringstream ss;
147 ss << (now_tm->tm_year + BASEYEAR) << '-'
148 << std::setw(LENGTH_TWO) << std::setfill('0') << (now_tm->tm_mon + ONE_MORE) << '-'
149 << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_mday << ' '
150 << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_hour << ':'
151 << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_min << ':'
152 << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_sec << '.'
153 << std::setw(LENGTH_THREE) << ms_part.count();
154 return ss.str();
155 }
156
JoinStrings(const unordered_set<string> & strSet,char delimiter)157 string DfxUtils::JoinStrings(const unordered_set<string>& strSet, char delimiter)
158 {
159 string result;
160 for (auto it = strSet.begin(); it != strSet.end(); ++it) {
161 if (it != strSet.begin()) {
162 result += delimiter;
163 }
164 result += *it;
165 }
166 return result;
167 }
168
SplitString(const string & input,char delimiter)169 unordered_set<string> DfxUtils::SplitString(const string& input, char delimiter)
170 {
171 if (input.empty()) {
172 return {};
173 }
174
175 unordered_set<std::string> result;
176 std::istringstream iss(input);
177 string token;
178 while (std::getline(iss, token, delimiter)) {
179 result.emplace(token);
180 }
181 return result;
182 }
183
GetSafeAlbumName(const string & albumName)184 string DfxUtils::GetSafeAlbumName(const string& albumName)
185 {
186 if (albumName == "") {
187 return albumName;
188 }
189 uint32_t length = albumName.size();
190 string safeAlbumName;
191 if (length <= GARBLE_SMALL) {
192 safeAlbumName = GARBLE + albumName.substr(length - GARBLE_LAST_ONE);
193 } else if (length > GARBLE_LARGE) {
194 safeAlbumName = GARBLE + albumName.substr(GARBLE_LARGE);
195 } else {
196 safeAlbumName = GARBLE + albumName.substr(length - GARBLE_LAST_TWO);
197 }
198 return safeAlbumName;
199 }
200
201 } // namespace Media
202 } // namespace OHOS