1 /*
2 * Copyright (c) 2023 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 "cloud_download_uri_manager.h"
17 #include "dfs_error.h"
18 #include "utils_log.h"
19
20 namespace OHOS::FileManagement::CloudSync {
21
GetInstance()22 CloudDownloadUriManager& CloudDownloadUriManager::GetInstance()
23 {
24 static CloudDownloadUriManager mgr;
25 return mgr;
26 }
27
AddPathToUri(const std::string & path,const std::string & uri)28 int32_t CloudDownloadUriManager::AddPathToUri(const std::string& path, const std::string& uri)
29 {
30 LOGI("download_file : add path [ %{public}s ] -> uri [ %{public}s ]", path.c_str(), uri.c_str());
31 std::lock_guard<std::mutex> lock(mutex_);
32 if (pathMap_.find(path) == pathMap_.end()) {
33 LOGI("download_file : remove path [ %{public}s ] success", path.c_str());
34 pathMap_[path] = uri;
35 return E_OK;
36 }
37 LOGE("file is already trigger downloading");
38 return E_STOP;
39 }
40
RemoveUri(const std::string & path)41 void CloudDownloadUriManager::RemoveUri(const std::string& path)
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 if (pathMap_.find(path) != pathMap_.end()) {
45 LOGI("download_file : remove path [ %{public}s ] success", path.c_str());
46 pathMap_.erase(path);
47 }
48 }
49
GetUri(const std::string & path)50 std::string CloudDownloadUriManager::GetUri(const std::string& path)
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (pathMap_.find(path) != pathMap_.end()) {
54 LOGI("download_file : get path [ %{public}s ] success", path.c_str());
55 return pathMap_[path];
56 }
57
58 LOGE("download_file : get path [ %{public}s ] fail", path.c_str());
59 return "";
60 }
61
62 } // namespace OHOS::FileManagement::CloudSync
63