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,431 +1,431 @@
1
- package com.ReactNativeBlobUtil;
2
-
3
- import android.app.Activity;
4
- import android.app.DownloadManager;
5
- import android.content.ActivityNotFoundException;
6
- import android.content.Intent;
7
- import android.net.Uri;
8
- import android.os.Build;
9
- import android.util.SparseArray;
10
-
11
- import androidx.annotation.RequiresApi;
12
- import androidx.core.content.FileProvider;
13
-
14
- import com.ReactNativeBlobUtil.Utils.FileDescription;
15
- import com.facebook.react.bridge.ActivityEventListener;
16
- import com.facebook.react.bridge.Callback;
17
- import com.facebook.react.bridge.LifecycleEventListener;
18
- import com.facebook.react.bridge.Promise;
19
- import com.facebook.react.bridge.ReactApplicationContext;
20
- import com.facebook.react.bridge.ReadableArray;
21
- import com.facebook.react.bridge.ReadableMap;
22
- import com.facebook.react.bridge.WritableMap;
23
- import com.facebook.react.modules.network.CookieJarContainer;
24
- import com.facebook.react.modules.network.ForwardingCookieHandler;
25
- import com.facebook.react.modules.network.OkHttpClientProvider;
26
-
27
- import java.io.File;
28
- import java.util.concurrent.LinkedBlockingQueue;
29
- import java.util.concurrent.ThreadPoolExecutor;
30
- import java.util.concurrent.TimeUnit;
31
-
32
- import javax.annotation.Nullable;
33
-
34
- import okhttp3.JavaNetCookieJar;
35
- import okhttp3.OkHttpClient;
36
-
37
- import static android.app.Activity.RESULT_OK;
38
- import static com.ReactNativeBlobUtil.ReactNativeBlobUtilConst.GET_CONTENT_INTENT;
39
-
40
- class ReactNativeBlobUtilImpl {
41
-
42
- public static final String NAME = "ReactNativeBlobUtil";
43
-
44
- private final OkHttpClient mClient;
45
-
46
- static ReactApplicationContext RCTContext;
47
- private static final LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
48
- private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
49
- static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
50
- private static final ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
51
- private static boolean ActionViewVisible = false;
52
- private static final SparseArray<Promise> promiseTable = new SparseArray<>();
53
-
54
- public ReactNativeBlobUtilImpl(ReactApplicationContext reactContext) {
55
- mClient = OkHttpClientProvider.getOkHttpClient();
56
- ForwardingCookieHandler mCookieHandler = new ForwardingCookieHandler(reactContext);
57
- CookieJarContainer mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
58
- mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));
59
-
60
- RCTContext = reactContext;
61
- reactContext.addActivityEventListener(new ActivityEventListener() {
62
- @Override
63
- public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
64
- if (requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
65
- Uri d = data.getData();
66
- promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString());
67
- promiseTable.remove(GET_CONTENT_INTENT);
68
- }
69
- }
70
-
71
- @Override
72
- public void onNewIntent(Intent intent) {
73
-
74
- }
75
- });
76
- }
77
-
78
- public void createFile(final String path, final String content, final String encode, final Promise promise) {
79
- threadPool.execute(new Runnable() {
80
- @Override
81
- public void run() {
82
- ReactNativeBlobUtilFS.createFile(path, content, encode, promise);
83
- }
84
- });
85
- }
86
-
87
- public void createFileASCII(final String path, final ReadableArray dataArray, final Promise promise) {
88
- threadPool.execute(new Runnable() {
89
- @Override
90
- public void run() {
91
- ReactNativeBlobUtilFS.createFileASCII(path, dataArray, promise);
92
- }
93
- });
94
- }
95
-
96
- public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
97
- try {
98
- Uri uriForFile = null;
99
- if (!ReactNativeBlobUtilUtils.isContentUri(path)) {
100
- uriForFile = FileProvider.getUriForFile(RCTContext,
101
- RCTContext.getPackageName() + ".provider", new File(path));
102
- } else {
103
- uriForFile = Uri.parse(path);
104
- }
105
- Intent intent = new Intent(Intent.ACTION_VIEW);
106
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
107
- // Create the intent with data and type
108
- intent.setDataAndType(uriForFile, mime);
109
-
110
- // Set flag to give temporary permission to external app to use FileProvider
111
- intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
112
- // All the activity to be opened outside of an activity
113
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
114
- } else {
115
- intent.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
116
- }
117
-
118
- if (chooserTitle != null) {
119
- intent = Intent.createChooser(intent, chooserTitle);
120
- }
121
-
122
- try {
123
- RCTContext.startActivity(intent);
124
- promise.resolve(true);
125
- } catch (ActivityNotFoundException ex) {
126
- promise.reject("ENOAPP", "No app installed for " + mime);
127
- }
128
-
129
- ActionViewVisible = true;
130
-
131
- final LifecycleEventListener listener = new LifecycleEventListener() {
132
-
133
- @Override
134
- public void onHostResume() {
135
- if (ActionViewVisible)
136
- promise.resolve(null);
137
- RCTContext.removeLifecycleEventListener(this);
138
- }
139
-
140
- @Override
141
- public void onHostPause() {
142
-
143
- }
144
-
145
- @Override
146
- public void onHostDestroy() {
147
-
148
- }
149
- };
150
- RCTContext.addLifecycleEventListener(listener);
151
- } catch (Exception ex) {
152
- promise.reject("EUNSPECIFIED", ex.getLocalizedMessage());
153
- }
154
- }
155
-
156
- public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) {
157
- ReactNativeBlobUtilStream.writeArrayChunk(streamId, dataArray, callback);
158
- }
159
-
160
- public void unlink(String path, Callback callback) {
161
- ReactNativeBlobUtilFS.unlink(path, callback);
162
- }
163
-
164
- public void mkdir(String path, Promise promise) {
165
- ReactNativeBlobUtilFS.mkdir(path, promise);
166
- }
167
-
168
- public void exists(String path, Callback callback) {
169
- ReactNativeBlobUtilFS.exists(path, callback);
170
- }
171
-
172
- public void cp(final String path, final String dest, final Callback callback) {
173
- threadPool.execute(new Runnable() {
174
- @Override
175
- public void run() {
176
- ReactNativeBlobUtilFS.cp(path, dest, callback);
177
- }
178
- });
179
- }
180
-
181
- public void mv(String path, String dest, Callback callback) {
182
- ReactNativeBlobUtilFS.mv(path, dest, callback);
183
- }
184
-
185
- public void ls(String path, Promise promise) {
186
- ReactNativeBlobUtilFS.ls(path, promise);
187
- }
188
-
189
- public void writeStream(String path, String encode, boolean append, Callback callback) {
190
- new ReactNativeBlobUtilStream(RCTContext).writeStream(path, encode, append, callback);
191
- }
192
-
193
- public void writeChunk(String streamId, String data, Callback callback) {
194
- ReactNativeBlobUtilStream.writeChunk(streamId, data, callback);
195
- }
196
-
197
- public void closeStream(String streamId, Callback callback) {
198
- ReactNativeBlobUtilStream.closeStream(streamId, callback);
199
- }
200
-
201
- public void removeSession(ReadableArray paths, Callback callback) {
202
- ReactNativeBlobUtilFS.removeSession(paths, callback);
203
- }
204
-
205
- public void readFile(final String path, final String encoding, final boolean transformFile, final Promise promise) {
206
- threadPool.execute(new Runnable() {
207
- @Override
208
- public void run() {
209
- ReactNativeBlobUtilFS.readFile(path, encoding, transformFile, promise);
210
- }
211
- });
212
- }
213
-
214
- public void writeFileArray(final String path, final ReadableArray data, final boolean append, final Promise promise) {
215
- threadPool.execute(new Runnable() {
216
- @Override
217
- public void run() {
218
- ReactNativeBlobUtilFS.writeFile(path, data, append, promise);
219
- }
220
- });
221
- }
222
-
223
- public void writeFile(final String path, final String encoding, final String data, final boolean transformFile, final boolean append, final Promise promise) {
224
- threadPool.execute(new Runnable() {
225
- @Override
226
- public void run() {
227
- ReactNativeBlobUtilFS.writeFile(path, encoding, data, transformFile, append, promise);
228
- }
229
- });
230
- }
231
-
232
- public void lstat(String path, Callback callback) {
233
- ReactNativeBlobUtilFS.lstat(path, callback);
234
- }
235
-
236
- public void stat(String path, Callback callback) {
237
- ReactNativeBlobUtilFS.stat(path, callback);
238
- }
239
-
240
- public void scanFile(final ReadableArray pairs, final Callback callback) {
241
- final ReactApplicationContext ctx = RCTContext;
242
- threadPool.execute(new Runnable() {
243
- @Override
244
- public void run() {
245
- int size = pairs.size();
246
- String[] p = new String[size];
247
- String[] m = new String[size];
248
- for (int i = 0; i < size; i++) {
249
- ReadableMap pair = pairs.getMap(i);
250
- if (pair.hasKey("path")) {
251
- p[i] = pair.getString("path");
252
- if (pair.hasKey("mime"))
253
- m[i] = pair.getString("mime");
254
- else
255
- m[i] = null;
256
- }
257
- }
258
- new ReactNativeBlobUtilFS(ctx).scanFile(p, m, callback);
259
- }
260
- });
261
- }
262
-
263
- public void hash(final String path, final String algorithm, final Promise promise) {
264
- threadPool.execute(new Runnable() {
265
- @Override
266
- public void run() {
267
- ReactNativeBlobUtilFS.hash(path, algorithm, promise);
268
- }
269
- });
270
- }
271
-
272
- /**
273
- * @param path Stream file path
274
- * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
275
- * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
276
- */
277
- public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
278
- final ReactApplicationContext ctx = RCTContext;
279
- fsThreadPool.execute(new Runnable() {
280
- @Override
281
- public void run() {
282
- ReactNativeBlobUtilStream fs = new ReactNativeBlobUtilStream(ctx);
283
- fs.readStream(path, encoding, bufferSize, tick, streamId, RCTContext);
284
- }
285
- });
286
- }
287
-
288
- public void cancelRequest(String taskId, Callback callback) {
289
- try {
290
- ReactNativeBlobUtilReq.cancelTask(taskId);
291
- callback.invoke(null, taskId);
292
- } catch (Exception ex) {
293
- callback.invoke(ex.getLocalizedMessage(), null);
294
- }
295
- }
296
-
297
- public void slice(String src, String dest, long start, long end, Promise promise) {
298
- ReactNativeBlobUtilFS.slice(src, dest, start, end, "", promise);
299
- }
300
-
301
- public void enableProgressReport(String taskId, int interval, int count) {
302
- ReactNativeBlobUtilProgressConfig config = new ReactNativeBlobUtilProgressConfig(true, interval, count, ReactNativeBlobUtilProgressConfig.ReportType.Download);
303
- ReactNativeBlobUtilReq.progressReport.put(taskId, config);
304
- }
305
-
306
- public void df(final Callback callback) {
307
- fsThreadPool.execute(new Runnable() {
308
- @Override
309
- public void run() {
310
- ReactNativeBlobUtilFS.df(callback, RCTContext);
311
- }
312
- });
313
- }
314
-
315
-
316
- public void enableUploadProgressReport(String taskId, int interval, int count) {
317
- ReactNativeBlobUtilProgressConfig config = new ReactNativeBlobUtilProgressConfig(true, interval, count, ReactNativeBlobUtilProgressConfig.ReportType.Upload);
318
- ReactNativeBlobUtilReq.uploadProgressReport.put(taskId, config);
319
- }
320
-
321
- public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
322
- new ReactNativeBlobUtilReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
323
- }
324
-
325
- public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
326
- new ReactNativeBlobUtilReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
327
- }
328
-
329
- public void getContentIntent(String mime, Promise promise) {
330
- Intent i = new Intent(Intent.ACTION_GET_CONTENT);
331
- if (mime != null)
332
- i.setType(mime);
333
- else
334
- i.setType("*/*");
335
- promiseTable.put(GET_CONTENT_INTENT, promise);
336
- RCTContext.startActivityForResult(i, GET_CONTENT_INTENT, null);
337
-
338
- }
339
-
340
- public void addCompleteDownload(ReadableMap config, Promise promise) {
341
- DownloadManager dm = (DownloadManager) RCTContext.getSystemService(RCTContext.DOWNLOAD_SERVICE);
342
- if (config == null || !config.hasKey("path")) {
343
- promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload config or path missing.");
344
- return;
345
- }
346
- String path = ReactNativeBlobUtilUtils.normalizePath(config.getString("path"));
347
- if (path == null) {
348
- promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload can not resolve URI:" + config.getString("path"));
349
- return;
350
- }
351
- try {
352
- WritableMap stat = ReactNativeBlobUtilFS.statFile(path);
353
- dm.addCompletedDownload(
354
- config.hasKey("title") ? config.getString("title") : "",
355
- config.hasKey("description") ? config.getString("description") : "",
356
- true,
357
- config.hasKey("mime") ? config.getString("mime") : null,
358
- path,
359
- Long.valueOf(stat.getString("size")),
360
- config.hasKey("showNotification") && config.getBoolean("showNotification")
361
- );
362
- promise.resolve(null);
363
- } catch (Exception ex) {
364
- promise.reject("EUNSPECIFIED", ex.getLocalizedMessage());
365
- }
366
-
367
- }
368
-
369
- public void getSDCardDir(Promise promise) {
370
- ReactNativeBlobUtilFS.getSDCardDir(RCTContext, promise);
371
- }
372
-
373
- public void getSDCardApplicationDir(Promise promise) {
374
- ReactNativeBlobUtilFS.getSDCardApplicationDir(RCTContext, promise);
375
- }
376
-
377
- public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
378
- if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
379
- promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
380
- return;
381
- }
382
- if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
383
-
384
- FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
385
- Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), RCTContext);
386
- if (res != null) promise.resolve(res.toString());
387
- else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
388
- }
389
-
390
- public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
391
- boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise, RCTContext);
392
- if (res) promise.resolve("Success");
393
- }
394
-
395
- @RequiresApi(api = Build.VERSION_CODES.Q)
396
- public void copyToInternal(String contentUri, String destpath, Promise promise) {
397
- ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise);
398
- }
399
-
400
- @RequiresApi(api = Build.VERSION_CODES.Q)
401
- public void getBlob(String contentUri, String encoding, Promise promise) {
402
- ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
403
- }
404
-
405
- public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
406
- if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
407
- promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
408
- return;
409
- }
410
- if (mt == null) {
411
- promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
412
- return;
413
- }
414
- if (path == null) {
415
- promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid path");
416
- return;
417
- }
418
-
419
- FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
420
- Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), RCTContext);
421
-
422
- if (fileuri == null) {
423
- promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
424
- return;
425
- }
426
-
427
- boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise, RCTContext);
428
- if (res) promise.resolve(fileuri.toString());
429
- }
430
-
431
- }
1
+ package com.ReactNativeBlobUtil;
2
+
3
+ import android.app.Activity;
4
+ import android.app.DownloadManager;
5
+ import android.content.ActivityNotFoundException;
6
+ import android.content.Intent;
7
+ import android.net.Uri;
8
+ import android.os.Build;
9
+ import android.util.SparseArray;
10
+
11
+ import androidx.annotation.RequiresApi;
12
+ import androidx.core.content.FileProvider;
13
+
14
+ import com.ReactNativeBlobUtil.Utils.FileDescription;
15
+ import com.facebook.react.bridge.ActivityEventListener;
16
+ import com.facebook.react.bridge.Callback;
17
+ import com.facebook.react.bridge.LifecycleEventListener;
18
+ import com.facebook.react.bridge.Promise;
19
+ import com.facebook.react.bridge.ReactApplicationContext;
20
+ import com.facebook.react.bridge.ReadableArray;
21
+ import com.facebook.react.bridge.ReadableMap;
22
+ import com.facebook.react.bridge.WritableMap;
23
+ import com.facebook.react.modules.network.CookieJarContainer;
24
+ import com.facebook.react.modules.network.ForwardingCookieHandler;
25
+ import com.facebook.react.modules.network.OkHttpClientProvider;
26
+
27
+ import java.io.File;
28
+ import java.util.concurrent.LinkedBlockingQueue;
29
+ import java.util.concurrent.ThreadPoolExecutor;
30
+ import java.util.concurrent.TimeUnit;
31
+
32
+ import javax.annotation.Nullable;
33
+
34
+ import okhttp3.JavaNetCookieJar;
35
+ import okhttp3.OkHttpClient;
36
+
37
+ import static android.app.Activity.RESULT_OK;
38
+ import static com.ReactNativeBlobUtil.ReactNativeBlobUtilConst.GET_CONTENT_INTENT;
39
+
40
+ class ReactNativeBlobUtilImpl {
41
+
42
+ public static final String NAME = "ReactNativeBlobUtil";
43
+
44
+ private final OkHttpClient mClient;
45
+
46
+ static ReactApplicationContext RCTContext;
47
+ private static final LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
48
+ private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
49
+ static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
50
+ private static final ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
51
+ private static boolean ActionViewVisible = false;
52
+ private static final SparseArray<Promise> promiseTable = new SparseArray<>();
53
+
54
+ public ReactNativeBlobUtilImpl(ReactApplicationContext reactContext) {
55
+ mClient = OkHttpClientProvider.getOkHttpClient();
56
+ ForwardingCookieHandler mCookieHandler = new ForwardingCookieHandler(reactContext);
57
+ CookieJarContainer mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
58
+ mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));
59
+
60
+ RCTContext = reactContext;
61
+ reactContext.addActivityEventListener(new ActivityEventListener() {
62
+ @Override
63
+ public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
64
+ if (requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
65
+ Uri d = data.getData();
66
+ promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString());
67
+ promiseTable.remove(GET_CONTENT_INTENT);
68
+ }
69
+ }
70
+
71
+ @Override
72
+ public void onNewIntent(Intent intent) {
73
+
74
+ }
75
+ });
76
+ }
77
+
78
+ public void createFile(final String path, final String content, final String encode, final Promise promise) {
79
+ threadPool.execute(new Runnable() {
80
+ @Override
81
+ public void run() {
82
+ ReactNativeBlobUtilFS.createFile(path, content, encode, promise);
83
+ }
84
+ });
85
+ }
86
+
87
+ public void createFileASCII(final String path, final ReadableArray dataArray, final Promise promise) {
88
+ threadPool.execute(new Runnable() {
89
+ @Override
90
+ public void run() {
91
+ ReactNativeBlobUtilFS.createFileASCII(path, dataArray, promise);
92
+ }
93
+ });
94
+ }
95
+
96
+ public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
97
+ try {
98
+ Uri uriForFile = null;
99
+ if (!ReactNativeBlobUtilUtils.isContentUri(path)) {
100
+ uriForFile = FileProvider.getUriForFile(RCTContext,
101
+ RCTContext.getPackageName() + ".provider", new File(path));
102
+ } else {
103
+ uriForFile = Uri.parse(path);
104
+ }
105
+ Intent intent = new Intent(Intent.ACTION_VIEW);
106
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
107
+ // Create the intent with data and type
108
+ intent.setDataAndType(uriForFile, mime);
109
+
110
+ // Set flag to give temporary permission to external app to use FileProvider
111
+ intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
112
+ // All the activity to be opened outside of an activity
113
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
114
+ } else {
115
+ intent.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
116
+ }
117
+
118
+ if (chooserTitle != null) {
119
+ intent = Intent.createChooser(intent, chooserTitle);
120
+ }
121
+
122
+ try {
123
+ RCTContext.startActivity(intent);
124
+ promise.resolve(true);
125
+ } catch (ActivityNotFoundException ex) {
126
+ promise.reject("ENOAPP", "No app installed for " + mime);
127
+ }
128
+
129
+ ActionViewVisible = true;
130
+
131
+ final LifecycleEventListener listener = new LifecycleEventListener() {
132
+
133
+ @Override
134
+ public void onHostResume() {
135
+ if (ActionViewVisible)
136
+ promise.resolve(null);
137
+ RCTContext.removeLifecycleEventListener(this);
138
+ }
139
+
140
+ @Override
141
+ public void onHostPause() {
142
+
143
+ }
144
+
145
+ @Override
146
+ public void onHostDestroy() {
147
+
148
+ }
149
+ };
150
+ RCTContext.addLifecycleEventListener(listener);
151
+ } catch (Exception ex) {
152
+ promise.reject("EUNSPECIFIED", ex.getLocalizedMessage());
153
+ }
154
+ }
155
+
156
+ public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) {
157
+ ReactNativeBlobUtilStream.writeArrayChunk(streamId, dataArray, callback);
158
+ }
159
+
160
+ public void unlink(String path, Callback callback) {
161
+ ReactNativeBlobUtilFS.unlink(path, callback);
162
+ }
163
+
164
+ public void mkdir(String path, Promise promise) {
165
+ ReactNativeBlobUtilFS.mkdir(path, promise);
166
+ }
167
+
168
+ public void exists(String path, Callback callback) {
169
+ ReactNativeBlobUtilFS.exists(path, callback);
170
+ }
171
+
172
+ public void cp(final String path, final String dest, final Callback callback) {
173
+ threadPool.execute(new Runnable() {
174
+ @Override
175
+ public void run() {
176
+ ReactNativeBlobUtilFS.cp(path, dest, callback);
177
+ }
178
+ });
179
+ }
180
+
181
+ public void mv(String path, String dest, Callback callback) {
182
+ ReactNativeBlobUtilFS.mv(path, dest, callback);
183
+ }
184
+
185
+ public void ls(String path, Promise promise) {
186
+ ReactNativeBlobUtilFS.ls(path, promise);
187
+ }
188
+
189
+ public void writeStream(String path, String encode, boolean append, Callback callback) {
190
+ new ReactNativeBlobUtilStream(RCTContext).writeStream(path, encode, append, callback);
191
+ }
192
+
193
+ public void writeChunk(String streamId, String data, Callback callback) {
194
+ ReactNativeBlobUtilStream.writeChunk(streamId, data, callback);
195
+ }
196
+
197
+ public void closeStream(String streamId, Callback callback) {
198
+ ReactNativeBlobUtilStream.closeStream(streamId, callback);
199
+ }
200
+
201
+ public void removeSession(ReadableArray paths, Callback callback) {
202
+ ReactNativeBlobUtilFS.removeSession(paths, callback);
203
+ }
204
+
205
+ public void readFile(final String path, final String encoding, final boolean transformFile, final Promise promise) {
206
+ threadPool.execute(new Runnable() {
207
+ @Override
208
+ public void run() {
209
+ ReactNativeBlobUtilFS.readFile(path, encoding, transformFile, promise);
210
+ }
211
+ });
212
+ }
213
+
214
+ public void writeFileArray(final String path, final ReadableArray data, final boolean append, final Promise promise) {
215
+ threadPool.execute(new Runnable() {
216
+ @Override
217
+ public void run() {
218
+ ReactNativeBlobUtilFS.writeFile(path, data, append, promise);
219
+ }
220
+ });
221
+ }
222
+
223
+ public void writeFile(final String path, final String encoding, final String data, final boolean transformFile, final boolean append, final Promise promise) {
224
+ threadPool.execute(new Runnable() {
225
+ @Override
226
+ public void run() {
227
+ ReactNativeBlobUtilFS.writeFile(path, encoding, data, transformFile, append, promise);
228
+ }
229
+ });
230
+ }
231
+
232
+ public void lstat(String path, Callback callback) {
233
+ ReactNativeBlobUtilFS.lstat(path, callback);
234
+ }
235
+
236
+ public void stat(String path, Callback callback) {
237
+ ReactNativeBlobUtilFS.stat(path, callback);
238
+ }
239
+
240
+ public void scanFile(final ReadableArray pairs, final Callback callback) {
241
+ final ReactApplicationContext ctx = RCTContext;
242
+ threadPool.execute(new Runnable() {
243
+ @Override
244
+ public void run() {
245
+ int size = pairs.size();
246
+ String[] p = new String[size];
247
+ String[] m = new String[size];
248
+ for (int i = 0; i < size; i++) {
249
+ ReadableMap pair = pairs.getMap(i);
250
+ if (pair.hasKey("path")) {
251
+ p[i] = pair.getString("path");
252
+ if (pair.hasKey("mime"))
253
+ m[i] = pair.getString("mime");
254
+ else
255
+ m[i] = null;
256
+ }
257
+ }
258
+ new ReactNativeBlobUtilFS(ctx).scanFile(p, m, callback);
259
+ }
260
+ });
261
+ }
262
+
263
+ public void hash(final String path, final String algorithm, final Promise promise) {
264
+ threadPool.execute(new Runnable() {
265
+ @Override
266
+ public void run() {
267
+ ReactNativeBlobUtilFS.hash(path, algorithm, promise);
268
+ }
269
+ });
270
+ }
271
+
272
+ /**
273
+ * @param path Stream file path
274
+ * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
275
+ * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
276
+ */
277
+ public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
278
+ final ReactApplicationContext ctx = RCTContext;
279
+ fsThreadPool.execute(new Runnable() {
280
+ @Override
281
+ public void run() {
282
+ ReactNativeBlobUtilStream fs = new ReactNativeBlobUtilStream(ctx);
283
+ fs.readStream(path, encoding, bufferSize, tick, streamId, RCTContext);
284
+ }
285
+ });
286
+ }
287
+
288
+ public void cancelRequest(String taskId, Callback callback) {
289
+ try {
290
+ ReactNativeBlobUtilReq.cancelTask(taskId);
291
+ callback.invoke(null, taskId);
292
+ } catch (Exception ex) {
293
+ callback.invoke(ex.getLocalizedMessage(), null);
294
+ }
295
+ }
296
+
297
+ public void slice(String src, String dest, long start, long end, Promise promise) {
298
+ ReactNativeBlobUtilFS.slice(src, dest, start, end, "", promise);
299
+ }
300
+
301
+ public void enableProgressReport(String taskId, int interval, int count) {
302
+ ReactNativeBlobUtilProgressConfig config = new ReactNativeBlobUtilProgressConfig(true, interval, count, ReactNativeBlobUtilProgressConfig.ReportType.Download);
303
+ ReactNativeBlobUtilReq.progressReport.put(taskId, config);
304
+ }
305
+
306
+ public void df(final Callback callback) {
307
+ fsThreadPool.execute(new Runnable() {
308
+ @Override
309
+ public void run() {
310
+ ReactNativeBlobUtilFS.df(callback, RCTContext);
311
+ }
312
+ });
313
+ }
314
+
315
+
316
+ public void enableUploadProgressReport(String taskId, int interval, int count) {
317
+ ReactNativeBlobUtilProgressConfig config = new ReactNativeBlobUtilProgressConfig(true, interval, count, ReactNativeBlobUtilProgressConfig.ReportType.Upload);
318
+ ReactNativeBlobUtilReq.uploadProgressReport.put(taskId, config);
319
+ }
320
+
321
+ public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
322
+ new ReactNativeBlobUtilReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
323
+ }
324
+
325
+ public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
326
+ new ReactNativeBlobUtilReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
327
+ }
328
+
329
+ public void getContentIntent(String mime, Promise promise) {
330
+ Intent i = new Intent(Intent.ACTION_GET_CONTENT);
331
+ if (mime != null)
332
+ i.setType(mime);
333
+ else
334
+ i.setType("*/*");
335
+ promiseTable.put(GET_CONTENT_INTENT, promise);
336
+ RCTContext.startActivityForResult(i, GET_CONTENT_INTENT, null);
337
+
338
+ }
339
+
340
+ public void addCompleteDownload(ReadableMap config, Promise promise) {
341
+ DownloadManager dm = (DownloadManager) RCTContext.getSystemService(RCTContext.DOWNLOAD_SERVICE);
342
+ if (config == null || !config.hasKey("path")) {
343
+ promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload config or path missing.");
344
+ return;
345
+ }
346
+ String path = ReactNativeBlobUtilUtils.normalizePath(config.getString("path"));
347
+ if (path == null) {
348
+ promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload can not resolve URI:" + config.getString("path"));
349
+ return;
350
+ }
351
+ try {
352
+ WritableMap stat = ReactNativeBlobUtilFS.statFile(path);
353
+ dm.addCompletedDownload(
354
+ config.hasKey("title") ? config.getString("title") : "",
355
+ config.hasKey("description") ? config.getString("description") : "",
356
+ true,
357
+ config.hasKey("mime") ? config.getString("mime") : null,
358
+ path,
359
+ Long.valueOf(stat.getString("size")),
360
+ config.hasKey("showNotification") && config.getBoolean("showNotification")
361
+ );
362
+ promise.resolve(null);
363
+ } catch (Exception ex) {
364
+ promise.reject("EUNSPECIFIED", ex.getLocalizedMessage());
365
+ }
366
+
367
+ }
368
+
369
+ public void getSDCardDir(Promise promise) {
370
+ ReactNativeBlobUtilFS.getSDCardDir(RCTContext, promise);
371
+ }
372
+
373
+ public void getSDCardApplicationDir(Promise promise) {
374
+ ReactNativeBlobUtilFS.getSDCardApplicationDir(RCTContext, promise);
375
+ }
376
+
377
+ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
378
+ if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
379
+ promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
380
+ return;
381
+ }
382
+ if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
383
+
384
+ FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
385
+ Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), RCTContext);
386
+ if (res != null) promise.resolve(res.toString());
387
+ else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
388
+ }
389
+
390
+ public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
391
+ boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise, RCTContext);
392
+ if (res) promise.resolve("Success");
393
+ }
394
+
395
+ @RequiresApi(api = Build.VERSION_CODES.Q)
396
+ public void copyToInternal(String contentUri, String destpath, Promise promise) {
397
+ ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise);
398
+ }
399
+
400
+ @RequiresApi(api = Build.VERSION_CODES.Q)
401
+ public void getBlob(String contentUri, String encoding, Promise promise) {
402
+ ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
403
+ }
404
+
405
+ public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
406
+ if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
407
+ promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
408
+ return;
409
+ }
410
+ if (mt == null) {
411
+ promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
412
+ return;
413
+ }
414
+ if (path == null) {
415
+ promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid path");
416
+ return;
417
+ }
418
+
419
+ FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
420
+ Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), RCTContext);
421
+
422
+ if (fileuri == null) {
423
+ promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
424
+ return;
425
+ }
426
+
427
+ boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise, RCTContext);
428
+ if (res) promise.resolve(fileuri.toString());
429
+ }
430
+
431
+ }