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 "optimize_storage_task.h"
17 #include "cloud_file_kit.h"
18 #include "system_load.h"
19 #include "utils_log.h"
20 
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudSync {
OptimizeStorageTask(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)24 OptimizeStorageTask::OptimizeStorageTask(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
25     : CycleTask("optimize_storage_task", {"com.ohos.photos"}, ONE_DAY, dataSyncManager)
26 {
27 }
28 
RunTaskForBundle(int32_t userId,std::string bundleName)29 int32_t OptimizeStorageTask::RunTaskForBundle(int32_t userId, std::string bundleName)
30 {
31     if (!SystemLoadStatus::IsLoadStatusUnderHot()) {
32         LOGE("OptimizeStorageTask::RunTaskForBundle system load is over hot");
33         return E_STOP;
34     }
35     auto instance = CloudFile::CloudFileKit::GetInstance();
36     if (instance == nullptr) {
37         LOGE("get cloud file helper instance failed");
38         return E_NULLPTR;
39     }
40     auto dataSyncManager_ = GetDataSyncManager();
41     if (dataSyncManager_->CleanRemainFile(bundleName, userId) != E_OK) {
42         LOGW(" clean reamin file fail");
43     }
44     std::map<std::string, std::string> param;
45     auto ret = instance->GetAppConfigParams(userId, bundleName, param);
46     if (ret != E_OK || param.empty()) {
47         LOGE("GetAppConfigParams failed");
48         return ret;
49     }
50 
51     int32_t agingDays = std::stoi(param["validDays"]);
52     int32_t agingPolicy = std::stoi(param["dataAgingPolicy"]);
53     if (agingPolicy == 0) {
54         return dataSyncManager_->OptimizeStorage(bundleName, userId, agingDays);
55     }
56     return E_OK;
57 }
58 } // namespace CloudSync
59 } // namespace FileManagement
60 } // namespace OHOS