• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "utils/mediatool_command_utils.h"
17 
18 #include <unistd.h>
19 #include <regex>
20 
21 #include "medialibrary_errno.h"
22 #include "media_file_utils.h"
23 #include "media_log.h"
24 #include "userfile_client.h"
25 #include "userfilemgr_uri.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace MediaTool {
30 
31 using namespace std;
32 
QueryActiveUserId(string & activeUserId)33 int32_t MediatoolCommandUtils::QueryActiveUserId(string& activeUserId)
34 {
35     std::string queryUserIdUriStr = QUERY_ACTIVE_USER_ID;
36     Uri queryUserIdUri(queryUserIdUriStr);
37     DataShare::DataShareValuesBucket values;
38     const string stubValue = "stub";
39     values.Put(stubValue, 0); // used for not getting error prompt when performing insert operation.
40     auto ret = UserFileClient::InsertExt(queryUserIdUri, values, activeUserId);
41     if (ret != E_OK) {
42         MEDIA_ERR_LOG("mediatool query active user id failed. ret: %{public}d", ret);
43         return ret;
44     }
45     return E_OK;
46 }
47 
CheckAndReformatPathParam(const std::string & inputPath,std::string & reformattedPath)48 bool MediatoolCommandUtils::CheckAndReformatPathParam(const std::string& inputPath, std::string& reformattedPath)
49 {
50     const string basePath = "/storage/media/local/files/Photo";
51 
52     if (MediaFileUtils::StartsWith(inputPath, basePath)) {
53         reformattedPath = inputPath;
54         return true;
55     }
56 
57     string activeUserId = "-1";
58     int32_t ret = QueryActiveUserId(activeUserId);
59     if (ret != E_OK) {
60         MEDIA_ERR_LOG("Failed to get active user: %{public}d", ret);
61         return false;
62     }
63 
64     const string allowedBaseUIDPath = "/storage/media/" + activeUserId + "/local/files/Photo";
65 
66     if (MediaFileUtils::StartsWith(inputPath, allowedBaseUIDPath)) {
67         // reformat "/storage/media/xxx/local/files/Photo" into "/storage/media/local/files/Photo"
68         string extendedPath = inputPath.substr(allowedBaseUIDPath.length());
69         reformattedPath = basePath + extendedPath;
70         return true;
71     }
72 
73     MEDIA_ERR_LOG("Failed to check path param. current user id: %{public}s", activeUserId.c_str());
74     return false;
75 }
76 
77 } // namespace MediaTool
78 } // namespace Media
79 } // namespace OHOS