react-native-blob-util 0.19.8 → 0.19.10

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.
package/README.md CHANGED
@@ -951,6 +951,30 @@ public class MainApplication extends Application implements ReactApplication {
951
951
  }
952
952
  ````
953
953
 
954
+ #### Kotlin
955
+ ````kotlin
956
+ ....
957
+ import com.ReactNativeBlobUtil.ReactNativeBlobUtilUtils;
958
+ import javax.net.ssl.X509TrustManager
959
+ ...
960
+
961
+ public class MainApplication extends Application implements ReactApplication {
962
+ ...
963
+ public void onCreate() {
964
+ ...
965
+ ReactNativeBlobUtilUtils.sharedTrustManager = object : X509TrustManager {
966
+ override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {}
967
+
968
+ override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {}
969
+
970
+ override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
971
+ return arrayOf()
972
+ }
973
+ };
974
+ ...
975
+ }
976
+ ````
977
+
954
978
  ```js
955
979
  ReactNativeBlobUtil.config({
956
980
  trusty: true
@@ -519,15 +519,15 @@ class ReactNativeBlobUtilFS {
519
519
  * @param callback JS context callback
520
520
  */
521
521
  static void cp(String path, String dest, Callback callback) {
522
- path = ReactNativeBlobUtilUtils.normalizePath(path);
523
522
  dest = ReactNativeBlobUtilUtils.normalizePath(dest);
524
523
  InputStream in = null;
525
524
  OutputStream out = null;
526
525
  String message = "";
527
526
 
528
527
  try {
529
- if (!isPathExists(path)) {
530
- callback.invoke("Source file at path`" + path + "` does not exist");
528
+ in = inputStreamFromPath(path);
529
+ if (in == null) {
530
+ callback.invoke("Source file at path`" + path + "` does not exist or can not be opened");
531
531
  return;
532
532
  }
533
533
  if (!new File(dest).exists()) {
@@ -538,7 +538,6 @@ class ReactNativeBlobUtilFS {
538
538
  }
539
539
  }
540
540
 
541
- in = inputStreamFromPath(path);
542
541
  out = new FileOutputStream(dest);
543
542
 
544
543
  byte[] buf = new byte[10240];
@@ -679,39 +678,38 @@ class ReactNativeBlobUtilFS {
679
678
  * @param end End byte offset
680
679
  * @param encode NOT IMPLEMENTED
681
680
  */
682
- static void slice(String path, String dest, int start, int end, String encode, Promise promise) {
681
+ static void slice(String path, String dest, long start, long end, String encode, Promise promise) {
683
682
  try {
684
- path = ReactNativeBlobUtilUtils.normalizePath(path);
685
683
  dest = ReactNativeBlobUtilUtils.normalizePath(dest);
686
- File source = new File(path);
687
- if (source.isDirectory()) {
688
- promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
689
- return;
684
+
685
+ if (!path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT)) {
686
+ File file = new File(ReactNativeBlobUtilUtils.normalizePath(path));
687
+ if (file.isDirectory()) {
688
+ promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
689
+ return;
690
+ }
690
691
  }
691
- if (!source.exists()) {
692
+
693
+ InputStream in = inputStreamFromPath(path);
694
+ if (in == null) {
692
695
  promise.reject("ENOENT", "No such file '" + path + "'");
693
696
  return;
694
697
  }
695
- int size = (int) source.length();
696
- int max = Math.min(size, end);
697
- int expected = max - start;
698
- int now = 0;
699
- FileInputStream in = new FileInputStream(new File(path));
700
698
  FileOutputStream out = new FileOutputStream(new File(dest));
701
- int skipped = (int) in.skip(start);
699
+ long skipped = in.skip(start);
702
700
  if (skipped != start) {
703
- promise.reject("EUNSPECIFIED", "Skipped " + skipped + " instead of the specified " + start + " bytes, size is " + size);
701
+ promise.reject("EUNSPECIFIED", "Skipped " + skipped + " instead of the specified " + start + " bytes");
704
702
  return;
705
703
  }
706
704
  byte[] buffer = new byte[10240];
707
- while (now < expected) {
705
+ int remain = (int) (end - start);
706
+ while (remain > 0) {
708
707
  int read = in.read(buffer, 0, 10240);
709
- int remain = expected - now;
710
708
  if (read <= 0) {
711
709
  break;
712
710
  }
713
711
  out.write(buffer, 0, (int) Math.min(remain, read));
714
- now += read;
712
+ remain -= read;
715
713
  }
716
714
  in.close();
717
715
  out.flush();
@@ -847,31 +845,27 @@ class ReactNativeBlobUtilFS {
847
845
  return;
848
846
  }
849
847
 
850
- path = ReactNativeBlobUtilUtils.normalizePath(path);
851
-
852
- File file = new File(path);
853
-
854
- if (file.isDirectory()) {
855
- promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
856
- return;
848
+ if (!path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT)) {
849
+ File file = new File(ReactNativeBlobUtilUtils.normalizePath(path));
850
+ if (file.isDirectory()) {
851
+ promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
852
+ return;
853
+ }
857
854
  }
858
855
 
859
- if (!file.exists()) {
856
+ MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
857
+
858
+ InputStream inputStream = inputStreamFromPath(path);
859
+ if (inputStream == null) {
860
860
  promise.reject("ENOENT", "No such file '" + path + "'");
861
861
  return;
862
862
  }
863
-
864
- MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
865
-
866
- FileInputStream inputStream = new FileInputStream(path);
867
863
  int chunkSize = 4096 * 256; // 1Mb
868
864
  byte[] buffer = new byte[chunkSize];
869
865
 
870
- if (file.length() != 0) {
871
- int bytesRead;
872
- while ((bytesRead = inputStream.read(buffer)) != -1) {
873
- md.update(buffer, 0, bytesRead);
874
- }
866
+ int bytesRead;
867
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
868
+ md.update(buffer, 0, bytesRead);
875
869
  }
876
870
 
877
871
  StringBuilder hexString = new StringBuilder();
@@ -1018,8 +1012,10 @@ class ReactNativeBlobUtilFS {
1018
1012
  }
1019
1013
 
1020
1014
  /**
1021
- * Get input stream of the given path, when the path is a string starts with bundle-assets://
1022
- * the stream is created by Assets Manager, otherwise use FileInputStream.
1015
+ * Get input stream of the given path.
1016
+ * When the path starts with bundle-assets:// the stream is created by Assets Manager
1017
+ * When the path starts with content:// the stream is created by ContentResolver
1018
+ * otherwise use FileInputStream.
1023
1019
  *
1024
1020
  * @param path The file to open stream
1025
1021
  * @return InputStream instance
@@ -1029,7 +1025,10 @@ class ReactNativeBlobUtilFS {
1029
1025
  if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
1030
1026
  return ReactNativeBlobUtilImpl.RCTContext.getAssets().open(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, ""));
1031
1027
  }
1032
- return new FileInputStream(new File(path));
1028
+ if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_CONTENT)) {
1029
+ return ReactNativeBlobUtilImpl.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
1030
+ }
1031
+ return new FileInputStream(new File(ReactNativeBlobUtilUtils.normalizePath(path)));
1033
1032
  }
1034
1033
 
1035
1034
  /**
@@ -294,7 +294,7 @@ class ReactNativeBlobUtilImpl {
294
294
  }
295
295
  }
296
296
 
297
- public void slice(String src, String dest, int start, int end, Promise promise) {
297
+ public void slice(String src, String dest, long start, long end, Promise promise) {
298
298
  ReactNativeBlobUtilFS.slice(src, dest, start, end, "", promise);
299
299
  }
300
300
 
@@ -148,11 +148,15 @@ public class ReactNativeBlobUtilStream {
148
148
  * @param callback Callback
149
149
  */
150
150
  void writeStream(String path, String encoding, boolean append, Callback callback) {
151
+ String resolved = ReactNativeBlobUtilUtils.normalizePath(path);
152
+ if (resolved != null)
153
+ path = resolved;
154
+
151
155
  try {
152
156
  File dest = new File(path);
153
157
  File dir = dest.getParentFile();
154
158
 
155
- if (!dest.exists()) {
159
+ if (resolved != null && !dest.exists()) {
156
160
  if (dir != null && !dir.exists()) {
157
161
  if (!dir.mkdirs()) {
158
162
  callback.invoke("ENOTDIR", "Failed to create parent directory of '" + path + "'");
@@ -168,7 +172,16 @@ public class ReactNativeBlobUtilStream {
168
172
  return;
169
173
  }
170
174
 
171
- OutputStream fs = new FileOutputStream(path, append);
175
+ OutputStream fs;
176
+ if (resolved != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
177
+ fs = ReactNativeBlobUtilImpl.RCTContext.getAssets().openFd(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "")).createOutputStream ();
178
+ }
179
+ // fix issue 287
180
+ else if (resolved == null) {
181
+ fs = ReactNativeBlobUtilImpl.RCTContext.getContentResolver().openOutputStream(Uri.parse(path));
182
+ } else {
183
+ fs = new FileOutputStream(path, append);
184
+ }
172
185
  this.encoding = encoding;
173
186
  String streamId = UUID.randomUUID().toString();
174
187
  ReactNativeBlobUtilStream.fileStreams.put(streamId, this);
@@ -274,20 +287,4 @@ public class ReactNativeBlobUtilStream {
274
287
  eventData.putString("streamId", streamName);
275
288
  this.emitter.emit(EVENT_FILESYSTEM, eventData);
276
289
  }
277
-
278
- /**
279
- * Get input stream of the given path, when the path is a string starts with bundle-assets://
280
- * the stream is created by Assets Manager, otherwise use FileInputStream.
281
- *
282
- * @param path The file to open stream
283
- * @return InputStream instance
284
- * @throws IOException If the given file does not exist or is a directory FileInputStream will throw a FileNotFoundException
285
- */
286
- public static InputStream inputStreamFromPath(String path) throws IOException {
287
- if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
288
- return ReactNativeBlobUtilImpl.RCTContext.getAssets().open(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, ""));
289
- }
290
- return new FileInputStream(new File(path));
291
- }
292
-
293
290
  }
@@ -198,7 +198,7 @@ public class ReactNativeBlobUtil extends NativeBlobUtilsSpec {
198
198
 
199
199
  @Override
200
200
  public void slice(String src, String dest, double start, double end, Promise promise) {
201
- delegate.slice(src, dest, (int) start, (int) end, promise);
201
+ delegate.slice(src, dest, (long) start, (long) end, promise);
202
202
  }
203
203
 
204
204
  @Override
@@ -174,8 +174,8 @@ public class ReactNativeBlobUtil extends ReactContextBaseJavaModule {
174
174
  }
175
175
 
176
176
  @ReactMethod
177
- public void slice(String src, String dest, int start, int end, Promise promise) {
178
- delegate.slice(src, dest, start, end, promise);
177
+ public void slice(String src, String dest, double start, double end, Promise promise) {
178
+ delegate.slice(src, dest, (long) start, (long) end, promise);
179
179
  }
180
180
 
181
181
  @ReactMethod