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_file_cache_napi.h"
17
18 #include <sys/types.h>
19
20 #include "async_work.h"
21 #include "cloud_sync_manager.h"
22 #include "dfs_error.h"
23 #include "utils_log.h"
24 #include "uv.h"
25
26 namespace OHOS::FileManagement::CloudSync {
27 using namespace FileManagement::LibN;
28 using namespace std;
29
CleanCloudFileCache(napi_env env,napi_callback_info info)30 napi_value CloudFileCacheNapi::CleanCloudFileCache(napi_env env, napi_callback_info info)
31 {
32 LOGI("CleanCache start");
33 NFuncArg funcArg(env, info);
34
35 if (!funcArg.InitArgs(NARG_CNT::ONE)) {
36 NError(E_PARAMS).ThrowErr(env);
37 return nullptr;
38 }
39
40 auto [succ, uri, ignore] = NVal(env, funcArg[(int)NARG_POS::FIRST]).ToUTF8String();
41 if (!succ) {
42 LOGE("Get uri error");
43 NError(EINVAL).ThrowErr(env);
44 return nullptr;
45 }
46
47 int32_t ret = CloudSyncManager::GetInstance().CleanCache(uri.get());
48 if (ret != E_OK) {
49 NError(Convert2JsErrNum(ret)).ThrowErr(env);
50 return nullptr;
51 }
52 return NVal::CreateUndefined(env).val_;
53 }
54
Export()55 bool CloudFileCacheNapi::Export()
56 {
57 std::vector<napi_property_descriptor> props = {
58 NVal::DeclareNapiFunction("on", CloudFileCacheNapi::On),
59 NVal::DeclareNapiFunction("off", CloudFileCacheNapi::Off),
60 NVal::DeclareNapiFunction("start", CloudFileCacheNapi::StartFileCache),
61 NVal::DeclareNapiFunction("stop", CloudFileCacheNapi::Stop),
62 NVal::DeclareNapiFunction("cleanCache", CloudFileCacheNapi::CleanCloudFileCache),
63 };
64
65 SetClassName("CloudFileCache");
66 return ToExport(props);
67 }
68
StartFileCache(napi_env env,napi_callback_info info)69 napi_value CloudFileCacheNapi::StartFileCache(napi_env env, napi_callback_info info)
70 {
71 LOGI("Start begin");
72 NFuncArg funcArg(env, info);
73 if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
74 LOGE("Start Number of arguments unmatched");
75 NError(E_PARAMS).ThrowErr(env);
76 return nullptr;
77 }
78 auto [succUri, uri, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String();
79 if (!succUri) {
80 LOGE("Start get uri parameter failed!");
81 NError(E_PARAMS).ThrowErr(env);
82 return nullptr;
83 }
84
85 auto cbExec = [uri = string(uri.get())]() -> NError {
86 int32_t ret = CloudSyncManager::GetInstance().StartFileCache(uri);
87 if (ret != E_OK) {
88 LOGE("Start Download failed! ret = %{public}d", ret);
89 return NError(Convert2JsErrNum(ret));
90 }
91 LOGI("Start Download Success!");
92 return NError(ERRNO_NOERR);
93 };
94
95 auto cbCompl = [](napi_env env, NError err) -> NVal {
96 if (err) {
97 return {env, err.GetNapiErr(env)};
98 }
99 return NVal::CreateUndefined(env);
100 };
101
102 string procedureName = "cloudFileCache";
103 auto asyncWork = GetPromiseOrCallBackWork(env, funcArg, static_cast<size_t>(NARG_CNT::TWO));
104 return asyncWork == nullptr ? nullptr : asyncWork->Schedule(procedureName, cbExec, cbCompl).val_;
105 }
106 } // namespace OHOS::FileManagement::CloudSync