react-native-blob-util 0.16.4 → 0.17.0

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 (75) hide show
  1. package/.eslintrc.js +29 -3
  2. package/Migration.md +41 -0
  3. package/README.md +13 -6
  4. package/android/build.gradle +19 -1
  5. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilBody.java +6 -6
  6. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +12 -8
  7. package/android/src/main/java/com/ReactNativeBlobUtil/{ReactNativeBlobUtil.java → ReactNativeBlobUtilImpl.java} +19 -78
  8. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java +5 -5
  9. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilPackage.java +31 -21
  10. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +22 -21
  11. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilStream.java +6 -6
  12. package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java +2 -2
  13. package/android/src/newarch/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +281 -0
  14. package/android/src/oldarch/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +242 -0
  15. package/android.js +2 -3
  16. package/class/ReactNativeBlobUtilFile.js +1 -13
  17. package/class/ReactNativeBlobUtilReadStream.js +1 -2
  18. package/class/ReactNativeBlobUtilSession.js +1 -2
  19. package/class/ReactNativeBlobUtilWriteStream.js +1 -3
  20. package/codegenSpecs/NativeBlobUtils.js +77 -0
  21. package/fetch.js +2 -2
  22. package/fs.js +15 -22
  23. package/index.js +2 -2
  24. package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.h +10 -1
  25. package/ios/ReactNativeBlobUtil/{ReactNativeBlobUtil.m → ReactNativeBlobUtil.mm} +334 -41
  26. package/ios/{ReactNativeBlobUtilConst.m → ReactNativeBlobUtilConst.mm} +0 -0
  27. package/ios/ReactNativeBlobUtilFS.h +5 -5
  28. package/ios/{ReactNativeBlobUtilFS.m → ReactNativeBlobUtilFS.mm} +32 -26
  29. package/ios/{ReactNativeBlobUtilFileTransformer.m → ReactNativeBlobUtilFileTransformer.mm} +0 -0
  30. package/ios/ReactNativeBlobUtilNetwork.h +2 -2
  31. package/ios/{ReactNativeBlobUtilNetwork.m → ReactNativeBlobUtilNetwork.mm} +15 -17
  32. package/ios/{ReactNativeBlobUtilProgress.m → ReactNativeBlobUtilProgress.mm} +4 -4
  33. package/ios/{ReactNativeBlobUtilReqBuilder.m → ReactNativeBlobUtilReqBuilder.mm} +4 -4
  34. package/ios/ReactNativeBlobUtilRequest.h +4 -2
  35. package/ios/{ReactNativeBlobUtilRequest.m → ReactNativeBlobUtilRequest.mm} +16 -11
  36. package/ios.js +2 -3
  37. package/mediacollection.js +2 -4
  38. package/package.json +6 -1
  39. package/react-native-blob-util.podspec +21 -1
  40. package/examples/ReactNativeBlobUtil/.buckconfig +0 -6
  41. package/examples/ReactNativeBlobUtil/.eslintrc.js +0 -4
  42. package/examples/ReactNativeBlobUtil/.prettierrc.js +0 -6
  43. package/examples/ReactNativeBlobUtil/App.js +0 -698
  44. package/examples/ReactNativeBlobUtil/index.js +0 -9
  45. package/examples/ReactNativeBlobUtil/metro.config.js +0 -27
  46. package/examples/ReactNativeBlobUtil/package.json +0 -31
  47. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/App.cpp +0 -80
  48. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/App.h +0 -20
  49. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/App.idl +0 -3
  50. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/App.xaml +0 -10
  51. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/LockScreenLogo.scale-200.png +0 -0
  52. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/SplashScreen.scale-200.png +0 -0
  53. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/Square150x150Logo.scale-200.png +0 -0
  54. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/Square44x44Logo.scale-200.png +0 -0
  55. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/Square44x44Logo.targetsize-24_altform-unplated.png +0 -0
  56. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/StoreLogo.png +0 -0
  57. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Assets/Wide310x150Logo.scale-200.png +0 -0
  58. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/AutolinkedNativeModules.g.cpp +0 -18
  59. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/AutolinkedNativeModules.g.h +0 -10
  60. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/AutolinkedNativeModules.g.targets +0 -11
  61. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/MainPage.cpp +0 -24
  62. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/MainPage.h +0 -21
  63. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/MainPage.idl +0 -8
  64. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/MainPage.xaml +0 -16
  65. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/Package.appxmanifest +0 -50
  66. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/PropertySheet.props +0 -16
  67. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/ReactNativeBlobUtilWin.vcxproj +0 -196
  68. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/ReactNativeBlobUtilWin.vcxproj.filters +0 -65
  69. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/ReactNativeBlobUtilWin_TemporaryKey.pfx +0 -0
  70. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/ReactPackageProvider.cpp +0 -18
  71. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/ReactPackageProvider.h +0 -15
  72. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/packages.config +0 -5
  73. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/pch.cpp +0 -1
  74. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin/pch.h +0 -26
  75. package/examples/ReactNativeBlobUtil/windows/ReactNativeBlobUtilWin.sln +0 -207
@@ -8,7 +8,6 @@ import android.net.Uri;
8
8
  import android.os.Build;
9
9
  import android.util.SparseArray;
10
10
 
11
- import androidx.annotation.NonNull;
12
11
  import androidx.annotation.RequiresApi;
13
12
  import androidx.core.content.FileProvider;
14
13
 
@@ -18,8 +17,6 @@ import com.facebook.react.bridge.Callback;
18
17
  import com.facebook.react.bridge.LifecycleEventListener;
19
18
  import com.facebook.react.bridge.Promise;
20
19
  import com.facebook.react.bridge.ReactApplicationContext;
21
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
22
- import com.facebook.react.bridge.ReactMethod;
23
20
  import com.facebook.react.bridge.ReadableArray;
24
21
  import com.facebook.react.bridge.ReadableMap;
25
22
  import com.facebook.react.bridge.WritableMap;
@@ -28,8 +25,6 @@ import com.facebook.react.modules.network.ForwardingCookieHandler;
28
25
  import com.facebook.react.modules.network.OkHttpClientProvider;
29
26
 
30
27
  import java.io.File;
31
- import java.util.HashMap;
32
- import java.util.Map;
33
28
  import java.util.concurrent.LinkedBlockingQueue;
34
29
  import java.util.concurrent.ThreadPoolExecutor;
35
30
  import java.util.concurrent.TimeUnit;
@@ -42,7 +37,9 @@ import okhttp3.OkHttpClient;
42
37
  import static android.app.Activity.RESULT_OK;
43
38
  import static com.ReactNativeBlobUtil.ReactNativeBlobUtilConst.GET_CONTENT_INTENT;
44
39
 
45
- public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
40
+ class ReactNativeBlobUtilImpl {
41
+
42
+ public static final String NAME = "ReactNativeBlobUtil";
46
43
 
47
44
  private final OkHttpClient mClient;
48
45
 
@@ -54,10 +51,7 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
54
51
  private static boolean ActionViewVisible = false;
55
52
  private static final SparseArray<Promise> promiseTable = new SparseArray<>();
56
53
 
57
- public ReactNativeBlobUtil(ReactApplicationContext reactContext) {
58
-
59
- super(reactContext);
60
-
54
+ public ReactNativeBlobUtilImpl(ReactApplicationContext reactContext) {
61
55
  mClient = OkHttpClientProvider.getOkHttpClient();
62
56
  ForwardingCookieHandler mCookieHandler = new ForwardingCookieHandler(reactContext);
63
57
  CookieJarContainer mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
@@ -81,22 +75,6 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
81
75
  });
82
76
  }
83
77
 
84
- @NonNull
85
- @Override
86
- public String getName() {
87
- return "ReactNativeBlobUtil";
88
- }
89
-
90
- @Override
91
- public Map<String, Object> getConstants() {
92
- Map<String, Object> res = new HashMap<>();
93
- res.putAll(ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext()));
94
- res.putAll(ReactNativeBlobUtilFS.getLegacySystemfolders(this.getReactApplicationContext()));
95
- System.out.println(res);
96
- return res;
97
- }
98
-
99
- @ReactMethod
100
78
  public void createFile(final String path, final String content, final String encode, final Promise promise) {
101
79
  threadPool.execute(new Runnable() {
102
80
  @Override
@@ -106,7 +84,6 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
106
84
  });
107
85
  }
108
86
 
109
- @ReactMethod
110
87
  public void createFileASCII(final String path, final ReadableArray dataArray, final Promise promise) {
111
88
  threadPool.execute(new Runnable() {
112
89
  @Override
@@ -116,13 +93,12 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
116
93
  });
117
94
  }
118
95
 
119
- @ReactMethod
120
96
  public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
121
97
  try {
122
98
  Uri uriForFile = null;
123
99
  if (!ReactNativeBlobUtilUtils.isContentUri(path)) {
124
- uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(),
125
- this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
100
+ uriForFile = FileProvider.getUriForFile(RCTContext,
101
+ RCTContext.getPackageName() + ".provider", new File(path));
126
102
  } else {
127
103
  uriForFile = Uri.parse(path);
128
104
  }
@@ -144,7 +120,7 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
144
120
  }
145
121
 
146
122
  try {
147
- this.getReactApplicationContext().startActivity(intent);
123
+ RCTContext.startActivity(intent);
148
124
  promise.resolve(true);
149
125
  } catch (ActivityNotFoundException ex) {
150
126
  promise.reject("ENOAPP", "No app installed for " + mime);
@@ -177,27 +153,22 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
177
153
  }
178
154
  }
179
155
 
180
- @ReactMethod
181
156
  public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) {
182
157
  ReactNativeBlobUtilStream.writeArrayChunk(streamId, dataArray, callback);
183
158
  }
184
159
 
185
- @ReactMethod
186
160
  public void unlink(String path, Callback callback) {
187
161
  ReactNativeBlobUtilFS.unlink(path, callback);
188
162
  }
189
163
 
190
- @ReactMethod
191
164
  public void mkdir(String path, Promise promise) {
192
165
  ReactNativeBlobUtilFS.mkdir(path, promise);
193
166
  }
194
167
 
195
- @ReactMethod
196
168
  public void exists(String path, Callback callback) {
197
169
  ReactNativeBlobUtilFS.exists(path, callback);
198
170
  }
199
171
 
200
- @ReactMethod
201
172
  public void cp(final String path, final String dest, final Callback callback) {
202
173
  threadPool.execute(new Runnable() {
203
174
  @Override
@@ -207,37 +178,30 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
207
178
  });
208
179
  }
209
180
 
210
- @ReactMethod
211
181
  public void mv(String path, String dest, Callback callback) {
212
182
  ReactNativeBlobUtilFS.mv(path, dest, callback);
213
183
  }
214
184
 
215
- @ReactMethod
216
185
  public void ls(String path, Promise promise) {
217
186
  ReactNativeBlobUtilFS.ls(path, promise);
218
187
  }
219
188
 
220
- @ReactMethod
221
189
  public void writeStream(String path, String encode, boolean append, Callback callback) {
222
- new ReactNativeBlobUtilStream(this.getReactApplicationContext()).writeStream(path, encode, append, callback);
190
+ new ReactNativeBlobUtilStream(RCTContext).writeStream(path, encode, append, callback);
223
191
  }
224
192
 
225
- @ReactMethod
226
193
  public void writeChunk(String streamId, String data, Callback callback) {
227
194
  ReactNativeBlobUtilStream.writeChunk(streamId, data, callback);
228
195
  }
229
196
 
230
- @ReactMethod
231
197
  public void closeStream(String streamId, Callback callback) {
232
198
  ReactNativeBlobUtilStream.closeStream(streamId, callback);
233
199
  }
234
200
 
235
- @ReactMethod
236
201
  public void removeSession(ReadableArray paths, Callback callback) {
237
202
  ReactNativeBlobUtilFS.removeSession(paths, callback);
238
203
  }
239
204
 
240
- @ReactMethod
241
205
  public void readFile(final String path, final String encoding, final boolean transformFile, final Promise promise) {
242
206
  threadPool.execute(new Runnable() {
243
207
  @Override
@@ -247,7 +211,6 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
247
211
  });
248
212
  }
249
213
 
250
- @ReactMethod
251
214
  public void writeFileArray(final String path, final ReadableArray data, final boolean append, final Promise promise) {
252
215
  threadPool.execute(new Runnable() {
253
216
  @Override
@@ -257,7 +220,6 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
257
220
  });
258
221
  }
259
222
 
260
- @ReactMethod
261
223
  public void writeFile(final String path, final String encoding, final String data, final boolean transformFile, final boolean append, final Promise promise) {
262
224
  threadPool.execute(new Runnable() {
263
225
  @Override
@@ -267,19 +229,16 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
267
229
  });
268
230
  }
269
231
 
270
- @ReactMethod
271
232
  public void lstat(String path, Callback callback) {
272
233
  ReactNativeBlobUtilFS.lstat(path, callback);
273
234
  }
274
235
 
275
- @ReactMethod
276
236
  public void stat(String path, Callback callback) {
277
237
  ReactNativeBlobUtilFS.stat(path, callback);
278
238
  }
279
239
 
280
- @ReactMethod
281
240
  public void scanFile(final ReadableArray pairs, final Callback callback) {
282
- final ReactApplicationContext ctx = this.getReactApplicationContext();
241
+ final ReactApplicationContext ctx = RCTContext;
283
242
  threadPool.execute(new Runnable() {
284
243
  @Override
285
244
  public void run() {
@@ -301,7 +260,6 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
301
260
  });
302
261
  }
303
262
 
304
- @ReactMethod
305
263
  public void hash(final String path, final String algorithm, final Promise promise) {
306
264
  threadPool.execute(new Runnable() {
307
265
  @Override
@@ -316,19 +274,17 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
316
274
  * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
317
275
  * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
318
276
  */
319
- @ReactMethod
320
277
  public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
321
- final ReactApplicationContext ctx = this.getReactApplicationContext();
278
+ final ReactApplicationContext ctx = RCTContext;
322
279
  fsThreadPool.execute(new Runnable() {
323
280
  @Override
324
281
  public void run() {
325
282
  ReactNativeBlobUtilStream fs = new ReactNativeBlobUtilStream(ctx);
326
- fs.readStream(path, encoding, bufferSize, tick, streamId);
283
+ fs.readStream(path, encoding, bufferSize, tick, streamId, RCTContext);
327
284
  }
328
285
  });
329
286
  }
330
287
 
331
- @ReactMethod
332
288
  public void cancelRequest(String taskId, Callback callback) {
333
289
  try {
334
290
  ReactNativeBlobUtilReq.cancelTask(taskId);
@@ -338,45 +294,38 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
338
294
  }
339
295
  }
340
296
 
341
- @ReactMethod
342
297
  public void slice(String src, String dest, int start, int end, Promise promise) {
343
298
  ReactNativeBlobUtilFS.slice(src, dest, start, end, "", promise);
344
299
  }
345
300
 
346
- @ReactMethod
347
301
  public void enableProgressReport(String taskId, int interval, int count) {
348
302
  ReactNativeBlobUtilProgressConfig config = new ReactNativeBlobUtilProgressConfig(true, interval, count, ReactNativeBlobUtilProgressConfig.ReportType.Download);
349
303
  ReactNativeBlobUtilReq.progressReport.put(taskId, config);
350
304
  }
351
305
 
352
- @ReactMethod
353
306
  public void df(final Callback callback) {
354
307
  fsThreadPool.execute(new Runnable() {
355
308
  @Override
356
309
  public void run() {
357
- ReactNativeBlobUtilFS.df(callback, getReactApplicationContext());
310
+ ReactNativeBlobUtilFS.df(callback, RCTContext);
358
311
  }
359
312
  });
360
313
  }
361
314
 
362
315
 
363
- @ReactMethod
364
316
  public void enableUploadProgressReport(String taskId, int interval, int count) {
365
317
  ReactNativeBlobUtilProgressConfig config = new ReactNativeBlobUtilProgressConfig(true, interval, count, ReactNativeBlobUtilProgressConfig.ReportType.Upload);
366
318
  ReactNativeBlobUtilReq.uploadProgressReport.put(taskId, config);
367
319
  }
368
320
 
369
- @ReactMethod
370
321
  public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
371
322
  new ReactNativeBlobUtilReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
372
323
  }
373
324
 
374
- @ReactMethod
375
325
  public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
376
326
  new ReactNativeBlobUtilReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
377
327
  }
378
328
 
379
- @ReactMethod
380
329
  public void getContentIntent(String mime, Promise promise) {
381
330
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
382
331
  if (mime != null)
@@ -384,11 +333,10 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
384
333
  else
385
334
  i.setType("*/*");
386
335
  promiseTable.put(GET_CONTENT_INTENT, promise);
387
- this.getReactApplicationContext().startActivityForResult(i, GET_CONTENT_INTENT, null);
336
+ RCTContext.startActivityForResult(i, GET_CONTENT_INTENT, null);
388
337
 
389
338
  }
390
339
 
391
- @ReactMethod
392
340
  public void addCompleteDownload(ReadableMap config, Promise promise) {
393
341
  DownloadManager dm = (DownloadManager) RCTContext.getSystemService(RCTContext.DOWNLOAD_SERVICE);
394
342
  if (config == null || !config.hasKey("path")) {
@@ -418,17 +366,14 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
418
366
 
419
367
  }
420
368
 
421
- @ReactMethod
422
369
  public void getSDCardDir(Promise promise) {
423
- ReactNativeBlobUtilFS.getSDCardDir(this.getReactApplicationContext(), promise);
370
+ ReactNativeBlobUtilFS.getSDCardDir(RCTContext, promise);
424
371
  }
425
372
 
426
- @ReactMethod
427
373
  public void getSDCardApplicationDir(Promise promise) {
428
- ReactNativeBlobUtilFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
374
+ ReactNativeBlobUtilFS.getSDCardApplicationDir(RCTContext, promise);
429
375
  }
430
376
 
431
- @ReactMethod
432
377
  public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
433
378
  if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
434
379
  promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
@@ -437,30 +382,26 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
437
382
  if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
438
383
 
439
384
  FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
440
- Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), this.getReactApplicationContext());
385
+ Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), RCTContext);
441
386
  if (res != null) promise.resolve(res.toString());
442
387
  else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
443
388
  }
444
389
 
445
- @ReactMethod
446
390
  public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
447
- boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise);
391
+ boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise, RCTContext);
448
392
  if (res) promise.resolve("Success");
449
393
  }
450
394
 
451
395
  @RequiresApi(api = Build.VERSION_CODES.Q)
452
- @ReactMethod
453
396
  public void copyToInternal(String contentUri, String destpath, Promise promise) {
454
397
  ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise);
455
398
  }
456
399
 
457
400
  @RequiresApi(api = Build.VERSION_CODES.Q)
458
- @ReactMethod
459
401
  public void getBlob(String contentUri, String encoding, Promise promise) {
460
402
  ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
461
403
  }
462
404
 
463
- @ReactMethod
464
405
  public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
465
406
  if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
466
407
  promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
@@ -476,14 +417,14 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
476
417
  }
477
418
 
478
419
  FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
479
- Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), this.getReactApplicationContext());
420
+ Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt), RCTContext);
480
421
 
481
422
  if (fileuri == null) {
482
423
  promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
483
424
  return;
484
425
  }
485
426
 
486
- boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise);
427
+ boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise, RCTContext);
487
428
  if (res) promise.resolve(fileuri.toString());
488
429
  }
489
430
 
@@ -79,7 +79,7 @@ public class ReactNativeBlobUtilMediaCollection {
79
79
 
80
80
  public static Uri createNewMediaFile(FileDescription file, MediaType mt, ReactApplicationContext ctx) {
81
81
  // Add a specific media item.
82
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
82
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
83
83
  ContentResolver resolver = appCtx.getContentResolver();
84
84
 
85
85
  ContentValues fileDetails = new ContentValues();
@@ -124,10 +124,10 @@ public class ReactNativeBlobUtilMediaCollection {
124
124
  return null;
125
125
  }
126
126
 
127
- public static boolean writeToMediaFile(Uri fileUri, String data, boolean transformFile, Promise promise) {
127
+ public static boolean writeToMediaFile(Uri fileUri, String data, boolean transformFile, Promise promise, ReactApplicationContext ctx) {
128
128
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
129
129
  try {
130
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
130
+ Context appCtx = ctx.getApplicationContext();
131
131
  ContentResolver resolver = appCtx.getContentResolver();
132
132
 
133
133
  // set pending doesn't work right now. We would have to requery for the item
@@ -220,7 +220,7 @@ public class ReactNativeBlobUtilMediaCollection {
220
220
  }
221
221
 
222
222
  public static void copyToInternal(Uri contenturi, String destpath, Promise promise) {
223
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
223
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
224
224
  ContentResolver resolver = appCtx.getContentResolver();
225
225
 
226
226
  InputStream in = null;
@@ -277,7 +277,7 @@ public class ReactNativeBlobUtilMediaCollection {
277
277
  }
278
278
 
279
279
  public static void getBlob(Uri contentUri, String encoding, Promise promise) {
280
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
280
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
281
281
  ContentResolver resolver = appCtx.getContentResolver();
282
282
  try {
283
283
  InputStream in = resolver.openInputStream(contentUri);
@@ -1,36 +1,46 @@
1
1
  package com.ReactNativeBlobUtil;
2
2
 
3
- import androidx.annotation.NonNull;
3
+ import androidx.annotation.Nullable;
4
4
 
5
- import com.facebook.react.ReactPackage;
6
- import com.facebook.react.bridge.JavaScriptModule;
5
+ import com.facebook.react.TurboReactPackage;
7
6
  import com.facebook.react.bridge.NativeModule;
8
7
  import com.facebook.react.bridge.ReactApplicationContext;
9
- import com.facebook.react.uimanager.ViewManager;
8
+ import com.facebook.react.module.model.ReactModuleInfo;
9
+ import com.facebook.react.module.model.ReactModuleInfoProvider;
10
10
 
11
- import java.util.ArrayList;
12
- import java.util.Collections;
13
- import java.util.List;
11
+ import java.util.HashMap;
12
+ import java.util.Map;
14
13
 
14
+ public class ReactNativeBlobUtilPackage extends TurboReactPackage {
15
15
 
16
- public class ReactNativeBlobUtilPackage implements ReactPackage {
17
-
18
- @NonNull
16
+ @Nullable
19
17
  @Override
20
- public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
21
- List<NativeModule> modules = new ArrayList<>();
22
- modules.add(new ReactNativeBlobUtil(reactContext));
23
- return modules;
24
- }
25
-
26
- public List<Class<? extends JavaScriptModule>> createJSModules() {
27
- return Collections.emptyList();
18
+ public NativeModule getModule(String name, ReactApplicationContext reactContext) {
19
+ if (name.equals(ReactNativeBlobUtilImpl.NAME)) {
20
+ return new ReactNativeBlobUtil(reactContext);
21
+ } else {
22
+ return null;
23
+ }
28
24
  }
29
25
 
30
- @NonNull
31
26
  @Override
32
- public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
33
- return Collections.emptyList();
27
+ public ReactModuleInfoProvider getReactModuleInfoProvider() {
28
+ return () -> {
29
+ final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
30
+ boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
31
+ moduleInfos.put(
32
+ ReactNativeBlobUtilImpl.NAME,
33
+ new ReactModuleInfo(
34
+ ReactNativeBlobUtilImpl.NAME,
35
+ ReactNativeBlobUtilImpl.NAME,
36
+ false, // canOverrideExistingModule
37
+ false, // needsEagerInit
38
+ true, // hasConstants
39
+ false, // isCxxModule
40
+ isTurboModule // isTurboModule
41
+ ));
42
+ return moduleInfos;
43
+ };
34
44
  }
35
45
 
36
46
  }
@@ -95,8 +95,8 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
95
95
 
96
96
  private boolean shouldTransformFile() {
97
97
  return this.options.transformFile &&
98
- // Can only process if it's written to a file
99
- (this.options.fileCache || this.options.path != null);
98
+ // Can only process if it's written to a file
99
+ (this.options.fileCache || this.options.path != null);
100
100
  }
101
101
 
102
102
  public static HashMap<String, Call> taskTable = new HashMap<>();
@@ -114,7 +114,6 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
114
114
  ReadableArray rawRequestBodyArray;
115
115
  ReadableMap headers;
116
116
  Callback callback;
117
- Boolean callbackfired = false;
118
117
  long contentLength;
119
118
  long downloadManagerId;
120
119
  ReactNativeBlobUtilBody requestBody;
@@ -125,6 +124,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
125
124
  boolean timeout = false;
126
125
  ArrayList<String> redirects = new ArrayList<>();
127
126
  OkHttpClient client;
127
+ boolean callbackfired;
128
128
 
129
129
  public ReactNativeBlobUtilReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, OkHttpClient client, final Callback callback) {
130
130
  this.method = method.toUpperCase(Locale.ROOT);
@@ -136,10 +136,11 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
136
136
  this.rawRequestBody = body;
137
137
  this.rawRequestBodyArray = arrayBody;
138
138
  this.client = client;
139
+ this.callbackfired = false;
139
140
 
140
141
  // If transformFile is specified, we first want to get the response back in memory so we can
141
142
  // encrypt it wholesale and at that point, write it into the file storage.
142
- if ((this.options.fileCache || this.options.path != null) && !this.shouldTransformFile())
143
+ if((this.options.fileCache || this.options.path != null) && !this.shouldTransformFile())
143
144
  responseType = ResponseType.FileStorage;
144
145
  else
145
146
  responseType = ResponseType.KeepInMemory;
@@ -162,7 +163,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
162
163
 
163
164
  if (androidDownloadManagerTaskTable.containsKey(taskId)) {
164
165
  long downloadManagerIdForTaskId = androidDownloadManagerTaskTable.get(taskId).longValue();
165
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
166
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
166
167
  DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
167
168
  dm.remove(downloadManagerIdForTaskId);
168
169
  }
@@ -187,7 +188,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
187
188
  long id = data.getLong("downloadManagerId");
188
189
  if (id == downloadManagerId) {
189
190
 
190
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
191
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
191
192
 
192
193
  DownloadManager downloadManager = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
193
194
 
@@ -213,7 +214,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
213
214
  args.putString("written", String.valueOf(written));
214
215
  args.putString("total", String.valueOf(total));
215
216
  args.putString("chunk", "");
216
- ReactNativeBlobUtil.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
217
+ ReactNativeBlobUtilImpl.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
217
218
  .emit(ReactNativeBlobUtilConst.EVENT_PROGRESS, args);
218
219
 
219
220
  }
@@ -274,7 +275,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
274
275
  } catch (MalformedURLException e) {
275
276
  e.printStackTrace();
276
277
  }
277
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
278
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
278
279
  DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
279
280
  downloadManagerId = dm.enqueue(req);
280
281
  androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));
@@ -336,7 +337,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
336
337
  boolean found = false;
337
338
 
338
339
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
339
- ConnectivityManager connectivityManager = (ConnectivityManager) ReactNativeBlobUtil.RCTContext.getSystemService(ReactNativeBlobUtil.RCTContext.CONNECTIVITY_SERVICE);
340
+ ConnectivityManager connectivityManager = (ConnectivityManager) ReactNativeBlobUtilImpl.RCTContext.getSystemService(ReactNativeBlobUtilImpl.RCTContext.CONNECTIVITY_SERVICE);
340
341
  Network[] networks = connectivityManager.getAllNetworks();
341
342
 
342
343
  for (Network network : networks) {
@@ -487,14 +488,14 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
487
488
  switch (responseType) {
488
489
  case KeepInMemory:
489
490
  extended = new ReactNativeBlobUtilDefaultResp(
490
- ReactNativeBlobUtil.RCTContext,
491
+ ReactNativeBlobUtilImpl.RCTContext,
491
492
  taskId,
492
493
  originalResponse.body(),
493
494
  options.increment);
494
495
  break;
495
496
  case FileStorage:
496
497
  extended = new ReactNativeBlobUtilFileResp(
497
- ReactNativeBlobUtil.RCTContext,
498
+ ReactNativeBlobUtilImpl.RCTContext,
498
499
  taskId,
499
500
  originalResponse.body(),
500
501
  destPath,
@@ -502,7 +503,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
502
503
  break;
503
504
  default:
504
505
  extended = new ReactNativeBlobUtilDefaultResp(
505
- ReactNativeBlobUtil.RCTContext,
506
+ ReactNativeBlobUtilImpl.RCTContext,
506
507
  taskId,
507
508
  originalResponse.body(),
508
509
  options.increment);
@@ -581,7 +582,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
581
582
  scannable = notifyConfig.getBoolean("mediaScannable");
582
583
  if (notifyConfig.hasKey("notification"))
583
584
  notification = notifyConfig.getBoolean("notification");
584
- DownloadManager dm = (DownloadManager) ReactNativeBlobUtil.RCTContext.getSystemService(ReactNativeBlobUtil.RCTContext.DOWNLOAD_SERVICE);
585
+ DownloadManager dm = (DownloadManager) ReactNativeBlobUtilImpl.RCTContext.getSystemService(ReactNativeBlobUtilImpl.RCTContext.DOWNLOAD_SERVICE);
585
586
  dm.addCompletedDownload(title, desc, scannable, mime, destPath, contentLength, notification);
586
587
  }
587
588
 
@@ -843,7 +844,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
843
844
  }
844
845
 
845
846
  private void emitStateEvent(WritableMap args) {
846
- ReactNativeBlobUtil.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
847
+ ReactNativeBlobUtilImpl.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
847
848
  .emit(ReactNativeBlobUtilConst.EVENT_HTTP_STATE, args);
848
849
  }
849
850
 
@@ -851,7 +852,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
851
852
  public void onReceive(Context context, Intent intent) {
852
853
  String action = intent.getAction();
853
854
  if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
854
- Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext();
855
+ Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
855
856
  long id = intent.getExtras().getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
856
857
  if (id == this.downloadManagerId) {
857
858
  releaseTaskResource(); // remove task ID from task map
@@ -863,7 +864,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
863
864
  Cursor c = dm.query(query);
864
865
  // #236 unhandled null check for DownloadManager.query() return value
865
866
  if (c == null) {
866
- this.invoke_callback("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
867
+ this.callback.invoke("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
867
868
  return;
868
869
  }
869
870
 
@@ -874,7 +875,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
874
875
  // #297 handle failed request
875
876
  int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
876
877
  if (statusCode == DownloadManager.STATUS_FAILED) {
877
- this.invoke_callback("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
878
+ this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
878
879
  return;
879
880
  }
880
881
  String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
@@ -903,17 +904,17 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
903
904
  if (!exists)
904
905
  throw new Exception("Download manager download failed, the file does not downloaded to destination.");
905
906
  else
906
- this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
907
+ this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
907
908
 
908
909
  } catch (Exception ex) {
909
910
  ex.printStackTrace();
910
- this.invoke_callback(ex.getLocalizedMessage(), null);
911
+ this.callback.invoke(ex.getLocalizedMessage(), null);
911
912
  }
912
913
  } else {
913
914
  if (filePath == null)
914
- this.invoke_callback("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
915
+ this.callback.invoke("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
915
916
  else
916
- this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
917
+ this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
917
918
  }
918
919
 
919
920
  }