react-native-blob-util 0.13.18 → 0.15.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.
- package/README.md +542 -401
- package/android/build.gradle +1 -0
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java +89 -25
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilBody.java +14 -15
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilConfig.java +10 -6
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java +115 -289
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFileTransformer.java +10 -0
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilMediaCollection.java +314 -0
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilPackage.java +6 -2
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java +46 -22
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilStream.java +287 -0
- package/android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilUtils.java +57 -2
- package/android/src/main/java/com/ReactNativeBlobUtil/Response/ReactNativeBlobUtilFileResp.java +9 -0
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/FileDescription.java +19 -0
- package/android/src/main/java/com/ReactNativeBlobUtil/Utils/MimeType.java +70 -0
- package/class/ReactNativeBlobUtilBlobResponse.js +1 -1
- package/fs.js +44 -4
- package/index.d.ts +121 -1
- package/index.js +2 -0
- package/ios/ReactNativeBlobUtil/ReactNativeBlobUtil.m +6 -4
- package/ios/ReactNativeBlobUtil.xcodeproj/project.pbxproj +6 -0
- package/ios/ReactNativeBlobUtilConst.h +1 -0
- package/ios/ReactNativeBlobUtilConst.m +1 -0
- package/ios/ReactNativeBlobUtilFS.h +3 -1
- package/ios/ReactNativeBlobUtilFS.m +33 -0
- package/ios/ReactNativeBlobUtilFileTransformer.h +24 -0
- package/ios/ReactNativeBlobUtilFileTransformer.m +21 -0
- package/ios/ReactNativeBlobUtilReqBuilder.m +2 -2
- package/ios/ReactNativeBlobUtilRequest.m +30 -1
- package/mediacollection.js +38 -0
- package/package.json +1 -1
- package/polyfill/Fetch.js +4 -2
- package/scripts/prelink.js +1 -1
- package/types.js +4 -0
|
@@ -7,10 +7,8 @@ import android.os.AsyncTask;
|
|
|
7
7
|
import android.os.Build;
|
|
8
8
|
import android.os.Environment;
|
|
9
9
|
import android.os.StatFs;
|
|
10
|
-
import android.os.SystemClock;
|
|
11
10
|
import android.util.Base64;
|
|
12
11
|
|
|
13
|
-
import com.ReactNativeBlobUtil.Utils.PathResolver;
|
|
14
12
|
import com.facebook.react.bridge.Arguments;
|
|
15
13
|
import com.facebook.react.bridge.Callback;
|
|
16
14
|
import com.facebook.react.bridge.Promise;
|
|
@@ -20,13 +18,18 @@ import com.facebook.react.bridge.WritableArray;
|
|
|
20
18
|
import com.facebook.react.bridge.WritableMap;
|
|
21
19
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
22
20
|
|
|
23
|
-
import java.io
|
|
24
|
-
import java.
|
|
21
|
+
import java.io.File;
|
|
22
|
+
import java.io.FileInputStream;
|
|
23
|
+
import java.io.FileNotFoundException;
|
|
24
|
+
import java.io.FileOutputStream;
|
|
25
|
+
import java.io.IOException;
|
|
26
|
+
import java.io.InputStream;
|
|
27
|
+
import java.io.OutputStream;
|
|
25
28
|
import java.security.MessageDigest;
|
|
26
29
|
import java.util.ArrayList;
|
|
27
30
|
import java.util.HashMap;
|
|
31
|
+
import java.util.Locale;
|
|
28
32
|
import java.util.Map;
|
|
29
|
-
import java.util.UUID;
|
|
30
33
|
|
|
31
34
|
class ReactNativeBlobUtilFS {
|
|
32
35
|
|
|
@@ -41,6 +44,76 @@ class ReactNativeBlobUtilFS {
|
|
|
41
44
|
this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
|
42
45
|
}
|
|
43
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Write string with encoding to file (used for mediastore)
|
|
49
|
+
*
|
|
50
|
+
* @param path Destination file path.
|
|
51
|
+
* @param encoding Encoding of the string.
|
|
52
|
+
* @param data Array passed from JS context.
|
|
53
|
+
*/
|
|
54
|
+
static boolean writeFile(String path, String encoding, String data, final boolean append) {
|
|
55
|
+
try {
|
|
56
|
+
int written;
|
|
57
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
58
|
+
File f = new File(path);
|
|
59
|
+
File dir = f.getParentFile();
|
|
60
|
+
if (!f.exists()) {
|
|
61
|
+
if (dir != null && !dir.exists()) {
|
|
62
|
+
if (!dir.mkdirs() && !dir.exists()) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!f.createNewFile()) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// write data from a file
|
|
72
|
+
if (encoding.equalsIgnoreCase(ReactNativeBlobUtilConst.DATA_ENCODE_URI)) {
|
|
73
|
+
String normalizedData = ReactNativeBlobUtilUtils.normalizePath(data);
|
|
74
|
+
File src = new File(normalizedData);
|
|
75
|
+
if (!src.exists()) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
byte[] buffer = new byte[10240];
|
|
79
|
+
int read;
|
|
80
|
+
written = 0;
|
|
81
|
+
FileInputStream fin = null;
|
|
82
|
+
FileOutputStream fout = null;
|
|
83
|
+
try {
|
|
84
|
+
fin = new FileInputStream(src);
|
|
85
|
+
fout = new FileOutputStream(f, append);
|
|
86
|
+
while ((read = fin.read(buffer)) > 0) {
|
|
87
|
+
fout.write(buffer, 0, read);
|
|
88
|
+
written += read;
|
|
89
|
+
}
|
|
90
|
+
} finally {
|
|
91
|
+
if (fin != null) {
|
|
92
|
+
fin.close();
|
|
93
|
+
}
|
|
94
|
+
if (fout != null) {
|
|
95
|
+
fout.close();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
byte[] bytes = ReactNativeBlobUtilUtils.stringToBytes(data, encoding);
|
|
100
|
+
FileOutputStream fout = new FileOutputStream(f, append);
|
|
101
|
+
try {
|
|
102
|
+
fout.write(bytes);
|
|
103
|
+
written = bytes.length;
|
|
104
|
+
} finally {
|
|
105
|
+
fout.close();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
} catch (FileNotFoundException e) {
|
|
110
|
+
// According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
|
|
111
|
+
return false;
|
|
112
|
+
} catch (Exception e) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
44
117
|
/**
|
|
45
118
|
* Write string with encoding to file
|
|
46
119
|
*
|
|
@@ -49,12 +122,11 @@ class ReactNativeBlobUtilFS {
|
|
|
49
122
|
* @param data Array passed from JS context.
|
|
50
123
|
* @param promise RCT Promise
|
|
51
124
|
*/
|
|
52
|
-
static void writeFile(String path, String encoding, String data, final boolean append, final Promise promise) {
|
|
125
|
+
static void writeFile(String path, String encoding, String data, final boolean transformFile, final boolean append, final Promise promise) {
|
|
53
126
|
try {
|
|
54
127
|
int written;
|
|
55
128
|
File f = new File(path);
|
|
56
129
|
File dir = f.getParentFile();
|
|
57
|
-
|
|
58
130
|
if (!f.exists()) {
|
|
59
131
|
if (dir != null && !dir.exists()) {
|
|
60
132
|
if (!dir.mkdirs() && !dir.exists()) {
|
|
@@ -70,7 +142,7 @@ class ReactNativeBlobUtilFS {
|
|
|
70
142
|
|
|
71
143
|
// write data from a file
|
|
72
144
|
if (encoding.equalsIgnoreCase(ReactNativeBlobUtilConst.DATA_ENCODE_URI)) {
|
|
73
|
-
String normalizedData = normalizePath(data);
|
|
145
|
+
String normalizedData = ReactNativeBlobUtilUtils.normalizePath(data);
|
|
74
146
|
File src = new File(normalizedData);
|
|
75
147
|
if (!src.exists()) {
|
|
76
148
|
promise.reject("ENOENT", "No such file '" + path + "' " + "('" + normalizedData + "')");
|
|
@@ -97,7 +169,13 @@ class ReactNativeBlobUtilFS {
|
|
|
97
169
|
}
|
|
98
170
|
}
|
|
99
171
|
} else {
|
|
100
|
-
byte[] bytes = stringToBytes(data, encoding);
|
|
172
|
+
byte[] bytes = ReactNativeBlobUtilUtils.stringToBytes(data, encoding);
|
|
173
|
+
if (transformFile) {
|
|
174
|
+
if (ReactNativeBlobUtilFileTransformer.sharedFileTransformer == null) {
|
|
175
|
+
throw new IllegalStateException("Write file with transform was specified but the shared file transformer is not set");
|
|
176
|
+
}
|
|
177
|
+
bytes = ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(bytes);
|
|
178
|
+
}
|
|
101
179
|
FileOutputStream fout = new FileOutputStream(f, append);
|
|
102
180
|
try {
|
|
103
181
|
fout.write(bytes);
|
|
@@ -166,8 +244,8 @@ class ReactNativeBlobUtilFS {
|
|
|
166
244
|
* @param encoding Encoding of read stream.
|
|
167
245
|
* @param promise JS promise
|
|
168
246
|
*/
|
|
169
|
-
static void readFile(String path, String encoding, final Promise promise) {
|
|
170
|
-
String resolved = normalizePath(path);
|
|
247
|
+
static void readFile(String path, String encoding, final boolean transformFile, final Promise promise) {
|
|
248
|
+
String resolved = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
171
249
|
if (resolved != null)
|
|
172
250
|
path = resolved;
|
|
173
251
|
try {
|
|
@@ -209,7 +287,14 @@ class ReactNativeBlobUtilFS {
|
|
|
209
287
|
return;
|
|
210
288
|
}
|
|
211
289
|
|
|
212
|
-
|
|
290
|
+
if (transformFile) {
|
|
291
|
+
if (ReactNativeBlobUtilFileTransformer.sharedFileTransformer == null) {
|
|
292
|
+
throw new IllegalStateException("Read file with transform was specified but the shared file transformer is not set");
|
|
293
|
+
}
|
|
294
|
+
bytes = ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onReadFile(bytes);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
switch (encoding.toLowerCase(Locale.ROOT)) {
|
|
213
298
|
case "base64":
|
|
214
299
|
promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
|
|
215
300
|
break;
|
|
@@ -329,205 +414,6 @@ class ReactNativeBlobUtilFS {
|
|
|
329
414
|
return ReactNativeBlobUtil.RCTContext.getFilesDir() + "/ReactNativeBlobUtilTmp_" + taskId;
|
|
330
415
|
}
|
|
331
416
|
|
|
332
|
-
/**
|
|
333
|
-
* Create a file stream for read
|
|
334
|
-
*
|
|
335
|
-
* @param path File stream target path
|
|
336
|
-
* @param encoding File stream decoder, should be one of `base64`, `utf8`, `ascii`
|
|
337
|
-
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
|
|
338
|
-
*/
|
|
339
|
-
void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
|
|
340
|
-
String resolved = normalizePath(path);
|
|
341
|
-
if (resolved != null)
|
|
342
|
-
path = resolved;
|
|
343
|
-
|
|
344
|
-
try {
|
|
345
|
-
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
|
|
346
|
-
if (bufferSize > 0)
|
|
347
|
-
chunkSize = bufferSize;
|
|
348
|
-
|
|
349
|
-
InputStream fs;
|
|
350
|
-
|
|
351
|
-
if (resolved != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
|
|
352
|
-
fs = ReactNativeBlobUtil.RCTContext.getAssets().open(path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, ""));
|
|
353
|
-
}
|
|
354
|
-
// fix issue 287
|
|
355
|
-
else if (resolved == null) {
|
|
356
|
-
fs = ReactNativeBlobUtil.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
|
|
357
|
-
} else {
|
|
358
|
-
fs = new FileInputStream(new File(path));
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
int cursor = 0;
|
|
362
|
-
boolean error = false;
|
|
363
|
-
|
|
364
|
-
if (encoding.equalsIgnoreCase("utf8")) {
|
|
365
|
-
InputStreamReader isr = new InputStreamReader(fs, Charset.forName("UTF-8"));
|
|
366
|
-
BufferedReader reader = new BufferedReader(isr, chunkSize);
|
|
367
|
-
char[] buffer = new char[chunkSize];
|
|
368
|
-
// read chunks of the string
|
|
369
|
-
while (reader.read(buffer, 0, chunkSize) != -1) {
|
|
370
|
-
String chunk = new String(buffer);
|
|
371
|
-
emitStreamEvent(streamId, "data", chunk);
|
|
372
|
-
if (tick > 0)
|
|
373
|
-
SystemClock.sleep(tick);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
reader.close();
|
|
377
|
-
isr.close();
|
|
378
|
-
} else if (encoding.equalsIgnoreCase("ascii")) {
|
|
379
|
-
byte[] buffer = new byte[chunkSize];
|
|
380
|
-
while ((cursor = fs.read(buffer)) != -1) {
|
|
381
|
-
WritableArray chunk = Arguments.createArray();
|
|
382
|
-
for (int i = 0; i < cursor; i++) {
|
|
383
|
-
chunk.pushInt((int) buffer[i]);
|
|
384
|
-
}
|
|
385
|
-
emitStreamEvent(streamId, "data", chunk);
|
|
386
|
-
if (tick > 0)
|
|
387
|
-
SystemClock.sleep(tick);
|
|
388
|
-
}
|
|
389
|
-
} else if (encoding.equalsIgnoreCase("base64")) {
|
|
390
|
-
byte[] buffer = new byte[chunkSize];
|
|
391
|
-
while ((cursor = fs.read(buffer)) != -1) {
|
|
392
|
-
if (cursor < chunkSize) {
|
|
393
|
-
byte[] copy = new byte[cursor];
|
|
394
|
-
System.arraycopy(buffer, 0, copy, 0, cursor);
|
|
395
|
-
emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
|
|
396
|
-
} else
|
|
397
|
-
emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
|
|
398
|
-
if (tick > 0)
|
|
399
|
-
SystemClock.sleep(tick);
|
|
400
|
-
}
|
|
401
|
-
} else {
|
|
402
|
-
emitStreamEvent(
|
|
403
|
-
streamId,
|
|
404
|
-
"error",
|
|
405
|
-
"EINVAL",
|
|
406
|
-
"Unrecognized encoding `" + encoding + "`, should be one of `base64`, `utf8`, `ascii`"
|
|
407
|
-
);
|
|
408
|
-
error = true;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (!error)
|
|
412
|
-
emitStreamEvent(streamId, "end", "");
|
|
413
|
-
fs.close();
|
|
414
|
-
|
|
415
|
-
} catch (FileNotFoundException err) {
|
|
416
|
-
emitStreamEvent(
|
|
417
|
-
streamId,
|
|
418
|
-
"error",
|
|
419
|
-
"ENOENT",
|
|
420
|
-
"No such file '" + path + "'"
|
|
421
|
-
);
|
|
422
|
-
} catch (Exception err) {
|
|
423
|
-
emitStreamEvent(
|
|
424
|
-
streamId,
|
|
425
|
-
"error",
|
|
426
|
-
"EUNSPECIFIED",
|
|
427
|
-
"Failed to convert data to " + encoding + " encoded string. This might be because this encoding cannot be used for this data."
|
|
428
|
-
);
|
|
429
|
-
err.printStackTrace();
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* Create a write stream and store its instance in ReactNativeBlobUtilFS.fileStreams
|
|
435
|
-
*
|
|
436
|
-
* @param path Target file path
|
|
437
|
-
* @param encoding Should be one of `base64`, `utf8`, `ascii`
|
|
438
|
-
* @param append Flag represents if the file stream overwrite existing content
|
|
439
|
-
* @param callback Callback
|
|
440
|
-
*/
|
|
441
|
-
void writeStream(String path, String encoding, boolean append, Callback callback) {
|
|
442
|
-
try {
|
|
443
|
-
File dest = new File(path);
|
|
444
|
-
File dir = dest.getParentFile();
|
|
445
|
-
|
|
446
|
-
if (!dest.exists()) {
|
|
447
|
-
if (dir != null && !dir.exists()) {
|
|
448
|
-
if (!dir.mkdirs() && !dir.exists()) {
|
|
449
|
-
callback.invoke("ENOTDIR", "Failed to create parent directory of '" + path + "'");
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
if (!dest.createNewFile()) {
|
|
454
|
-
callback.invoke("ENOENT", "File '" + path + "' does not exist and could not be created");
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
} else if (dest.isDirectory()) {
|
|
458
|
-
callback.invoke("EISDIR", "Expecting a file but '" + path + "' is a directory");
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
OutputStream fs = new FileOutputStream(path, append);
|
|
463
|
-
this.encoding = encoding;
|
|
464
|
-
String streamId = UUID.randomUUID().toString();
|
|
465
|
-
ReactNativeBlobUtilFS.fileStreams.put(streamId, this);
|
|
466
|
-
this.writeStreamInstance = fs;
|
|
467
|
-
callback.invoke(null, null, streamId);
|
|
468
|
-
} catch (Exception err) {
|
|
469
|
-
callback.invoke("EUNSPECIFIED", "Failed to create write stream at path `" + path + "`; " + err.getLocalizedMessage());
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* Write a chunk of data into a file stream.
|
|
475
|
-
*
|
|
476
|
-
* @param streamId File stream ID
|
|
477
|
-
* @param data Data chunk in string format
|
|
478
|
-
* @param callback JS context callback
|
|
479
|
-
*/
|
|
480
|
-
static void writeChunk(String streamId, String data, Callback callback) {
|
|
481
|
-
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
|
|
482
|
-
OutputStream stream = fs.writeStreamInstance;
|
|
483
|
-
byte[] chunk = ReactNativeBlobUtilFS.stringToBytes(data, fs.encoding);
|
|
484
|
-
try {
|
|
485
|
-
stream.write(chunk);
|
|
486
|
-
callback.invoke();
|
|
487
|
-
} catch (Exception e) {
|
|
488
|
-
callback.invoke(e.getLocalizedMessage());
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Write data using ascii array
|
|
494
|
-
*
|
|
495
|
-
* @param streamId File stream ID
|
|
496
|
-
* @param data Data chunk in ascii array format
|
|
497
|
-
* @param callback JS context callback
|
|
498
|
-
*/
|
|
499
|
-
static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
|
|
500
|
-
try {
|
|
501
|
-
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
|
|
502
|
-
OutputStream stream = fs.writeStreamInstance;
|
|
503
|
-
byte[] chunk = new byte[data.size()];
|
|
504
|
-
for (int i = 0; i < data.size(); i++) {
|
|
505
|
-
chunk[i] = (byte) data.getInt(i);
|
|
506
|
-
}
|
|
507
|
-
stream.write(chunk);
|
|
508
|
-
callback.invoke();
|
|
509
|
-
} catch (Exception e) {
|
|
510
|
-
callback.invoke(e.getLocalizedMessage());
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Close file write stream by ID
|
|
516
|
-
*
|
|
517
|
-
* @param streamId Stream ID
|
|
518
|
-
* @param callback JS context callback
|
|
519
|
-
*/
|
|
520
|
-
static void closeStream(String streamId, Callback callback) {
|
|
521
|
-
try {
|
|
522
|
-
ReactNativeBlobUtilFS fs = fileStreams.get(streamId);
|
|
523
|
-
OutputStream stream = fs.writeStreamInstance;
|
|
524
|
-
fileStreams.remove(streamId);
|
|
525
|
-
stream.close();
|
|
526
|
-
callback.invoke();
|
|
527
|
-
} catch (Exception err) {
|
|
528
|
-
callback.invoke(err.getLocalizedMessage());
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
417
|
|
|
532
418
|
/**
|
|
533
419
|
* Unlink file at path
|
|
@@ -537,7 +423,7 @@ class ReactNativeBlobUtilFS {
|
|
|
537
423
|
*/
|
|
538
424
|
static void unlink(String path, Callback callback) {
|
|
539
425
|
try {
|
|
540
|
-
String normalizedPath = normalizePath(path);
|
|
426
|
+
String normalizedPath = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
541
427
|
ReactNativeBlobUtilFS.deleteRecursive(new File(normalizedPath));
|
|
542
428
|
callback.invoke(null, true);
|
|
543
429
|
} catch (Exception err) {
|
|
@@ -569,6 +455,7 @@ class ReactNativeBlobUtilFS {
|
|
|
569
455
|
* @param promise JS promise
|
|
570
456
|
*/
|
|
571
457
|
static void mkdir(String path, Promise promise) {
|
|
458
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
572
459
|
File dest = new File(path);
|
|
573
460
|
if (dest.exists()) {
|
|
574
461
|
promise.reject("EEXIST", (dest.isDirectory() ? "Folder" : "File") + " '" + path + "' already exists");
|
|
@@ -595,7 +482,8 @@ class ReactNativeBlobUtilFS {
|
|
|
595
482
|
* @param callback JS context callback
|
|
596
483
|
*/
|
|
597
484
|
static void cp(String path, String dest, Callback callback) {
|
|
598
|
-
path = normalizePath(path);
|
|
485
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
486
|
+
dest = ReactNativeBlobUtilUtils.normalizePath(dest);
|
|
599
487
|
InputStream in = null;
|
|
600
488
|
OutputStream out = null;
|
|
601
489
|
String message = "";
|
|
@@ -652,6 +540,8 @@ class ReactNativeBlobUtilFS {
|
|
|
652
540
|
* @param callback JS context callback
|
|
653
541
|
*/
|
|
654
542
|
static void mv(String path, String dest, Callback callback) {
|
|
543
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
544
|
+
dest = ReactNativeBlobUtilUtils.normalizePath(dest);
|
|
655
545
|
File src = new File(path);
|
|
656
546
|
if (!src.exists()) {
|
|
657
547
|
callback.invoke("Source file at path `" + path + "` does not exist");
|
|
@@ -700,7 +590,7 @@ class ReactNativeBlobUtilFS {
|
|
|
700
590
|
callback.invoke(false, false);
|
|
701
591
|
}
|
|
702
592
|
} else {
|
|
703
|
-
path = normalizePath(path);
|
|
593
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
704
594
|
if (path != null) {
|
|
705
595
|
boolean exist = new File(path).exists();
|
|
706
596
|
boolean isDir = new File(path).isDirectory();
|
|
@@ -719,7 +609,7 @@ class ReactNativeBlobUtilFS {
|
|
|
719
609
|
*/
|
|
720
610
|
static void ls(String path, Promise promise) {
|
|
721
611
|
try {
|
|
722
|
-
path = normalizePath(path);
|
|
612
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
723
613
|
File src = new File(path);
|
|
724
614
|
if (!src.exists()) {
|
|
725
615
|
promise.reject("ENOENT", "No such file '" + path + "'");
|
|
@@ -754,7 +644,8 @@ class ReactNativeBlobUtilFS {
|
|
|
754
644
|
*/
|
|
755
645
|
static void slice(String path, String dest, int start, int end, String encode, Promise promise) {
|
|
756
646
|
try {
|
|
757
|
-
path = normalizePath(path);
|
|
647
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
648
|
+
dest = ReactNativeBlobUtilUtils.normalizePath(dest);
|
|
758
649
|
File source = new File(path);
|
|
759
650
|
if (source.isDirectory()) {
|
|
760
651
|
promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
|
|
@@ -796,7 +687,7 @@ class ReactNativeBlobUtilFS {
|
|
|
796
687
|
}
|
|
797
688
|
|
|
798
689
|
static void lstat(String path, final Callback callback) {
|
|
799
|
-
path = normalizePath(path);
|
|
690
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
800
691
|
|
|
801
692
|
new AsyncTask<String, Integer, Integer>() {
|
|
802
693
|
@Override
|
|
@@ -835,7 +726,7 @@ class ReactNativeBlobUtilFS {
|
|
|
835
726
|
*/
|
|
836
727
|
static void stat(String path, Callback callback) {
|
|
837
728
|
try {
|
|
838
|
-
path = normalizePath(path);
|
|
729
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
839
730
|
WritableMap result = statFile(path);
|
|
840
731
|
if (result == null)
|
|
841
732
|
callback.invoke("failed to stat path `" + path + "` because it does not exist or it is not a folder", null);
|
|
@@ -854,7 +745,7 @@ class ReactNativeBlobUtilFS {
|
|
|
854
745
|
*/
|
|
855
746
|
static WritableMap statFile(String path) {
|
|
856
747
|
try {
|
|
857
|
-
path = normalizePath(path);
|
|
748
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
858
749
|
WritableMap stat = Arguments.createMap();
|
|
859
750
|
if (isAsset(path)) {
|
|
860
751
|
String name = path.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
|
|
@@ -918,6 +809,8 @@ class ReactNativeBlobUtilFS {
|
|
|
918
809
|
promise.reject("EINVAL", "Invalid algorithm '" + algorithm + "', must be one of md5, sha1, sha224, sha256, sha384, sha512");
|
|
919
810
|
return;
|
|
920
811
|
}
|
|
812
|
+
|
|
813
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
921
814
|
|
|
922
815
|
File file = new File(path);
|
|
923
816
|
|
|
@@ -965,6 +858,7 @@ class ReactNativeBlobUtilFS {
|
|
|
965
858
|
*/
|
|
966
859
|
static void createFile(String path, String data, String encoding, Promise promise) {
|
|
967
860
|
try {
|
|
861
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
968
862
|
File dest = new File(path);
|
|
969
863
|
boolean created = dest.createNewFile();
|
|
970
864
|
if (encoding.equals(ReactNativeBlobUtilConst.DATA_ENCODE_URI)) {
|
|
@@ -990,7 +884,7 @@ class ReactNativeBlobUtilFS {
|
|
|
990
884
|
return;
|
|
991
885
|
}
|
|
992
886
|
OutputStream ostream = new FileOutputStream(dest);
|
|
993
|
-
ostream.write(
|
|
887
|
+
ostream.write(ReactNativeBlobUtilUtils.stringToBytes(data, encoding));
|
|
994
888
|
}
|
|
995
889
|
promise.resolve(path);
|
|
996
890
|
} catch (Exception err) {
|
|
@@ -1007,6 +901,7 @@ class ReactNativeBlobUtilFS {
|
|
|
1007
901
|
*/
|
|
1008
902
|
static void createFileASCII(String path, ReadableArray data, Promise promise) {
|
|
1009
903
|
try {
|
|
904
|
+
path = ReactNativeBlobUtilUtils.normalizePath(path);
|
|
1010
905
|
File dest = new File(path);
|
|
1011
906
|
boolean created = dest.createNewFile();
|
|
1012
907
|
if (!created) {
|
|
@@ -1085,56 +980,6 @@ class ReactNativeBlobUtilFS {
|
|
|
1085
980
|
task.execute(paths);
|
|
1086
981
|
}
|
|
1087
982
|
|
|
1088
|
-
/**
|
|
1089
|
-
* String to byte converter method
|
|
1090
|
-
*
|
|
1091
|
-
* @param data Raw data in string format
|
|
1092
|
-
* @param encoding Decoder name
|
|
1093
|
-
* @return Converted data byte array
|
|
1094
|
-
*/
|
|
1095
|
-
private static byte[] stringToBytes(String data, String encoding) {
|
|
1096
|
-
if (encoding.equalsIgnoreCase("ascii")) {
|
|
1097
|
-
return data.getBytes(Charset.forName("US-ASCII"));
|
|
1098
|
-
} else if (encoding.toLowerCase().contains("base64")) {
|
|
1099
|
-
return Base64.decode(data, Base64.NO_WRAP);
|
|
1100
|
-
|
|
1101
|
-
} else if (encoding.equalsIgnoreCase("utf8")) {
|
|
1102
|
-
return data.getBytes(Charset.forName("UTF-8"));
|
|
1103
|
-
}
|
|
1104
|
-
return data.getBytes(Charset.forName("US-ASCII"));
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
/**
|
|
1108
|
-
* Private method for emit read stream event.
|
|
1109
|
-
*
|
|
1110
|
-
* @param streamName ID of the read stream
|
|
1111
|
-
* @param event Event name, `data`, `end`, `error`, etc.
|
|
1112
|
-
* @param data Event data
|
|
1113
|
-
*/
|
|
1114
|
-
private void emitStreamEvent(String streamName, String event, String data) {
|
|
1115
|
-
WritableMap eventData = Arguments.createMap();
|
|
1116
|
-
eventData.putString("event", event);
|
|
1117
|
-
eventData.putString("detail", data);
|
|
1118
|
-
this.emitter.emit(streamName, eventData);
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
// "event" always is "data"...
|
|
1122
|
-
private void emitStreamEvent(String streamName, String event, WritableArray data) {
|
|
1123
|
-
WritableMap eventData = Arguments.createMap();
|
|
1124
|
-
eventData.putString("event", event);
|
|
1125
|
-
eventData.putArray("detail", data);
|
|
1126
|
-
this.emitter.emit(streamName, eventData);
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
|
-
// "event" always is "error"...
|
|
1130
|
-
private void emitStreamEvent(String streamName, String event, String code, String message) {
|
|
1131
|
-
WritableMap eventData = Arguments.createMap();
|
|
1132
|
-
eventData.putString("event", event);
|
|
1133
|
-
eventData.putString("code", code);
|
|
1134
|
-
eventData.putString("detail", message);
|
|
1135
|
-
this.emitter.emit(streamName, eventData);
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
983
|
/**
|
|
1139
984
|
* Get input stream of the given path, when the path is a string starts with bundle-assets://
|
|
1140
985
|
* the stream is created by Assets Manager, otherwise use FileInputStream.
|
|
@@ -1174,23 +1019,4 @@ class ReactNativeBlobUtilFS {
|
|
|
1174
1019
|
return path != null && path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET);
|
|
1175
1020
|
}
|
|
1176
1021
|
|
|
1177
|
-
/**
|
|
1178
|
-
* Normalize the path, remove URI scheme (xxx://) so that we can handle it.
|
|
1179
|
-
*
|
|
1180
|
-
* @param path URI string.
|
|
1181
|
-
* @return Normalized string
|
|
1182
|
-
*/
|
|
1183
|
-
static String normalizePath(String path) {
|
|
1184
|
-
if (path == null)
|
|
1185
|
-
return null;
|
|
1186
|
-
if (!path.matches("\\w+\\:.*"))
|
|
1187
|
-
return path;
|
|
1188
|
-
|
|
1189
|
-
Uri uri = Uri.parse(path);
|
|
1190
|
-
if (path.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET)) {
|
|
1191
|
-
return path;
|
|
1192
|
-
} else
|
|
1193
|
-
return PathResolver.getRealPathFromURI(ReactNativeBlobUtil.RCTContext, uri);
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
1022
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
package com.ReactNativeBlobUtil;
|
|
2
|
+
|
|
3
|
+
public class ReactNativeBlobUtilFileTransformer {
|
|
4
|
+
public interface FileTransformer {
|
|
5
|
+
public byte[] onWriteFile(byte[] data);
|
|
6
|
+
public byte[] onReadFile(byte[] data);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public static ReactNativeBlobUtilFileTransformer.FileTransformer sharedFileTransformer;
|
|
10
|
+
}
|