1 /*
2  * Copyright (c) 2021-2022 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 #ifndef INTERFACES_KITS_JS_SRC_MOD_FILE_CLASS_FILE_FILE_N_EXPORTER_H
17 #define INTERFACES_KITS_JS_SRC_MOD_FILE_CLASS_FILE_FILE_N_EXPORTER_H
18 
19 #include "../../common/napi/n_exporter.h"
20 
21 namespace OHOS {
22 namespace DistributedFS {
23 namespace ModuleFile {
24 namespace {
25     constexpr int32_t PARAMS_NUMBER_THREE = 3;
26     constexpr int32_t DEFAULT_RESULT = -100;
27 }
28 enum COMMON_NUM {
29     ZERO = 0,
30     ONE = 1,
31     TWO = 2,
32     THOUSAND = 1000,
33     MILLION = 1000000,
34 };
35 
36 struct FileInfo {
37     int32_t length = 0;
38     int64_t lastModifiedTime = 0;
39     std::string type = "";
40     std::string uri = "";
41 };
42 
43 struct AsyncAccessCallbackInfo {
44     napi_env env = nullptr;
45     napi_async_work asyncWork = nullptr;
46     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
47     std::string url = "";
48     int errorType = -1;
49     int result = DEFAULT_RESULT;
50 };
51 
52 struct AsyncMkdirCallbackInfo {
53     napi_env env = nullptr;
54     napi_async_work asyncWork = nullptr;
55     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
56     bool recursive = false;
57     std::string url = "";
58     int result = DEFAULT_RESULT;
59     int errorType = -1;
60 };
61 
62 struct AsyncRmdirCallbackInfo {
63     napi_env env = nullptr;
64     napi_async_work asyncWork = nullptr;
65     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
66     bool recursive = false;
67     std::string url = "";
68     int result = DEFAULT_RESULT;
69     int errorType = -1;
70 };
71 
72 struct AsyncGetCallbackInfo {
73     napi_env env = nullptr;
74     napi_async_work asyncWork = nullptr;
75     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
76     bool recursive = false;
77     std::string url = "";
78     std::string originUri = "";
79     int result = DEFAULT_RESULT;
80     int errorType = -1;
81     int32_t length = 0;
82     int64_t lastMT = 0;
83     std::string type = "";
84     std::vector<std::string> subFiles;
85 };
86 
87 struct AsyncListCallbackInfo {
88     napi_env env = nullptr;
89     napi_async_work asyncWork = nullptr;
90     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
91     bool recursive = false;
92     std::string url = "";
93     std::string originUri = "";
94     int result = DEFAULT_RESULT;
95     int errorType = -1;
96     std::vector<FileInfo> fileList;
97 };
98 
99 struct AsyncCopyCallbackInfo {
100     napi_env env = nullptr;
101     napi_async_work asyncWork = nullptr;
102     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
103     std::string url = "";
104     std::string urlDst = "";
105     std::string originDst = "";
106     int result = DEFAULT_RESULT;
107     int errorType = -1;
108 };
109 
110 struct AsyncMoveCallbackInfo {
111     napi_env env = nullptr;
112     napi_async_work asyncWork = nullptr;
113     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
114     std::string url = "";
115     std::string urlDst = "";
116     std::string originDst = "";
117     int result = DEFAULT_RESULT;
118     int errorType = -1;
119 };
120 
121 struct AsyncDeleteCallbackInfo {
122     napi_env env = nullptr;
123     napi_async_work asyncWork = nullptr;
124     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
125     std::string url = "";
126     int result = DEFAULT_RESULT;
127     int errorType = -1;
128 };
129 
130 struct AsyncWriteCallbackInfo {
131     napi_env env = nullptr;
132     napi_async_work asyncWork = nullptr;
133     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
134     std::string url = "";
135     std::string text = "";
136     bool append = false;
137     int result = DEFAULT_RESULT;
138     int errorType = -1;
139 };
140 
141 struct AsyncWriteBufferCallbackInfo {
142     napi_env env = nullptr;
143     napi_async_work asyncWork = nullptr;
144     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
145     std::string url = "";
146     bool append = false;
147     int result = DEFAULT_RESULT;
148     int errorType = -1;
149     int32_t length = 0;
150     int32_t position = 0;
151     void* buf = nullptr;
152     napi_ref bufferAddress = nullptr;
153 };
154 
155 struct AsyncReadCallbackInfo {
156     napi_env env = nullptr;
157     napi_async_work asyncWork = nullptr;
158     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
159     std::string url = "";
160     int result = DEFAULT_RESULT;
161     int errorType = -1;
162     std::string contents = "";
163 };
164 
165 struct AsyncReadBufferCallbackInfo {
166     napi_env env = nullptr;
167     napi_async_work asyncWork = nullptr;
168     napi_ref callback[PARAMS_NUMBER_THREE] = { 0 };
169     std::string url = "";
170     int length = 0;
171     int position = 0;
172     int result = DEFAULT_RESULT;
173     int errorType = -1;
174     int32_t len = 0;
175     std::string contents = "";
176 };
177 
178 class FileNExporter final : public NExporter {
179 public:
180     inline static const std::string className_ = "File";
181     static napi_value Mkdir(napi_env env, napi_callback_info info);
182     static napi_value Rmdir(napi_env env, napi_callback_info info);
183     static napi_value Get(napi_env env, napi_callback_info info);
184     static napi_value List(napi_env env, napi_callback_info info);
185     static napi_value Copy(napi_env env, napi_callback_info info);
186     static napi_value Move(napi_env env, napi_callback_info info);
187     static napi_value Delete(napi_env env, napi_callback_info info);
188     static napi_value Access(napi_env env, napi_callback_info info);
189     static napi_value WriteText(napi_env env, napi_callback_info info);
190     static napi_value WriteArrayBuffer(napi_env env, napi_callback_info info);
191     static napi_value ReadText(napi_env env, napi_callback_info info);
192     static napi_value ReadArrayBuffer(napi_env env, napi_callback_info info);
193 
194     bool Export() override;
195 
196     std::string GetClassName() override;
197 
198     FileNExporter(napi_env env, napi_value exports);
199     ~FileNExporter() override;
200 };
201 }  // namespace ModuleFile
202 }  // namespace DistributedFS
203 }  // namespace OHOS
204 #endif // INTERFACES_KITS_JS_SRC_MOD_FILE_CLASS_FILE_FILE_N_EXPORTER_H
205