react-native-blob-util 0.16.1 → 0.16.3

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.
@@ -28,6 +28,7 @@ import com.facebook.react.modules.network.ForwardingCookieHandler;
28
28
  import com.facebook.react.modules.network.OkHttpClientProvider;
29
29
 
30
30
  import java.io.File;
31
+ import java.util.HashMap;
31
32
  import java.util.Map;
32
33
  import java.util.concurrent.LinkedBlockingQueue;
33
34
  import java.util.concurrent.ThreadPoolExecutor;
@@ -88,7 +89,11 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
88
89
 
89
90
  @Override
90
91
  public Map<String, Object> getConstants() {
91
- return ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext());
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;
92
97
  }
93
98
 
94
99
  @ReactMethod
@@ -440,7 +445,7 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
440
445
  @ReactMethod
441
446
  public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
442
447
  boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise);
443
- if(res) promise.resolve("Success");
448
+ if (res) promise.resolve("Success");
444
449
  }
445
450
 
446
451
  @RequiresApi(api = Build.VERSION_CODES.Q)
@@ -479,7 +484,7 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
479
484
  }
480
485
 
481
486
  boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise);
482
- if(res) promise.resolve(fileuri.toString());
487
+ if (res) promise.resolve(fileuri.toString());
483
488
  }
484
489
 
485
490
  }
@@ -9,6 +9,8 @@ import android.os.Environment;
9
9
  import android.os.StatFs;
10
10
  import android.util.Base64;
11
11
 
12
+ import androidx.annotation.NonNull;
13
+
12
14
  import com.facebook.react.bridge.Arguments;
13
15
  import com.facebook.react.bridge.Callback;
14
16
  import com.facebook.react.bridge.Promise;
@@ -359,6 +361,34 @@ class ReactNativeBlobUtilFS {
359
361
  return res;
360
362
  }
361
363
 
364
+ /**
365
+ * Static method that returns legacy system folders to JS context (usage of deprecated functions since these retunr different folders)
366
+ *
367
+ * @param ctx React Native application context
368
+ */
369
+
370
+ @NonNull
371
+ @SuppressWarnings("deprecation")
372
+ static Map<String, Object> getLegacySystemfolders(ReactApplicationContext ctx) {
373
+ Map<String, Object> res = new HashMap<>();
374
+
375
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return ReactNativeBlobUtilFS.getSystemfolders(ctx);
376
+
377
+ res.put("LegacyDCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
378
+ res.put("LegacyPictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
379
+ res.put("LegacyMusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
380
+ res.put("LegacyDownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
381
+ res.put("LegacyMovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
382
+ res.put("LegacyRingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
383
+
384
+ String state = Environment.getExternalStorageState();
385
+ if (state.equals(Environment.MEDIA_MOUNTED)) {
386
+ res.put("LegacySDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
387
+ }
388
+
389
+ return res;
390
+ }
391
+
362
392
  static String getExternalFilesDirPath(ReactApplicationContext ctx, String type) {
363
393
  File dir = ctx.getExternalFilesDir(type);
364
394
  if (dir != null) return dir.getAbsolutePath();
@@ -809,7 +839,7 @@ class ReactNativeBlobUtilFS {
809
839
  promise.reject("EINVAL", "Invalid algorithm '" + algorithm + "', must be one of md5, sha1, sha224, sha256, sha384, sha512");
810
840
  return;
811
841
  }
812
-
842
+
813
843
  path = ReactNativeBlobUtilUtils.normalizePath(path);
814
844
 
815
845
  File file = new File(path);
@@ -69,11 +69,11 @@ public class ReactNativeBlobUtilMediaCollection {
69
69
  if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS;
70
70
  return Environment.DIRECTORY_DOWNLOADS;
71
71
  } else {
72
- if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MUSIC);
73
- if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MOVIES);
74
- if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_PICTURES);
75
- if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS);
76
- return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS);
72
+ if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMusicDir").toString();
73
+ if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMovieDir").toString();
74
+ if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyPictureDir").toString();
75
+ if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
76
+ return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
77
77
  }
78
78
  }
79
79
 
@@ -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,6 +114,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
114
114
  ReadableArray rawRequestBodyArray;
115
115
  ReadableMap headers;
116
116
  Callback callback;
117
+ Boolean callbackfired = false;
117
118
  long contentLength;
118
119
  long downloadManagerId;
119
120
  ReactNativeBlobUtilBody requestBody;
@@ -138,7 +139,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
138
139
 
139
140
  // If transformFile is specified, we first want to get the response back in memory so we can
140
141
  // encrypt it wholesale and at that point, write it into the file storage.
141
- if((this.options.fileCache || this.options.path != null) && !this.shouldTransformFile())
142
+ if ((this.options.fileCache || this.options.path != null) && !this.shouldTransformFile())
142
143
  responseType = ResponseType.FileStorage;
143
144
  else
144
145
  responseType = ResponseType.KeepInMemory;
@@ -167,6 +168,11 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
167
168
  }
168
169
  }
169
170
 
171
+ private void invoke_callback(Object... args) {
172
+ if (this.callbackfired) return;
173
+ this.callback.invoke(args);
174
+ }
175
+
170
176
  private final int QUERY = 1314;
171
177
  private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
172
178
  private Future<?> future;
@@ -301,7 +307,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
301
307
  File file = new File(ReactNativeBlobUtilFS.getTmpPath(cacheKey) + ext);
302
308
 
303
309
  if (file.exists()) {
304
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
310
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
305
311
  return;
306
312
  }
307
313
  }
@@ -355,7 +361,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
355
361
  }
356
362
 
357
363
  if (!found) {
358
- callback.invoke("No available WiFi connections.", null, null);
364
+ invoke_callback("No available WiFi connections.", null, null);
359
365
  releaseTaskResource();
360
366
  return;
361
367
  }
@@ -551,9 +557,9 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
551
557
  // check if this error caused by socket timeout
552
558
  if (e.getClass().equals(SocketTimeoutException.class)) {
553
559
  respInfo.putBoolean("timeout", true);
554
- callback.invoke("The request timed out.", null, null);
560
+ invoke_callback("The request timed out.", null, null);
555
561
  } else
556
- callback.invoke(e.getLocalizedMessage(), null, null);
562
+ invoke_callback(e.getLocalizedMessage(), null, null);
557
563
  releaseTaskResource();
558
564
  }
559
565
 
@@ -586,7 +592,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
586
592
  } catch (Exception error) {
587
593
  error.printStackTrace();
588
594
  releaseTaskResource();
589
- callback.invoke("ReactNativeBlobUtil request error: " + error.getMessage() + error.getCause());
595
+ invoke_callback("ReactNativeBlobUtil request error: " + error.getMessage() + error.getCause());
590
596
  }
591
597
  }
592
598
 
@@ -631,7 +637,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
631
637
  ins.close();
632
638
  os.flush();
633
639
  os.close();
634
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
640
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
635
641
  }
636
642
  // response data directly pass to JS context as string.
637
643
  else {
@@ -649,15 +655,15 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
649
655
  }
650
656
  try (FileOutputStream fos = new FileOutputStream(file)) {
651
657
  fos.write(ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(b));
652
- } catch(Exception e) {
653
- callback.invoke("Error from file transformer:" + e.getLocalizedMessage(), null);
658
+ } catch (Exception e) {
659
+ invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null);
654
660
  return;
655
661
  }
656
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
662
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
657
663
  return;
658
664
  }
659
665
  if (responseFormat == ResponseFormat.BASE64) {
660
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
666
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
661
667
  return;
662
668
  }
663
669
  try {
@@ -667,21 +673,21 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
667
673
  decoder.decode(ByteBuffer.wrap(b));
668
674
  // If the data contains invalid characters the following lines will be skipped.
669
675
  String utf8 = new String(b, charSet);
670
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
676
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
671
677
  }
672
678
  // This usually means the data contains invalid unicode characters but still valid data,
673
679
  // it's binary data, so send it as a normal string
674
680
  catch (CharacterCodingException ignored) {
675
681
  if (responseFormat == ResponseFormat.UTF8) {
676
682
  String utf8 = new String(b);
677
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
683
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
678
684
  } else {
679
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
685
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
680
686
  }
681
687
  }
682
688
  }
683
689
  } catch (IOException e) {
684
- callback.invoke("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
690
+ invoke_callback("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
685
691
  }
686
692
  break;
687
693
  case FileStorage:
@@ -713,26 +719,26 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
713
719
  } catch (IOException exception) {
714
720
  exception.printStackTrace();
715
721
  }
716
- callback.invoke("Unexpected FileStorage response file: " + responseBodyString, null);
722
+ invoke_callback("Unexpected FileStorage response file: " + responseBodyString, null);
717
723
  } else {
718
- callback.invoke("Unexpected FileStorage response with no file.", null);
724
+ invoke_callback("Unexpected FileStorage response with no file.", null);
719
725
  }
720
726
  return;
721
727
  }
722
728
 
723
729
  if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) {
724
- callback.invoke("Download interrupted.", null);
730
+ invoke_callback("Download interrupted.", null);
725
731
  } else {
726
732
  this.destPath = this.destPath.replace("?append=true", "");
727
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
733
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
728
734
  }
729
735
 
730
736
  break;
731
737
  default:
732
738
  try {
733
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
739
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
734
740
  } catch (IOException e) {
735
- callback.invoke("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
741
+ invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
736
742
  }
737
743
  break;
738
744
  }
@@ -856,7 +862,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
856
862
  Cursor c = dm.query(query);
857
863
  // #236 unhandled null check for DownloadManager.query() return value
858
864
  if (c == null) {
859
- this.callback.invoke("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
865
+ this.invoke_callback("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
860
866
  return;
861
867
  }
862
868
 
@@ -867,7 +873,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
867
873
  // #297 handle failed request
868
874
  int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
869
875
  if (statusCode == DownloadManager.STATUS_FAILED) {
870
- this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
876
+ this.invoke_callback("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
871
877
  return;
872
878
  }
873
879
  String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
@@ -896,17 +902,17 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
896
902
  if (!exists)
897
903
  throw new Exception("Download manager download failed, the file does not downloaded to destination.");
898
904
  else
899
- this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
905
+ this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
900
906
 
901
907
  } catch (Exception ex) {
902
908
  ex.printStackTrace();
903
- this.callback.invoke(ex.getLocalizedMessage(), null);
909
+ this.invoke_callback(ex.getLocalizedMessage(), null);
904
910
  }
905
911
  } else {
906
912
  if (filePath == null)
907
- this.callback.invoke("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
913
+ this.invoke_callback("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
908
914
  else
909
- this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
915
+ this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
910
916
  }
911
917
 
912
918
  }
package/fs.js CHANGED
@@ -24,7 +24,15 @@ const dirs = {
24
24
  SDCardApplicationDir: ReactNativeBlobUtil.SDCardApplicationDir, // Deprecated
25
25
  MainBundleDir: ReactNativeBlobUtil.MainBundleDir,
26
26
  LibraryDir: ReactNativeBlobUtil.LibraryDir,
27
- ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir
27
+ ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir,
28
+
29
+ LegacyPictureDir: ReactNativeBlobUtil.LegacyPictureDir,
30
+ LegacyMusicDir: ReactNativeBlobUtil.LegacyMusicDir,
31
+ LegacyMovieDir: ReactNativeBlobUtil.LegacyMovieDir,
32
+ LegacyDownloadDir: ReactNativeBlobUtil.LegacyDownloadDir,
33
+ LegacyDCIMDir: ReactNativeBlobUtil.LegacyDCIMDir,
34
+ LegacySDCardDir: ReactNativeBlobUtil.LegacySDCardDir, // Depracated
35
+
28
36
  };
29
37
 
30
38
  function addCode(code: string, error: Error): Error {
package/index.d.ts CHANGED
@@ -481,6 +481,13 @@ export interface Dirs {
481
481
  DCIMDir: string;
482
482
  SDCardDir: string;
483
483
  MainBundleDir: string;
484
+
485
+ LegacyPictureDir: string;
486
+ LegacyMusicDir: string;
487
+ LegacyMovieDir: string;
488
+ LegacyDownloadDir: string;
489
+ LegacyDCIMDir: string;
490
+ LegacySDCardDir: string; // Depracated
484
491
  }
485
492
 
486
493
  export interface ReactNativeBlobUtilWriteStream {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-blob-util",
3
- "version": "0.16.1",
3
+ "version": "0.16.3",
4
4
  "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
5
5
  "main": "index.js",
6
6
  "scripts": {