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