react-native-blob-util 0.22.0 → 0.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/.eslintrc.js +56 -56
  2. package/LICENSE +21 -21
  3. package/Migration.md +41 -41
  4. package/NuGet.config +11 -11
  5. package/README.md +1109 -1104
  6. package/android/gradle.properties +1 -1
  7. package/android/src/main/AndroidManifest.xml +37 -37
  8. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +57 -57
  9. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConst.java +20 -20
  10. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +9 -9
  11. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilImpl.java +431 -431
  12. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilProgressConfig.java +39 -39
  13. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +1014 -1014
  14. package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilDefaultResp.java +98 -98
  15. package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +166 -166
  16. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileDescription.java +19 -19
  17. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileProvider.java +4 -4
  18. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java +70 -70
  19. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/PathResolver.java +223 -223
  20. package/android/src/main/java/com/ReactNativeBlobUtil/Utils/Tls12SocketFactory.java +68 -68
  21. package/android/src/main/res/values/strings.xml +3 -3
  22. package/android/src/main/res/xml/provider_paths.xml +12 -12
  23. package/android.js +58 -58
  24. package/class/ReactNativeBlobUtilCanceledFetchError.js +9 -9
  25. package/class/ReactNativeBlobUtilFile.js +5 -5
  26. package/class/ReactNativeBlobUtilReadStream.js +84 -84
  27. package/class/ReactNativeBlobUtilSession.js +68 -68
  28. package/class/ReactNativeBlobUtilWriteStream.js +50 -50
  29. package/class/StatefulPromise.js +7 -7
  30. package/codegenSpecs/NativeBlobUtils.js +86 -86
  31. package/components/Fetch.onPress.js +10 -10
  32. package/components/Fetch.when.js +15 -15
  33. package/fetch.js +350 -350
  34. package/fs.js +480 -480
  35. package/index.js +64 -64
  36. package/index.js.flow +192 -192
  37. package/index.web.js +1 -1
  38. package/ios/PrivacyInfo.xcprivacy +28 -28
  39. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.h +55 -55
  40. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.mm +992 -992
  41. package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +405 -405
  42. package/ios/ReactNativeBlobUtilConst.h +58 -58
  43. package/ios/ReactNativeBlobUtilConst.mm +47 -47
  44. package/ios/ReactNativeBlobUtilFS.h +108 -108
  45. package/ios/ReactNativeBlobUtilFS.mm +1064 -1064
  46. package/ios/ReactNativeBlobUtilFileTransformer.h +23 -23
  47. package/ios/ReactNativeBlobUtilFileTransformer.mm +20 -20
  48. package/ios/ReactNativeBlobUtilNetwork.h +49 -49
  49. package/ios/ReactNativeBlobUtilNetwork.mm +159 -159
  50. package/ios/ReactNativeBlobUtilProgress.h +32 -32
  51. package/ios/ReactNativeBlobUtilProgress.mm +57 -57
  52. package/ios/ReactNativeBlobUtilReqBuilder.h +37 -37
  53. package/ios/ReactNativeBlobUtilReqBuilder.mm +292 -292
  54. package/ios/ReactNativeBlobUtilRequest.h +48 -48
  55. package/ios/ReactNativeBlobUtilRequest.mm +584 -584
  56. package/ios.js +61 -61
  57. package/json-stream.js +42 -42
  58. package/lib/oboe-browser.js +2703 -2703
  59. package/mediacollection.js +36 -36
  60. package/package.json +68 -68
  61. package/polyfill/Blob.js +362 -362
  62. package/polyfill/Event.js +11 -11
  63. package/polyfill/EventTarget.js +78 -78
  64. package/polyfill/Fetch.js +219 -219
  65. package/polyfill/File.js +27 -27
  66. package/polyfill/FileReader.js +91 -91
  67. package/polyfill/ProgressEvent.js +32 -32
  68. package/polyfill/XMLHttpRequest.js +466 -466
  69. package/polyfill/XMLHttpRequestEventTarget.js +126 -126
  70. package/polyfill/index.js +11 -11
  71. package/react-native-blob-util.podspec +48 -48
  72. package/types.js +69 -69
  73. package/utils/log.js +40 -40
  74. package/utils/unicode.js +7 -7
  75. package/utils/uri.js +33 -33
  76. package/utils/uuid.js +4 -4
  77. package/windows/ExperimentalFeatures.props +33 -33
  78. package/windows/README.md +2 -2
  79. package/windows/ReactNativeBlobUtil/PropertySheet.props +15 -15
  80. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.cpp +1836 -1836
  81. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.def +3 -3
  82. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.h +372 -372
  83. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj +139 -139
  84. package/windows/ReactNativeBlobUtil/ReactNativeBlobUtil.vcxproj.filters +43 -43
  85. package/windows/ReactNativeBlobUtil/ReactPackageProvider.cpp +20 -20
  86. package/windows/ReactNativeBlobUtil/ReactPackageProvider.h +24 -24
  87. package/windows/ReactNativeBlobUtil/ReactPackageProvider.idl +9 -9
  88. package/windows/ReactNativeBlobUtil/codegen/.clang-format +1 -1
  89. package/windows/ReactNativeBlobUtil/codegen/NativeBlobUtilsDataTypes.g.h +42 -42
  90. package/windows/ReactNativeBlobUtil/codegen/NativeBlobUtilsSpec.g.h +353 -353
  91. package/windows/ReactNativeBlobUtil/packages.config +3 -3
  92. package/windows/ReactNativeBlobUtil/packages.lock.json +59 -59
  93. package/windows/ReactNativeBlobUtil/pch.cpp +1 -1
  94. package/windows/ReactNativeBlobUtil/pch.h +30 -30
  95. package/windows/ReactNativeBlobUtil/resource.h +5 -5
  96. package/windows/ReactNativeBlobUtil/targetver.h +8 -8
  97. package/windows/ReactNativeBlobUtil.sln +43 -43
@@ -1,373 +1,373 @@
1
- #pragma once
2
-
3
- #include "pch.h"
4
- #include "resource.h"
5
-
6
- #if __has_include("codegen/NativeBlobUtilsDataTypes.g.h")
7
- #include "codegen/NativeBlobUtilsDataTypes.g.h"
8
- #endif
9
-
10
- #include "codegen/NativeBlobUtilsSpec.g.h"
11
- #include "NativeModules.h"
12
- #include <winrt/Windows.Foundation.h>
13
- #include <winrt/Windows.Security.Cryptography.h>
14
- #include <winrt/Windows.Security.Cryptography.Core.h>
15
- #include <winrt/Windows.Web.Http.Filters.h>
16
- #include <mutex>
17
-
18
- namespace Cryptography = winrt::Windows::Security::Cryptography;
19
- namespace CryptographyCore = winrt::Windows::Security::Cryptography::Core;
20
-
21
- enum struct EncodingOptions { UTF8, BASE64, ASCII, URI };
22
-
23
- struct CancellationDisposable
24
- {
25
- CancellationDisposable() = default;
26
- CancellationDisposable(winrt::Windows::Foundation::IAsyncInfo const& async, std::function<void()>&& onCancel) noexcept;
27
-
28
- CancellationDisposable(CancellationDisposable&& other) noexcept;
29
- CancellationDisposable& operator=(CancellationDisposable&& other) noexcept;
30
-
31
- CancellationDisposable(CancellationDisposable const&) = delete;
32
- CancellationDisposable& operator=(CancellationDisposable const&) = delete;
33
-
34
- ~CancellationDisposable() noexcept;
35
-
36
- void Cancel() noexcept;
37
- private:
38
- winrt::Windows::Foundation::IAsyncInfo m_async{ nullptr };
39
- std::function<void()> m_onCancel;
40
- };
41
-
42
- struct TaskCancellationManager
43
- {
44
- using TaskId = std::string;
45
-
46
- TaskCancellationManager() = default;
47
- ~TaskCancellationManager() noexcept;
48
-
49
- TaskCancellationManager(TaskCancellationManager const&) = delete;
50
- TaskCancellationManager& operator=(TaskCancellationManager const&) = delete;
51
-
52
- winrt::Windows::Foundation::IAsyncAction Add(TaskId taskId, winrt::Windows::Foundation::IAsyncAction const& asyncAction) noexcept;
53
- void Cancel(TaskId taskId) noexcept;
54
-
55
- private:
56
- std::mutex m_mutex; // to protect m_pendingTasks
57
- std::map<TaskId, CancellationDisposable> m_pendingTasks;
58
- };
59
-
60
- struct ReactNativeBlobUtilState
61
- {
62
- /*
63
-
64
- @"state": @"2", // store
65
- @"headers": headers, // store
66
- @"redirects": redirects, //check how to track, store
67
- @"respType" : respType, // store
68
- @"status": [NSNumber numberWithInteger : statusCode] // store
69
- */
70
- std::string state;
71
- winrt::Microsoft::ReactNative::JSValueObject headers;
72
- winrt::Microsoft::ReactNative::JSValueArray redirects;
73
- std::string respType;
74
- int status = 0;
75
-
76
- /*
77
- taskId: string;
78
- state: string;
79
- headers: any;
80
- redirects: string[];
81
- status: number;
82
- respType: "text" | "blob" | "" | "json";
83
- rnfbEncode: "path" | "base64" | "ascii" | "utf8";
84
- timeout: boolean;
85
- */
86
- };
87
-
88
-
89
- struct ReactNativeBlobUtilStream
90
- {
91
- public:
92
- ReactNativeBlobUtilStream(winrt::Windows::Storage::Streams::IRandomAccessStream& _streamInstance, EncodingOptions _encoding) noexcept;
93
- winrt::Windows::Storage::Streams::IRandomAccessStream streamInstance;
94
- const EncodingOptions encoding;
95
- };
96
-
97
- struct ReactNativeBlobUtilConfig
98
- {
99
- public:
100
- ReactNativeBlobUtilConfig(::React::JSValue& options);
101
-
102
- bool overwrite;
103
- std::chrono::seconds timeout;
104
- bool trusty;
105
- bool fileCache;
106
- std::string key;
107
- std::string appendExt;
108
- std::string path;
109
- bool followRedirect;
110
- };
111
-
112
-
113
- struct ReactNativeBlobUtilProgressConfig {
114
- public:
115
- ReactNativeBlobUtilProgressConfig() = default;
116
- ReactNativeBlobUtilProgressConfig(double count_, double interval_);
117
-
118
- double count{ -1.0 };
119
- double interval{ -1.0 };
120
- };
121
-
122
- namespace winrt::ReactNativeBlobUtil
123
- {
124
-
125
- REACT_MODULE(ReactNativeBlobUtil)
126
- struct ReactNativeBlobUtil
127
- {
128
- using ModuleSpec = ReactNativeBlobUtilCodegen::BlobUtilsSpec;
129
- using StreamId = std::string;
130
-
131
- REACT_INIT(Initialize)
132
- void Initialize(React::ReactContext const& reactContext) noexcept;
133
-
134
- REACT_GET_CONSTANTS(GetConstants)
135
- ReactNativeBlobUtilCodegen::BlobUtilsSpec_Constants GetConstants() noexcept;
136
-
137
- REACT_METHOD(fetchBlobForm)
138
- winrt::fire_and_forget fetchBlobForm(::React::JSValue options, std::string taskId, std::string method, std::string url, ::React::JSValue headers, ::React::JSValueArray form, std::function<void(::React::JSValueArray)> callback) noexcept;
139
-
140
- REACT_METHOD(fetchBlob)
141
- winrt::fire_and_forget fetchBlob(::React::JSValue options, std::string taskId, std::string method, std::string url, ::React::JSValue headers, std::string body, std::function<void(::React::JSValueArray)> callback) noexcept;
142
-
143
- REACT_METHOD(createFile)
144
- winrt::fire_and_forget createFile(
145
- std::string path,
146
- std::wstring content,
147
- std::string encoding,
148
- winrt::Microsoft::ReactNative::ReactPromise<void> promise
149
- ) noexcept;
150
-
151
- REACT_METHOD(createFileASCII)
152
- winrt::fire_and_forget createFileASCII(
153
- std::string path,
154
- winrt::Microsoft::ReactNative::JSValueArray dataArray,
155
- winrt::Microsoft::ReactNative::ReactPromise<void> promise) noexcept;
156
-
157
- REACT_METHOD(writeFile)
158
- winrt::fire_and_forget writeFile(
159
- std::string path,
160
- std::string encoding,
161
- std::wstring data,
162
- bool transformFile,
163
- bool append,
164
- winrt::Microsoft::ReactNative::ReactPromise<double> promise) noexcept;
165
-
166
- REACT_METHOD(writeFileArray)
167
- winrt::fire_and_forget writeFileArray(
168
- std::string path,
169
- winrt::Microsoft::ReactNative::JSValueArray dataArray,
170
- bool append,
171
- winrt::Microsoft::ReactNative::ReactPromise<double> promise) noexcept;
172
-
173
- REACT_METHOD(pathForAppGroup)
174
- void pathForAppGroup(std::string groupName, ::React::ReactPromise<std::string>&& result) noexcept;
175
-
176
- REACT_SYNC_METHOD(syncPathAppGroup)
177
- std::string syncPathAppGroup(std::string groupName) noexcept;
178
-
179
- REACT_METHOD(exists)
180
- void exists(std::string path, std::function<void(std::vector<bool> const&)> const& callback) noexcept;
181
-
182
- REACT_METHOD(writeStream)
183
- winrt::fire_and_forget writeStream(std::string path, std::string encoding, bool appendData, std::function<void(::React::JSValueArray)> callback) noexcept;
184
-
185
- REACT_METHOD(writeArrayChunk)
186
- void writeArrayChunk(std::string streamId, ::React::JSValueArray&& dataArray, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
187
-
188
- REACT_METHOD(writeChunk)
189
- void writeChunk(std::string streamId, std::string data, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
190
-
191
- REACT_METHOD(closeStream)
192
- void closeStream(std::string streamId, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
193
-
194
- REACT_METHOD(unlink)
195
- winrt::fire_and_forget unlink(std::string path, std::function<void(::React::JSValueArray)> callback) noexcept;
196
-
197
- REACT_METHOD(removeSession)
198
- winrt::fire_and_forget removeSession(::React::JSValueArray paths, std::function<void(::React::JSValueArray)> callback) noexcept;
199
-
200
- // readFile
201
- REACT_METHOD(readFile)
202
- winrt::fire_and_forget readFile(
203
- std::string path,
204
- std::string encoding,
205
- bool transformFile,
206
- ::React::ReactPromise<::React::JSValueArray> promise) noexcept;
207
-
208
- // hash
209
- REACT_METHOD(hash)
210
- winrt::fire_and_forget hash(
211
- std::string path,
212
- std::string algorithm,
213
- ::React::ReactPromise<std::string> promise) noexcept;
214
-
215
- // ls
216
- REACT_METHOD(ls)
217
- winrt::fire_and_forget ls(
218
- std::string path,
219
- ::React::ReactPromise<::React::JSValueArray> promise) noexcept;
220
-
221
- // mv
222
- REACT_METHOD(mv)
223
- winrt::fire_and_forget mv(
224
- std::string src,
225
- std::string dest,
226
- std::function<void(::React::JSValueArray)> callback) noexcept;
227
-
228
- // cp
229
- REACT_METHOD(cp)
230
- winrt::fire_and_forget cp(
231
- std::string src, // from
232
- std::string dest, // to
233
- std::function<void(::React::JSValueArray)> callback) noexcept;
234
-
235
- // lstat
236
- REACT_METHOD(lstat)
237
- winrt::fire_and_forget lstat(
238
- std::string path,
239
- std::function<void(::React::JSValueArray)> callback) noexcept;
240
-
241
- // stat
242
- REACT_METHOD(stat)
243
- winrt::fire_and_forget stat(
244
- std::string path,
245
- std::function<void(::React::JSValueArray)> callback) noexcept;
246
-
247
- // df
248
- REACT_METHOD(df)
249
- winrt::fire_and_forget df(
250
- std::function<void(::React::JSValueArray)> callback) noexcept;
251
-
252
- REACT_METHOD(slice)
253
- winrt::fire_and_forget slice(
254
- std::string src,
255
- std::string dest,
256
- double start,
257
- double end,
258
- ::React::ReactPromise<std::string> promise) noexcept;
259
-
260
- REACT_METHOD(mkdir)
261
- void mkdir(std::string path, ::React::ReactPromise<bool>&& result) noexcept;
262
-
263
- REACT_METHOD(readStream)
264
- winrt::fire_and_forget readStream(std::string path, std::string encoding, double bufferSize, double tick, std::string streamId) noexcept;
265
-
266
- REACT_METHOD(getEnvironmentDirs)
267
- void getEnvironmentDirs(std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
268
-
269
- REACT_METHOD(cancelRequest)
270
- void cancelRequest(std::string taskId, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
271
-
272
- REACT_METHOD(enableProgressReport)
273
- void enableProgressReport(std::string taskId, double interval, double count) noexcept;
274
-
275
- REACT_METHOD(enableUploadProgressReport)
276
- void enableUploadProgressReport(std::string taskId, double interval, double count) noexcept;
277
-
278
- REACT_METHOD(presentOptionsMenu)
279
- void presentOptionsMenu(std::string uri, std::string scheme, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
280
-
281
- REACT_METHOD(presentOpenInMenu)
282
- void presentOpenInMenu(std::string uri, std::string scheme, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
283
-
284
- REACT_METHOD(presentPreview)
285
- void presentPreview(std::string uri, std::string scheme, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
286
-
287
- REACT_METHOD(excludeFromBackupKey)
288
- void excludeFromBackupKey(std::string url, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
289
-
290
- REACT_METHOD(emitExpiredEvent)
291
- void emitExpiredEvent(std::function<void(std::string)> const& callback) noexcept;
292
-
293
- REACT_METHOD(actionViewIntent)
294
- void actionViewIntent(std::string path, std::string mime, std::string chooserTitle, ::React::ReactPromise<void>&& result) noexcept;
295
-
296
- REACT_METHOD(addCompleteDownload)
297
- void addCompleteDownload(::React::JSValue&& config, ::React::ReactPromise<void>&& result) noexcept;
298
-
299
- REACT_METHOD(copyToInternal)
300
- void copyToInternal(std::string contentUri, std::string destpath, ::React::ReactPromise<std::string>&& result) noexcept;
301
-
302
- REACT_METHOD(copyToMediaStore)
303
- void copyToMediaStore(::React::JSValue&& filedata, std::string mt, std::string path, ::React::ReactPromise<std::string>&& result) noexcept;
304
-
305
- REACT_METHOD(createMediaFile)
306
- void createMediaFile(::React::JSValue&& filedata, std::string mt, ::React::ReactPromise<std::string>&& result) noexcept;
307
-
308
- REACT_METHOD(getBlob)
309
- void getBlob(std::string contentUri, std::string encoding, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
310
-
311
- REACT_METHOD(getContentIntent)
312
- void getContentIntent(std::string mime, ::React::ReactPromise<std::string>&& result) noexcept;
313
-
314
- REACT_METHOD(getSDCardDir)
315
- void getSDCardDir(::React::ReactPromise<std::string>&& result) noexcept;
316
-
317
- REACT_METHOD(getSDCardApplicationDir)
318
- void getSDCardApplicationDir(::React::ReactPromise<std::string>&& result) noexcept;
319
-
320
- REACT_METHOD(scanFile)
321
- void scanFile(::React::JSValueArray&& pairs, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
322
-
323
- REACT_METHOD(writeToMediaFile)
324
- void writeToMediaFile(std::string fileUri, std::string path, bool transformFile, ::React::ReactPromise<std::string>&& result) noexcept;
325
-
326
- REACT_METHOD(addListener)
327
- void addListener(std::string eventName) noexcept;
328
-
329
- REACT_METHOD(removeListeners)
330
- void removeListeners(double count) noexcept;
331
-
332
- private:
333
- React::ReactContext m_context;
334
-
335
- constexpr static int64_t UNIX_EPOCH_IN_WINRT_SECONDS = 11644473600;
336
-
337
- std::map<StreamId, ReactNativeBlobUtilStream> m_streamMap;
338
- //winrt::Windows::Web::Http::HttpClient m_httpClient;
339
- winrt::Microsoft::ReactNative::ReactContext m_reactContext;
340
- TaskCancellationManager m_tasks;
341
-
342
- winrt::Windows::Foundation::IAsyncAction ProcessRequestAsync(
343
- const std::string& taskId,
344
- const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter& filter,
345
- winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
346
- ReactNativeBlobUtilConfig& config,
347
- std::function<void(std::string, std::string, std::string)> callback,
348
- std::string& error) noexcept;
349
-
350
- const std::map<std::string, std::function<CryptographyCore::HashAlgorithmProvider()>> availableHashes{
351
- {"md5", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Md5()); } },
352
- {"sha1", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha1()); } },
353
- {"sha256", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha256()); } },
354
- {"sha384", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha384()); } },
355
- {"sha512", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha512()); } }
356
- };
357
-
358
- void splitPath(const std::string& fullPath,
359
- winrt::hstring& directoryPath,
360
- winrt::hstring& fileName) noexcept;
361
-
362
- void splitPath(const std::wstring& fullPath,
363
- winrt::hstring& directoryPath,
364
- winrt::hstring& fileName) noexcept;
365
-
366
- const std::string prefix{ "ReactNativeBlobUtil-file://" };
367
-
368
- std::mutex m_mutex;
369
- std::map<std::string, ReactNativeBlobUtilProgressConfig> downloadProgressMap;
370
- std::map<std::string, ReactNativeBlobUtilProgressConfig> uploadProgressMap;
371
- };
372
-
1
+ #pragma once
2
+
3
+ #include "pch.h"
4
+ #include "resource.h"
5
+
6
+ #if __has_include("codegen/NativeBlobUtilsDataTypes.g.h")
7
+ #include "codegen/NativeBlobUtilsDataTypes.g.h"
8
+ #endif
9
+
10
+ #include "codegen/NativeBlobUtilsSpec.g.h"
11
+ #include "NativeModules.h"
12
+ #include <winrt/Windows.Foundation.h>
13
+ #include <winrt/Windows.Security.Cryptography.h>
14
+ #include <winrt/Windows.Security.Cryptography.Core.h>
15
+ #include <winrt/Windows.Web.Http.Filters.h>
16
+ #include <mutex>
17
+
18
+ namespace Cryptography = winrt::Windows::Security::Cryptography;
19
+ namespace CryptographyCore = winrt::Windows::Security::Cryptography::Core;
20
+
21
+ enum struct EncodingOptions { UTF8, BASE64, ASCII, URI };
22
+
23
+ struct CancellationDisposable
24
+ {
25
+ CancellationDisposable() = default;
26
+ CancellationDisposable(winrt::Windows::Foundation::IAsyncInfo const& async, std::function<void()>&& onCancel) noexcept;
27
+
28
+ CancellationDisposable(CancellationDisposable&& other) noexcept;
29
+ CancellationDisposable& operator=(CancellationDisposable&& other) noexcept;
30
+
31
+ CancellationDisposable(CancellationDisposable const&) = delete;
32
+ CancellationDisposable& operator=(CancellationDisposable const&) = delete;
33
+
34
+ ~CancellationDisposable() noexcept;
35
+
36
+ void Cancel() noexcept;
37
+ private:
38
+ winrt::Windows::Foundation::IAsyncInfo m_async{ nullptr };
39
+ std::function<void()> m_onCancel;
40
+ };
41
+
42
+ struct TaskCancellationManager
43
+ {
44
+ using TaskId = std::string;
45
+
46
+ TaskCancellationManager() = default;
47
+ ~TaskCancellationManager() noexcept;
48
+
49
+ TaskCancellationManager(TaskCancellationManager const&) = delete;
50
+ TaskCancellationManager& operator=(TaskCancellationManager const&) = delete;
51
+
52
+ winrt::Windows::Foundation::IAsyncAction Add(TaskId taskId, winrt::Windows::Foundation::IAsyncAction const& asyncAction) noexcept;
53
+ void Cancel(TaskId taskId) noexcept;
54
+
55
+ private:
56
+ std::mutex m_mutex; // to protect m_pendingTasks
57
+ std::map<TaskId, CancellationDisposable> m_pendingTasks;
58
+ };
59
+
60
+ struct ReactNativeBlobUtilState
61
+ {
62
+ /*
63
+
64
+ @"state": @"2", // store
65
+ @"headers": headers, // store
66
+ @"redirects": redirects, //check how to track, store
67
+ @"respType" : respType, // store
68
+ @"status": [NSNumber numberWithInteger : statusCode] // store
69
+ */
70
+ std::string state;
71
+ winrt::Microsoft::ReactNative::JSValueObject headers;
72
+ winrt::Microsoft::ReactNative::JSValueArray redirects;
73
+ std::string respType;
74
+ int status = 0;
75
+
76
+ /*
77
+ taskId: string;
78
+ state: string;
79
+ headers: any;
80
+ redirects: string[];
81
+ status: number;
82
+ respType: "text" | "blob" | "" | "json";
83
+ rnfbEncode: "path" | "base64" | "ascii" | "utf8";
84
+ timeout: boolean;
85
+ */
86
+ };
87
+
88
+
89
+ struct ReactNativeBlobUtilStream
90
+ {
91
+ public:
92
+ ReactNativeBlobUtilStream(winrt::Windows::Storage::Streams::IRandomAccessStream& _streamInstance, EncodingOptions _encoding) noexcept;
93
+ winrt::Windows::Storage::Streams::IRandomAccessStream streamInstance;
94
+ const EncodingOptions encoding;
95
+ };
96
+
97
+ struct ReactNativeBlobUtilConfig
98
+ {
99
+ public:
100
+ ReactNativeBlobUtilConfig(::React::JSValue& options);
101
+
102
+ bool overwrite;
103
+ std::chrono::seconds timeout;
104
+ bool trusty;
105
+ bool fileCache;
106
+ std::string key;
107
+ std::string appendExt;
108
+ std::string path;
109
+ bool followRedirect;
110
+ };
111
+
112
+
113
+ struct ReactNativeBlobUtilProgressConfig {
114
+ public:
115
+ ReactNativeBlobUtilProgressConfig() = default;
116
+ ReactNativeBlobUtilProgressConfig(double count_, double interval_);
117
+
118
+ double count{ -1.0 };
119
+ double interval{ -1.0 };
120
+ };
121
+
122
+ namespace winrt::ReactNativeBlobUtil
123
+ {
124
+
125
+ REACT_MODULE(ReactNativeBlobUtil)
126
+ struct ReactNativeBlobUtil
127
+ {
128
+ using ModuleSpec = ReactNativeBlobUtilCodegen::BlobUtilsSpec;
129
+ using StreamId = std::string;
130
+
131
+ REACT_INIT(Initialize)
132
+ void Initialize(React::ReactContext const& reactContext) noexcept;
133
+
134
+ REACT_GET_CONSTANTS(GetConstants)
135
+ ReactNativeBlobUtilCodegen::BlobUtilsSpec_Constants GetConstants() noexcept;
136
+
137
+ REACT_METHOD(fetchBlobForm)
138
+ winrt::fire_and_forget fetchBlobForm(::React::JSValue options, std::string taskId, std::string method, std::string url, ::React::JSValue headers, ::React::JSValueArray form, std::function<void(::React::JSValueArray)> callback) noexcept;
139
+
140
+ REACT_METHOD(fetchBlob)
141
+ winrt::fire_and_forget fetchBlob(::React::JSValue options, std::string taskId, std::string method, std::string url, ::React::JSValue headers, std::string body, std::function<void(::React::JSValueArray)> callback) noexcept;
142
+
143
+ REACT_METHOD(createFile)
144
+ winrt::fire_and_forget createFile(
145
+ std::string path,
146
+ std::wstring content,
147
+ std::string encoding,
148
+ winrt::Microsoft::ReactNative::ReactPromise<void> promise
149
+ ) noexcept;
150
+
151
+ REACT_METHOD(createFileASCII)
152
+ winrt::fire_and_forget createFileASCII(
153
+ std::string path,
154
+ winrt::Microsoft::ReactNative::JSValueArray dataArray,
155
+ winrt::Microsoft::ReactNative::ReactPromise<void> promise) noexcept;
156
+
157
+ REACT_METHOD(writeFile)
158
+ winrt::fire_and_forget writeFile(
159
+ std::string path,
160
+ std::string encoding,
161
+ std::wstring data,
162
+ bool transformFile,
163
+ bool append,
164
+ winrt::Microsoft::ReactNative::ReactPromise<double> promise) noexcept;
165
+
166
+ REACT_METHOD(writeFileArray)
167
+ winrt::fire_and_forget writeFileArray(
168
+ std::string path,
169
+ winrt::Microsoft::ReactNative::JSValueArray dataArray,
170
+ bool append,
171
+ winrt::Microsoft::ReactNative::ReactPromise<double> promise) noexcept;
172
+
173
+ REACT_METHOD(pathForAppGroup)
174
+ void pathForAppGroup(std::string groupName, ::React::ReactPromise<std::string>&& result) noexcept;
175
+
176
+ REACT_SYNC_METHOD(syncPathAppGroup)
177
+ std::string syncPathAppGroup(std::string groupName) noexcept;
178
+
179
+ REACT_METHOD(exists)
180
+ void exists(std::string path, std::function<void(std::vector<bool> const&)> const& callback) noexcept;
181
+
182
+ REACT_METHOD(writeStream)
183
+ winrt::fire_and_forget writeStream(std::string path, std::string encoding, bool appendData, std::function<void(::React::JSValueArray)> callback) noexcept;
184
+
185
+ REACT_METHOD(writeArrayChunk)
186
+ void writeArrayChunk(std::string streamId, ::React::JSValueArray&& dataArray, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
187
+
188
+ REACT_METHOD(writeChunk)
189
+ void writeChunk(std::string streamId, std::string data, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
190
+
191
+ REACT_METHOD(closeStream)
192
+ void closeStream(std::string streamId, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
193
+
194
+ REACT_METHOD(unlink)
195
+ winrt::fire_and_forget unlink(std::string path, std::function<void(::React::JSValueArray)> callback) noexcept;
196
+
197
+ REACT_METHOD(removeSession)
198
+ winrt::fire_and_forget removeSession(::React::JSValueArray paths, std::function<void(::React::JSValueArray)> callback) noexcept;
199
+
200
+ // readFile
201
+ REACT_METHOD(readFile)
202
+ winrt::fire_and_forget readFile(
203
+ std::string path,
204
+ std::string encoding,
205
+ bool transformFile,
206
+ ::React::ReactPromise<::React::JSValueArray> promise) noexcept;
207
+
208
+ // hash
209
+ REACT_METHOD(hash)
210
+ winrt::fire_and_forget hash(
211
+ std::string path,
212
+ std::string algorithm,
213
+ ::React::ReactPromise<std::string> promise) noexcept;
214
+
215
+ // ls
216
+ REACT_METHOD(ls)
217
+ winrt::fire_and_forget ls(
218
+ std::string path,
219
+ ::React::ReactPromise<::React::JSValueArray> promise) noexcept;
220
+
221
+ // mv
222
+ REACT_METHOD(mv)
223
+ winrt::fire_and_forget mv(
224
+ std::string src,
225
+ std::string dest,
226
+ std::function<void(::React::JSValueArray)> callback) noexcept;
227
+
228
+ // cp
229
+ REACT_METHOD(cp)
230
+ winrt::fire_and_forget cp(
231
+ std::string src, // from
232
+ std::string dest, // to
233
+ std::function<void(::React::JSValueArray)> callback) noexcept;
234
+
235
+ // lstat
236
+ REACT_METHOD(lstat)
237
+ winrt::fire_and_forget lstat(
238
+ std::string path,
239
+ std::function<void(::React::JSValueArray)> callback) noexcept;
240
+
241
+ // stat
242
+ REACT_METHOD(stat)
243
+ winrt::fire_and_forget stat(
244
+ std::string path,
245
+ std::function<void(::React::JSValueArray)> callback) noexcept;
246
+
247
+ // df
248
+ REACT_METHOD(df)
249
+ winrt::fire_and_forget df(
250
+ std::function<void(::React::JSValueArray)> callback) noexcept;
251
+
252
+ REACT_METHOD(slice)
253
+ winrt::fire_and_forget slice(
254
+ std::string src,
255
+ std::string dest,
256
+ double start,
257
+ double end,
258
+ ::React::ReactPromise<std::string> promise) noexcept;
259
+
260
+ REACT_METHOD(mkdir)
261
+ void mkdir(std::string path, ::React::ReactPromise<bool>&& result) noexcept;
262
+
263
+ REACT_METHOD(readStream)
264
+ winrt::fire_and_forget readStream(std::string path, std::string encoding, double bufferSize, double tick, std::string streamId) noexcept;
265
+
266
+ REACT_METHOD(getEnvironmentDirs)
267
+ void getEnvironmentDirs(std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
268
+
269
+ REACT_METHOD(cancelRequest)
270
+ void cancelRequest(std::string taskId, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
271
+
272
+ REACT_METHOD(enableProgressReport)
273
+ void enableProgressReport(std::string taskId, double interval, double count) noexcept;
274
+
275
+ REACT_METHOD(enableUploadProgressReport)
276
+ void enableUploadProgressReport(std::string taskId, double interval, double count) noexcept;
277
+
278
+ REACT_METHOD(presentOptionsMenu)
279
+ void presentOptionsMenu(std::string uri, std::string scheme, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
280
+
281
+ REACT_METHOD(presentOpenInMenu)
282
+ void presentOpenInMenu(std::string uri, std::string scheme, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
283
+
284
+ REACT_METHOD(presentPreview)
285
+ void presentPreview(std::string uri, std::string scheme, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
286
+
287
+ REACT_METHOD(excludeFromBackupKey)
288
+ void excludeFromBackupKey(std::string url, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
289
+
290
+ REACT_METHOD(emitExpiredEvent)
291
+ void emitExpiredEvent(std::function<void(std::string)> const& callback) noexcept;
292
+
293
+ REACT_METHOD(actionViewIntent)
294
+ void actionViewIntent(std::string path, std::string mime, std::string chooserTitle, ::React::ReactPromise<void>&& result) noexcept;
295
+
296
+ REACT_METHOD(addCompleteDownload)
297
+ void addCompleteDownload(::React::JSValue&& config, ::React::ReactPromise<void>&& result) noexcept;
298
+
299
+ REACT_METHOD(copyToInternal)
300
+ void copyToInternal(std::string contentUri, std::string destpath, ::React::ReactPromise<std::string>&& result) noexcept;
301
+
302
+ REACT_METHOD(copyToMediaStore)
303
+ void copyToMediaStore(::React::JSValue&& filedata, std::string mt, std::string path, ::React::ReactPromise<std::string>&& result) noexcept;
304
+
305
+ REACT_METHOD(createMediaFile)
306
+ void createMediaFile(::React::JSValue&& filedata, std::string mt, ::React::ReactPromise<std::string>&& result) noexcept;
307
+
308
+ REACT_METHOD(getBlob)
309
+ void getBlob(std::string contentUri, std::string encoding, ::React::ReactPromise<::React::JSValueArray>&& result) noexcept;
310
+
311
+ REACT_METHOD(getContentIntent)
312
+ void getContentIntent(std::string mime, ::React::ReactPromise<std::string>&& result) noexcept;
313
+
314
+ REACT_METHOD(getSDCardDir)
315
+ void getSDCardDir(::React::ReactPromise<std::string>&& result) noexcept;
316
+
317
+ REACT_METHOD(getSDCardApplicationDir)
318
+ void getSDCardApplicationDir(::React::ReactPromise<std::string>&& result) noexcept;
319
+
320
+ REACT_METHOD(scanFile)
321
+ void scanFile(::React::JSValueArray&& pairs, std::function<void(::React::JSValueArray const&)> const& callback) noexcept;
322
+
323
+ REACT_METHOD(writeToMediaFile)
324
+ void writeToMediaFile(std::string fileUri, std::string path, bool transformFile, ::React::ReactPromise<std::string>&& result) noexcept;
325
+
326
+ REACT_METHOD(addListener)
327
+ void addListener(std::string eventName) noexcept;
328
+
329
+ REACT_METHOD(removeListeners)
330
+ void removeListeners(double count) noexcept;
331
+
332
+ private:
333
+ React::ReactContext m_context;
334
+
335
+ constexpr static int64_t UNIX_EPOCH_IN_WINRT_SECONDS = 11644473600;
336
+
337
+ std::map<StreamId, ReactNativeBlobUtilStream> m_streamMap;
338
+ //winrt::Windows::Web::Http::HttpClient m_httpClient;
339
+ winrt::Microsoft::ReactNative::ReactContext m_reactContext;
340
+ TaskCancellationManager m_tasks;
341
+
342
+ winrt::Windows::Foundation::IAsyncAction ProcessRequestAsync(
343
+ const std::string& taskId,
344
+ const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter& filter,
345
+ winrt::Windows::Web::Http::HttpRequestMessage& httpRequestMessage,
346
+ ReactNativeBlobUtilConfig& config,
347
+ std::function<void(std::string, std::string, std::string)> callback,
348
+ std::string& error) noexcept;
349
+
350
+ const std::map<std::string, std::function<CryptographyCore::HashAlgorithmProvider()>> availableHashes{
351
+ {"md5", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Md5()); } },
352
+ {"sha1", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha1()); } },
353
+ {"sha256", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha256()); } },
354
+ {"sha384", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha384()); } },
355
+ {"sha512", []() { return CryptographyCore::HashAlgorithmProvider::OpenAlgorithm(CryptographyCore::HashAlgorithmNames::Sha512()); } }
356
+ };
357
+
358
+ void splitPath(const std::string& fullPath,
359
+ winrt::hstring& directoryPath,
360
+ winrt::hstring& fileName) noexcept;
361
+
362
+ void splitPath(const std::wstring& fullPath,
363
+ winrt::hstring& directoryPath,
364
+ winrt::hstring& fileName) noexcept;
365
+
366
+ const std::string prefix{ "ReactNativeBlobUtil-file://" };
367
+
368
+ std::mutex m_mutex;
369
+ std::map<std::string, ReactNativeBlobUtilProgressConfig> downloadProgressMap;
370
+ std::map<std::string, ReactNativeBlobUtilProgressConfig> uploadProgressMap;
371
+ };
372
+
373
373
  } // namespace winrt::ReactNativeBlobUtil