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 
GetSafeDiaplayName(string & displayName)74 string DfxUtils::GetSafeDiaplayName(string &displayName)
75 {
76     if (displayName == "") {
77         return displayName;
78     }
79     string extension;
80     size_t splitIndex = displayName.find_last_of(DOT);
81     if (splitIndex == string::npos) {
82         extension = "";
83     } else {
84         extension = displayName.substr(splitIndex);
85     }
86     string title = MediaFileUtils::GetTitleFromDisplayName(displayName);
87     if (title == "") {
88         return title;
89     }
90     uint32_t length = title.size();
91     string safeDisplayName;
92     if (length <= GARBLE_SMALL) {
93         safeDisplayName = GARBLE + title.substr(length - GARBLE_LAST_ONE) + extension;
94     } else if (length > GARBLE_LARGE) {
95         safeDisplayName = GARBLE + title.substr(GARBLE_LARGE) + extension;
96     } else {
97         safeDisplayName = GARBLE + title.substr(length - GARBLE_LAST_TWO) + extension;
98     }
99     return safeDisplayName;
100 }
101 
GetCurrentDate()102 string DfxUtils::GetCurrentDate()
103 {
104     auto now = std::chrono::system_clock::now();
105     auto time = std::chrono::system_clock::to_time_t(now);
106     std::tm* tmPtr = std::localtime(&time);
107     if (tmPtr == nullptr) {
108         MEDIA_ERR_LOG("GetCurrentDate failed: tmPtr is nullptr");
109         return "";
110     }
111     std::stringstream ss;
112     ss << std::put_time(tmPtr, "%Y-%m-%d");
113     return ss.str();
114 }
115 
GetCurrentDateMillisecond()116 string DfxUtils::GetCurrentDateMillisecond()
117 {
118     auto now = std::chrono::system_clock::now();
119     std::time_t now_time = std::chrono::system_clock::to_time_t(now);
120     std::tm* now_tm = std::localtime(&now_time);
121     if (now_tm == nullptr) {
122         MEDIA_ERR_LOG("GetCurrentDateMillisecond failed: now_tm is nullptr");
123         return "";
124     }
125     auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
126     std::chrono::duration<int, std::milli> ms_part = now_ms.time_since_epoch() % std::chrono::seconds(ONE_SECOND);
127 
128     std::stringstream ss;
129     ss << (now_tm->tm_year + BASEYEAR) << '-'
130         << std::setw(LENGTH_TWO) << std::setfill('0') << (now_tm->tm_mon + ONE_MORE) << '-'
131         << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_mday << ' '
132         << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_hour << ':'
133         << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_min << ':'
134         << std::setw(LENGTH_TWO) << std::setfill('0') << now_tm->tm_sec << '.'
135         << std::setw(LENGTH_THREE) << ms_part.count();
136     return ss.str();
137 }
138 
JoinStrings(const unordered_set<string> & strSet,char delimiter)139 string DfxUtils::JoinStrings(const unordered_set<string>& strSet, char delimiter)
140 {
141     string result;
142     for (auto it = strSet.begin(); it != strSet.end(); ++it) {
143         if (it != strSet.begin()) {
144             result += delimiter;
145         }
146         result += *it;
147     }
148     return result;
149 }
150 
SplitString(const string & input,char delimiter)151 unordered_set<string> DfxUtils::SplitString(const string& input, char delimiter)
152 {
153     if (input.empty()) {
154         return {};
155     }
156 
157     unordered_set<std::string> result;
158     std::istringstream iss(input);
159     string token;
160     while (std::getline(iss, token, delimiter)) {
161         result.emplace(token);
162     }
163     return result;
164 }
165 
GetSafeAlbumName(const string & albumName)166 string DfxUtils::GetSafeAlbumName(const string& albumName)
167 {
168     if (albumName == "") {
169         return albumName;
170     }
171     uint32_t length = albumName.size();
172     string safeAlbumName;
173     if (length <= GARBLE_SMALL) {
174         safeAlbumName = GARBLE + albumName.substr(length - GARBLE_LAST_ONE);
175     } else if (length > GARBLE_LARGE) {
176         safeAlbumName = GARBLE + albumName.substr(GARBLE_LARGE);
177     } else {
178         safeAlbumName = GARBLE + albumName.substr(length - GARBLE_LAST_TWO);
179     }
180     return safeAlbumName;
181 }
182 
183 } // namespace Media
184 } // namespace OHOS