• 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 #define MLOG_TAG "Media_Cloud_Utils"
17 
18 #include "cloud_media_uri_utils.h"
19 
20 #include "media_log.h"
21 #include "medialibrary_errno.h"
22 #include "string_ex.h"
23 #include "media_file_utils.h"
24 
25 namespace OHOS::Media::CloudSync {
GetFileIdFromUri(const std::string & uri,int32_t & fileUniqueId)26 int32_t CloudMediaUriUtils::GetFileIdFromUri(const std::string &uri, int32_t &fileUniqueId)
27 {
28     static std::string mediaDirPath = "file://media/Photo/";
29     size_t index = uri.find(mediaDirPath);
30     if (index != 0) {
31         MEDIA_INFO_LOG("Failed to get media dir from uri: %{public}s", MediaFileUtils::DesensitizePath(uri).c_str());
32         return E_INVAL_ARG;
33     }
34     std::string uriTails = uri.substr(index + mediaDirPath.length());
35     index = uriTails.find('/');
36     if (index == std::string::npos) {
37         MEDIA_INFO_LOG("Failed to get fileId from uri: %{public}s", MediaFileUtils::DesensitizePath(uri).c_str());
38         return E_INVAL_ARG;
39     }
40     std::string fileId = uriTails.substr(0, index);
41     if (!all_of(fileId.begin(), fileId.end(), ::isdigit)) {
42         MEDIA_INFO_LOG("Invalid fileId from uri: %{public}s", fileId.c_str());
43         return E_INVAL_ARG;
44     }
45     if (!StrToInt(fileId, fileUniqueId)) {
46         MEDIA_INFO_LOG("Invalid meida uri: %{public}s", MediaFileUtils::DesensitizePath(uri).c_str());
47         return E_INVAL_ARG;
48     }
49     return E_OK;
50 }
51 
GetFileIds(const std::vector<std::string> & uris,std::vector<int32_t> & fileIds)52 int32_t CloudMediaUriUtils::GetFileIds(const std::vector<std::string> &uris, std::vector<int32_t> &fileIds)
53 {
54     fileIds.clear();
55     for (auto &uri : uris) {
56         int32_t fileId = 0;
57         auto ret = CloudMediaUriUtils::GetFileIdFromUri(uri, fileId);
58         CHECK_AND_CONTINUE_ERR_LOG(ret == E_OK, "Failed to get fileId from uri: %{public}s", uri.c_str());
59         fileIds.push_back(fileId);
60         MEDIA_DEBUG_LOG("GetFileIds uri:%{public}s, fileId:%{public}d", uri.c_str(), fileId);
61     }
62     return uris.size() == fileIds.size() ? E_OK : E_INVAL_ARG;
63 }
64 }  // namespace OHOS::Media::CloudSync