forked from birdofpreyru/react-native-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactNativeModule.h
192 lines (141 loc) · 7.44 KB
/
ReactNativeModule.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once
#include "codegen/NativeReactNativeFsSpec.g.h"
#include "JSValue.h"
#include "NativeModules.h"
#include <mutex>
#include <winrt/Windows.Security.Cryptography.h>
#include <winrt/Windows.Security.Cryptography.Core.h>
#include <winrt/Windows.Web.Http.h>
using namespace winrt::Microsoft::ReactNative;
namespace Cryptography = winrt::Windows::Security::Cryptography;
namespace CryptographyCore = winrt::Windows::Security::Cryptography::Core;
namespace winrt::ReactNativeFs
{
struct CancellationDisposable final
{
CancellationDisposable() = default;
CancellationDisposable(winrt::Windows::Foundation::IAsyncInfo const& async, std::function<void()>&& onCancel) noexcept;
CancellationDisposable(CancellationDisposable&& other) noexcept;
CancellationDisposable& operator=(CancellationDisposable&& other) noexcept;
CancellationDisposable(CancellationDisposable const&) = delete;
CancellationDisposable& operator=(CancellationDisposable const&) = delete;
~CancellationDisposable() noexcept;
void Cancel() noexcept;
private:
winrt::Windows::Foundation::IAsyncInfo m_async{ nullptr };
std::function<void()> m_onCancel;
};
struct TaskCancellationManager final
{
using JobId = int32_t;
TaskCancellationManager() = default;
~TaskCancellationManager() noexcept;
TaskCancellationManager(TaskCancellationManager const&) = delete;
TaskCancellationManager& operator=(TaskCancellationManager const&) = delete;
winrt::Windows::Foundation::IAsyncAction Add(JobId jobId, winrt::Windows::Foundation::IAsyncAction const& asyncAction) noexcept;
void Cancel(JobId jobId) noexcept;
private:
std::mutex m_mutex; // to protect m_pendingTasks
std::map<JobId, CancellationDisposable> m_pendingTasks;
};
REACT_MODULE(ReactNativeModule, L"ReactNativeFs")
struct ReactNativeModule
{
// See https://microsoft.github.io/react-native-windows/docs/native-modules for details on writing native modules
REACT_INIT(Initialize)
void Initialize(ReactContext const &reactContext) noexcept;
REACT_GET_CONSTANTS(GetConstants)
ReactNativeFsSpec_Constants GetConstants() noexcept;
REACT_METHOD(mkdir); // Implemented
winrt::fire_and_forget mkdir(std::string directory, JSValueObject options, ReactPromise<void> promise) noexcept;
REACT_METHOD(moveFile); // Implemented
winrt::fire_and_forget moveFile(
std::string filepath,
std::string destPath,
JSValueObject options,
ReactPromise<void> promise) noexcept;
REACT_METHOD(copyFile); // Implemented
winrt::fire_and_forget copyFile(
std::string filepath,
std::string destPath,
JSValueObject options,
ReactPromise<void> promise) noexcept;
REACT_METHOD(copyFolder); // Implemented
winrt::fire_and_forget copyFolder(
std::string src,
std::string dest,
ReactPromise<void> promise) noexcept;
REACT_METHOD(getFSInfo); // Implemented, no unit tests but cannot be tested
winrt::fire_and_forget getFSInfo(ReactPromise<JSValueObject> promise) noexcept;
REACT_METHOD(unlink); // Implemented
winrt::fire_and_forget unlink(std::string filePath, ReactPromise<void> promise) noexcept;
REACT_METHOD(exists); // Implemented
winrt::fire_and_forget exists(std::string fullpath, ReactPromise<bool> promise) noexcept;
REACT_METHOD(stopDownload); // DOWNLOADER
void stopDownload(int jobID) noexcept;
REACT_METHOD(stopUpload); // DOWNLOADER
void stopUpload(int jobID) noexcept;
REACT_METHOD(readDir); // Implemented
winrt::fire_and_forget readDir(std::string directory, ReactPromise<JSValueArray> promise) noexcept;
REACT_METHOD(stat); // Implemented, unit tests incomplete
winrt::fire_and_forget stat(std::string filepath, ReactPromise<JSValueObject> promise) noexcept;
REACT_METHOD(readFile); // Implemented
winrt::fire_and_forget readFile(std::string filePath, ReactPromise<std::string> promise) noexcept;
REACT_METHOD(read); // Implemented
winrt::fire_and_forget read(
std::string filePath,
uint32_t length,
uint64_t position,
ReactPromise<std::string> promise) noexcept;
REACT_METHOD(hash); // Implemented
winrt::fire_and_forget hash(std::string filepath, std::string algorithm, ReactPromise<std::string> promise) noexcept;
REACT_METHOD(writeFile); // Implemented
winrt::fire_and_forget writeFile(
std::string filePath,
std::string base64Content,
JSValueObject options,
ReactPromise<void> promise) noexcept;
REACT_METHOD(appendFile); // Implemented, no unit tests
winrt::fire_and_forget appendFile(
std::string filepath,
std::string base64Content,
ReactPromise<void> promise
) noexcept;
REACT_METHOD(write); // Implemented
winrt::fire_and_forget write(
std::string filePath,
std::string base64Content,
int position,
ReactPromise<void> promise) noexcept;
REACT_METHOD(downloadFile); // DOWNLOADER
winrt::fire_and_forget downloadFile(JSValueObject options, ReactPromise<JSValueObject> promise) noexcept;
REACT_METHOD(uploadFiles); // DOWNLOADER
winrt::fire_and_forget uploadFiles(JSValueObject options, ReactPromise<JSValueObject> promise) noexcept;
REACT_METHOD(touch); // Implemented
void touch(std::string filepath, int64_t mtime, int64_t ctime, bool modifyCreationTime, ReactPromise<std::string> promise) noexcept;
REACT_EVENT(TimedEvent, L"TimedEventCpp");
std::function<void(int)> TimedEvent;
REACT_EVENT(emitDownloadBegin, L"DownloadBegin");
std::function<void(JSValue)> emitDownloadBegin;
private:
void splitPath(const std::string& fullPath, winrt::hstring& directoryPath, winrt::hstring& fileName) noexcept;
winrt::Windows::Foundation::IAsyncAction ProcessDownloadRequestAsync(ReactPromise<JSValueObject> promise,
winrt::Windows::Web::Http::HttpRequestMessage request, std::wstring_view filePath, int32_t jobId, int64_t progressInterval, int64_t progressDivider);
winrt::Windows::Foundation::IAsyncAction ProcessUploadRequestAsync(ReactPromise<JSValueObject> promise, JSValueObject& options,
winrt::Windows::Web::Http::HttpMethod httpMethod, JSValueArray const& files, int32_t jobId, uint64_t totalUploadSize);
winrt::fire_and_forget copyFolderHelper(
winrt::Windows::Storage::StorageFolder src,
winrt::Windows::Storage::StorageFolder dest) noexcept;
constexpr static int64_t UNIX_EPOCH_IN_WINRT_INTERVAL = 11644473600 * 10000000;
const std::unordered_map<std::string, std::function<CryptographyCore::HashAlgorithmProvider()>> availableHashes{
{"md5", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Md5()); } },
{"sha1", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha1()); } },
{"sha256", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha256()); } },
{"sha384", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha384()); } },
{"sha512", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha512()); } }
};
ReactContext m_reactContext{nullptr};
winrt::Windows::Web::Http::HttpClient m_httpClient;
TaskCancellationManager m_tasks;
};
} // namespace winrt::ReactNativeFs