react-native-blob-util 0.16.2 → 0.16.4

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,12 @@ 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
+ this.callbackfired = true;
175
+ }
176
+
170
177
  private final int QUERY = 1314;
171
178
  private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
172
179
  private Future<?> future;
@@ -290,7 +297,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
290
297
 
291
298
  // find cached result if `key` property exists
292
299
  String cacheKey = this.taskId;
293
- String ext = this.options.appendExt.isEmpty() ? "" : "." + this.options.appendExt;
300
+ String ext = (this.options.appendExt == null || this.options.appendExt.isEmpty()) ? "" : "." + this.options.appendExt;
294
301
 
295
302
  if (this.options.key != null) {
296
303
  cacheKey = ReactNativeBlobUtilUtils.getMD5(this.options.key);
@@ -301,7 +308,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
301
308
  File file = new File(ReactNativeBlobUtilFS.getTmpPath(cacheKey) + ext);
302
309
 
303
310
  if (file.exists()) {
304
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
311
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
305
312
  return;
306
313
  }
307
314
  }
@@ -355,7 +362,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
355
362
  }
356
363
 
357
364
  if (!found) {
358
- callback.invoke("No available WiFi connections.", null, null);
365
+ invoke_callback("No available WiFi connections.", null, null);
359
366
  releaseTaskResource();
360
367
  return;
361
368
  }
@@ -395,7 +402,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
395
402
 
396
403
  if (rawRequestBodyArray != null) {
397
404
  requestType = RequestType.Form;
398
- } else if (cType.isEmpty()) {
405
+ } else if (cType == null || cType.isEmpty()) {
399
406
  if (!cType.equalsIgnoreCase("")) {
400
407
  builder.header("Content-Type", "application/octet-stream");
401
408
  }
@@ -551,9 +558,9 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
551
558
  // check if this error caused by socket timeout
552
559
  if (e.getClass().equals(SocketTimeoutException.class)) {
553
560
  respInfo.putBoolean("timeout", true);
554
- callback.invoke("The request timed out.", null, null);
561
+ invoke_callback("The request timed out.", null, null);
555
562
  } else
556
- callback.invoke(e.getLocalizedMessage(), null, null);
563
+ invoke_callback(e.getLocalizedMessage(), null, null);
557
564
  releaseTaskResource();
558
565
  }
559
566
 
@@ -586,7 +593,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
586
593
  } catch (Exception error) {
587
594
  error.printStackTrace();
588
595
  releaseTaskResource();
589
- callback.invoke("ReactNativeBlobUtil request error: " + error.getMessage() + error.getCause());
596
+ invoke_callback("ReactNativeBlobUtil request error: " + error.getMessage() + error.getCause());
590
597
  }
591
598
  }
592
599
 
@@ -631,7 +638,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
631
638
  ins.close();
632
639
  os.flush();
633
640
  os.close();
634
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
641
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
635
642
  }
636
643
  // response data directly pass to JS context as string.
637
644
  else {
@@ -649,15 +656,15 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
649
656
  }
650
657
  try (FileOutputStream fos = new FileOutputStream(file)) {
651
658
  fos.write(ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(b));
652
- } catch(Exception e) {
653
- callback.invoke("Error from file transformer:" + e.getLocalizedMessage(), null);
659
+ } catch (Exception e) {
660
+ invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null);
654
661
  return;
655
662
  }
656
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
663
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
657
664
  return;
658
665
  }
659
666
  if (responseFormat == ResponseFormat.BASE64) {
660
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
667
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
661
668
  return;
662
669
  }
663
670
  try {
@@ -667,21 +674,21 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
667
674
  decoder.decode(ByteBuffer.wrap(b));
668
675
  // If the data contains invalid characters the following lines will be skipped.
669
676
  String utf8 = new String(b, charSet);
670
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
677
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
671
678
  }
672
679
  // This usually means the data contains invalid unicode characters but still valid data,
673
680
  // it's binary data, so send it as a normal string
674
681
  catch (CharacterCodingException ignored) {
675
682
  if (responseFormat == ResponseFormat.UTF8) {
676
683
  String utf8 = new String(b);
677
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
684
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
678
685
  } else {
679
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
686
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
680
687
  }
681
688
  }
682
689
  }
683
690
  } catch (IOException e) {
684
- callback.invoke("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
691
+ invoke_callback("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
685
692
  }
686
693
  break;
687
694
  case FileStorage:
@@ -713,26 +720,26 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
713
720
  } catch (IOException exception) {
714
721
  exception.printStackTrace();
715
722
  }
716
- callback.invoke("Unexpected FileStorage response file: " + responseBodyString, null);
723
+ invoke_callback("Unexpected FileStorage response file: " + responseBodyString, null);
717
724
  } else {
718
- callback.invoke("Unexpected FileStorage response with no file.", null);
725
+ invoke_callback("Unexpected FileStorage response with no file.", null);
719
726
  }
720
727
  return;
721
728
  }
722
729
 
723
730
  if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) {
724
- callback.invoke("Download interrupted.", null);
731
+ invoke_callback("Download interrupted.", null);
725
732
  } else {
726
733
  this.destPath = this.destPath.replace("?append=true", "");
727
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
734
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
728
735
  }
729
736
 
730
737
  break;
731
738
  default:
732
739
  try {
733
- callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
740
+ invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
734
741
  } catch (IOException e) {
735
- callback.invoke("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
742
+ invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
736
743
  }
737
744
  break;
738
745
  }
@@ -856,7 +863,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
856
863
  Cursor c = dm.query(query);
857
864
  // #236 unhandled null check for DownloadManager.query() return value
858
865
  if (c == null) {
859
- this.callback.invoke("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
866
+ this.invoke_callback("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
860
867
  return;
861
868
  }
862
869
 
@@ -867,7 +874,7 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
867
874
  // #297 handle failed request
868
875
  int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
869
876
  if (statusCode == DownloadManager.STATUS_FAILED) {
870
- this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
877
+ this.invoke_callback("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
871
878
  return;
872
879
  }
873
880
  String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
@@ -896,17 +903,17 @@ public class ReactNativeBlobUtilReq extends BroadcastReceiver implements Runnabl
896
903
  if (!exists)
897
904
  throw new Exception("Download manager download failed, the file does not downloaded to destination.");
898
905
  else
899
- this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
906
+ this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
900
907
 
901
908
  } catch (Exception ex) {
902
909
  ex.printStackTrace();
903
- this.callback.invoke(ex.getLocalizedMessage(), null);
910
+ this.invoke_callback(ex.getLocalizedMessage(), null);
904
911
  }
905
912
  } else {
906
913
  if (filePath == null)
907
- this.callback.invoke("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
914
+ this.invoke_callback("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
908
915
  else
909
- this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
916
+ this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
910
917
  }
911
918
 
912
919
  }
@@ -27,7 +27,7 @@ public class MimeType {
27
27
  public static String getFullFileName(String name, String mimeType) {
28
28
  // Prior to API 29, MimeType.BINARY_FILE has no file extension
29
29
  String ext = MimeType.getExtensionFromMimeType(mimeType);
30
- if (ext.isEmpty() || name.endsWith("." + "ext")) return name;
30
+ if ((ext == null || ext.isEmpty()) || name.endsWith("." + "ext")) return name;
31
31
  else {
32
32
  String fn = name + "." + ext;
33
33
  if (fn.endsWith(".")) return StringUtils.stripEnd(fn, ".");
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.2",
3
+ "version": "0.16.4",
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": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "base-64": "0.1.0",
11
- "glob": "^7.1.6"
11
+ "glob": "^7.2.3"
12
12
  },
13
13
  "keywords": [
14
14
  "react-native",