|
@@ -1,7 +1,5 @@
|
1
|
1
|
package com.RNFetchBlob;
|
2
|
2
|
|
3
|
|
-import android.content.pm.PackageInfo;
|
4
|
|
-import android.content.pm.PackageManager;
|
5
|
3
|
import android.content.res.AssetFileDescriptor;
|
6
|
4
|
import android.media.MediaScannerConnection;
|
7
|
5
|
import android.net.Uri;
|
|
@@ -22,43 +20,29 @@ import com.facebook.react.bridge.WritableArray;
|
22
|
20
|
import com.facebook.react.bridge.WritableMap;
|
23
|
21
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
24
|
22
|
|
25
|
|
-import java.io.File;
|
26
|
|
-import java.io.FileInputStream;
|
27
|
|
-import java.io.FileOutputStream;
|
28
|
|
-import java.io.IOException;
|
29
|
|
-import java.io.InputStream;
|
30
|
|
-import java.io.OutputStream;
|
|
23
|
+import java.io.*;
|
31
|
24
|
import java.nio.ByteBuffer;
|
32
|
25
|
import java.nio.charset.Charset;
|
33
|
26
|
import java.nio.charset.CharsetEncoder;
|
34
|
27
|
import java.security.MessageDigest;
|
|
28
|
+import java.util.ArrayList;
|
35
|
29
|
import java.util.HashMap;
|
36
|
30
|
import java.util.Map;
|
37
|
31
|
import java.util.UUID;
|
38
|
32
|
|
39
|
|
-public class RNFetchBlobFS {
|
|
33
|
+class RNFetchBlobFS {
|
40
|
34
|
|
41
|
|
- ReactApplicationContext mCtx;
|
42
|
|
- DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
|
43
|
|
- String encoding = "base64";
|
44
|
|
- boolean append = false;
|
45
|
|
- OutputStream writeStreamInstance = null;
|
46
|
|
- static HashMap<String, RNFetchBlobFS> fileStreams = new HashMap<>();
|
|
35
|
+ private ReactApplicationContext mCtx;
|
|
36
|
+ private DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
|
|
37
|
+ private String encoding = "base64";
|
|
38
|
+ private OutputStream writeStreamInstance = null;
|
|
39
|
+ private static HashMap<String, RNFetchBlobFS> fileStreams = new HashMap<>();
|
47
|
40
|
|
48
|
41
|
RNFetchBlobFS(ReactApplicationContext ctx) {
|
49
|
42
|
this.mCtx = ctx;
|
50
|
43
|
this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
51
|
44
|
}
|
52
|
45
|
|
53
|
|
- static String getExternalFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
|
54
|
|
- if(config.path != null)
|
55
|
|
- return config.path;
|
56
|
|
- else if(config.fileCache && config.appendExt != null)
|
57
|
|
- return RNFetchBlobFS.getTmpPath(ctx, taskId) + "." + config.appendExt;
|
58
|
|
- else
|
59
|
|
- return RNFetchBlobFS.getTmpPath(ctx, taskId);
|
60
|
|
- }
|
61
|
|
-
|
62
|
46
|
/**
|
63
|
47
|
* Write string with encoding to file
|
64
|
48
|
* @param path Destination file path.
|
|
@@ -66,25 +50,37 @@ public class RNFetchBlobFS {
|
66
|
50
|
* @param data Array passed from JS context.
|
67
|
51
|
* @param promise RCT Promise
|
68
|
52
|
*/
|
69
|
|
- static public void writeFile(String path, String encoding, String data, final boolean append, final Promise promise) {
|
|
53
|
+ static void writeFile(String path, String encoding, String data, final boolean append, final Promise promise) {
|
70
|
54
|
try {
|
71
|
|
- int written = 0;
|
|
55
|
+ int written;
|
72
|
56
|
File f = new File(path);
|
73
|
57
|
File dir = f.getParentFile();
|
74
|
|
- if(!dir.exists())
|
75
|
|
- dir.mkdirs();
|
|
58
|
+
|
|
59
|
+ if(!f.exists()) {
|
|
60
|
+ if(dir != null && !dir.exists()) {
|
|
61
|
+ if (!dir.mkdirs()) {
|
|
62
|
+ promise.reject("EUNSPECIFIED", "Failed to create parent directory of '" + path + "'");
|
|
63
|
+ return;
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ if(!f.createNewFile()) {
|
|
67
|
+ promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created");
|
|
68
|
+ return;
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+
|
76
|
72
|
FileOutputStream fout = new FileOutputStream(f, append);
|
77
|
73
|
// write data from a file
|
78
|
74
|
if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
|
79
|
|
- data = normalizePath(data);
|
80
|
|
- File src = new File(data);
|
81
|
|
- if(!src.exists()) {
|
82
|
|
- promise.reject("RNfetchBlob writeFile error", "source file : " + data + " does not exist");
|
|
75
|
+ String normalizedData = normalizePath(data);
|
|
76
|
+ File src = new File(normalizedData);
|
|
77
|
+ if (!src.exists()) {
|
|
78
|
+ promise.reject("ENOENT", "No such file '" + path + "' " + "('" + normalizedData + "')");
|
83
|
79
|
fout.close();
|
84
|
|
- return ;
|
|
80
|
+ return;
|
85
|
81
|
}
|
86
|
82
|
FileInputStream fin = new FileInputStream(src);
|
87
|
|
- byte [] buffer = new byte [10240];
|
|
83
|
+ byte[] buffer = new byte [10240];
|
88
|
84
|
int read;
|
89
|
85
|
written = 0;
|
90
|
86
|
while((read = fin.read(buffer)) > 0) {
|
|
@@ -100,8 +96,11 @@ public class RNFetchBlobFS {
|
100
|
96
|
}
|
101
|
97
|
fout.close();
|
102
|
98
|
promise.resolve(written);
|
|
99
|
+ } catch (FileNotFoundException e) {
|
|
100
|
+ // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
|
|
101
|
+ promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created, or it is a directory");
|
103
|
102
|
} catch (Exception e) {
|
104
|
|
- promise.reject("RNFetchBlob writeFile error", e.getLocalizedMessage());
|
|
103
|
+ promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
|
105
|
104
|
}
|
106
|
105
|
}
|
107
|
106
|
|
|
@@ -111,24 +110,37 @@ public class RNFetchBlobFS {
|
111
|
110
|
* @param data Array passed from JS context.
|
112
|
111
|
* @param promise RCT Promise
|
113
|
112
|
*/
|
114
|
|
- static public void writeFile(String path, ReadableArray data, final boolean append, final Promise promise) {
|
115
|
|
-
|
|
113
|
+ static void writeFile(String path, ReadableArray data, final boolean append, final Promise promise) {
|
116
|
114
|
try {
|
117
|
|
-
|
118
|
115
|
File f = new File(path);
|
119
|
116
|
File dir = f.getParentFile();
|
120
|
|
- if(!dir.exists())
|
121
|
|
- dir.mkdirs();
|
|
117
|
+
|
|
118
|
+ if(!f.exists()) {
|
|
119
|
+ if(dir != null && !dir.exists()) {
|
|
120
|
+ if (!dir.mkdirs()) {
|
|
121
|
+ promise.reject("ENOTDIR", "Failed to create parent directory of '" + path + "'");
|
|
122
|
+ return;
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ if(!f.createNewFile()) {
|
|
126
|
+ promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created");
|
|
127
|
+ return;
|
|
128
|
+ }
|
|
129
|
+ }
|
|
130
|
+
|
122
|
131
|
FileOutputStream os = new FileOutputStream(f, append);
|
123
|
|
- byte [] bytes = new byte[data.size()];
|
|
132
|
+ byte[] bytes = new byte[data.size()];
|
124
|
133
|
for(int i=0;i<data.size();i++) {
|
125
|
134
|
bytes[i] = (byte) data.getInt(i);
|
126
|
135
|
}
|
127
|
136
|
os.write(bytes);
|
128
|
137
|
os.close();
|
129
|
138
|
promise.resolve(data.size());
|
|
139
|
+ } catch (FileNotFoundException e) {
|
|
140
|
+ // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
|
|
141
|
+ promise.reject("ENOENT", "File '" + path + "' does not exist and could not be created");
|
130
|
142
|
} catch (Exception e) {
|
131
|
|
- promise.reject("RNFetchBlob writeFile error", e.getLocalizedMessage());
|
|
143
|
+ promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
|
132
|
144
|
}
|
133
|
145
|
}
|
134
|
146
|
|
|
@@ -136,48 +148,60 @@ public class RNFetchBlobFS {
|
136
|
148
|
* Read file with a buffer that has the same size as the target file.
|
137
|
149
|
* @param path Path of the file.
|
138
|
150
|
* @param encoding Encoding of read stream.
|
139
|
|
- * @param promise
|
|
151
|
+ * @param promise JS promise
|
140
|
152
|
*/
|
141
|
|
- static public void readFile(String path, String encoding, final Promise promise ) {
|
|
153
|
+ static void readFile(String path, String encoding, final Promise promise) {
|
142
|
154
|
String resolved = normalizePath(path);
|
143
|
155
|
if(resolved != null)
|
144
|
156
|
path = resolved;
|
145
|
157
|
try {
|
146
|
158
|
byte[] bytes;
|
|
159
|
+ int bytesRead;
|
|
160
|
+ int length; // max. array length limited to "int", also see https://stackoverflow.com/a/10787175/544779
|
147
|
161
|
|
148
|
162
|
if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
|
149
|
163
|
String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
|
150
|
|
- long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
|
151
|
|
- bytes = new byte[(int) length];
|
|
164
|
+ // This fails should an asset file be >2GB
|
|
165
|
+ length = (int) RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
|
|
166
|
+ bytes = new byte[length];
|
152
|
167
|
InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
|
153
|
|
- in.read(bytes, 0, (int) length);
|
|
168
|
+ bytesRead = in.read(bytes, 0, length);
|
154
|
169
|
in.close();
|
155
|
170
|
}
|
156
|
171
|
// issue 287
|
157
|
172
|
else if(resolved == null) {
|
158
|
173
|
InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
|
159
|
|
- int length = (int) in.available();
|
|
174
|
+ // TODO See https://developer.android.com/reference/java/io/InputStream.html#available()
|
|
175
|
+ // Quote: "Note that while some implementations of InputStream will return the total number of bytes
|
|
176
|
+ // in the stream, many will not. It is never correct to use the return value of this method to
|
|
177
|
+ // allocate a buffer intended to hold all data in this stream."
|
|
178
|
+ length = in.available();
|
160
|
179
|
bytes = new byte[length];
|
161
|
|
- in.read(bytes);
|
|
180
|
+ bytesRead = in.read(bytes);
|
162
|
181
|
in.close();
|
163
|
182
|
}
|
164
|
183
|
else {
|
165
|
184
|
File f = new File(path);
|
166
|
|
- int length = (int) f.length();
|
|
185
|
+ length = (int) f.length();
|
167
|
186
|
bytes = new byte[length];
|
168
|
187
|
FileInputStream in = new FileInputStream(f);
|
169
|
|
- in.read(bytes);
|
|
188
|
+ bytesRead = in.read(bytes);
|
170
|
189
|
in.close();
|
171
|
190
|
}
|
172
|
191
|
|
|
192
|
+ if (bytesRead < length) {
|
|
193
|
+ promise.reject("EUNSPECIFIED", "Read only " + bytesRead + " bytes of " + length);
|
|
194
|
+ return;
|
|
195
|
+ }
|
|
196
|
+
|
173
|
197
|
switch (encoding.toLowerCase()) {
|
174
|
198
|
case "base64" :
|
175
|
199
|
promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
|
176
|
200
|
break;
|
177
|
201
|
case "ascii" :
|
178
|
202
|
WritableArray asciiResult = Arguments.createArray();
|
179
|
|
- for(byte b : bytes) {
|
180
|
|
- asciiResult.pushInt((int)b);
|
|
203
|
+ for (byte b : bytes) {
|
|
204
|
+ asciiResult.pushInt((int) b);
|
181
|
205
|
}
|
182
|
206
|
promise.resolve(asciiResult);
|
183
|
207
|
break;
|
|
@@ -189,8 +213,16 @@ public class RNFetchBlobFS {
|
189
|
213
|
break;
|
190
|
214
|
}
|
191
|
215
|
}
|
|
216
|
+ catch(FileNotFoundException err) {
|
|
217
|
+ String msg = err.getLocalizedMessage();
|
|
218
|
+ if (msg.contains("EISDIR")) {
|
|
219
|
+ promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory; " + msg);
|
|
220
|
+ } else {
|
|
221
|
+ promise.reject("ENOENT", "No such file '" + path + "'; " + msg);
|
|
222
|
+ }
|
|
223
|
+ }
|
192
|
224
|
catch(Exception err) {
|
193
|
|
- promise.reject("RNFetchBlob readFile error", err.getLocalizedMessage());
|
|
225
|
+ promise.reject("EUNSPECIFIED", err.getLocalizedMessage());
|
194
|
226
|
}
|
195
|
227
|
|
196
|
228
|
}
|
|
@@ -199,7 +231,7 @@ public class RNFetchBlobFS {
|
199
|
231
|
* Static method that returns system folders to JS context
|
200
|
232
|
* @param ctx React Native application context
|
201
|
233
|
*/
|
202
|
|
- static public Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
|
|
234
|
+ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
|
203
|
235
|
Map<String, Object> res = new HashMap<>();
|
204
|
236
|
|
205
|
237
|
res.put("DocumentDir", ctx.getFilesDir().getAbsolutePath());
|
|
@@ -222,11 +254,10 @@ public class RNFetchBlobFS {
|
222
|
254
|
|
223
|
255
|
/**
|
224
|
256
|
* Static method that returns a temp file path
|
225
|
|
- * @param ctx React Native application context
|
226
|
257
|
* @param taskId An unique string for identify
|
227
|
|
- * @return
|
|
258
|
+ * @return String
|
228
|
259
|
*/
|
229
|
|
- static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
|
|
260
|
+ static String getTmpPath(String taskId) {
|
230
|
261
|
return RNFetchBlob.RCTContext.getFilesDir() + "/RNFetchBlobTmp_" + taskId;
|
231
|
262
|
}
|
232
|
263
|
|
|
@@ -236,12 +267,12 @@ public class RNFetchBlobFS {
|
236
|
267
|
* @param encoding File stream decoder, should be one of `base64`, `utf8`, `ascii`
|
237
|
268
|
* @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
|
238
|
269
|
*/
|
239
|
|
- public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
|
|
270
|
+ void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
|
240
|
271
|
String resolved = normalizePath(path);
|
241
|
272
|
if(resolved != null)
|
242
|
273
|
path = resolved;
|
243
|
|
- try {
|
244
|
274
|
|
|
275
|
+ try {
|
245
|
276
|
int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
|
246
|
277
|
if(bufferSize > 0)
|
247
|
278
|
chunkSize = bufferSize;
|
|
@@ -250,7 +281,6 @@ public class RNFetchBlobFS {
|
250
|
281
|
|
251
|
282
|
if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
|
252
|
283
|
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
|
253
|
|
-
|
254
|
284
|
}
|
255
|
285
|
// fix issue 287
|
256
|
286
|
else if(resolved == null) {
|
|
@@ -287,10 +317,8 @@ public class RNFetchBlobFS {
|
287
|
317
|
} else if (encoding.equalsIgnoreCase("base64")) {
|
288
|
318
|
while ((cursor = fs.read(buffer)) != -1) {
|
289
|
319
|
if(cursor < chunkSize) {
|
290
|
|
- byte [] copy = new byte[cursor];
|
291
|
|
- for(int i =0;i<cursor;i++) {
|
292
|
|
- copy[i] = buffer[i];
|
293
|
|
- }
|
|
320
|
+ byte[] copy = new byte[cursor];
|
|
321
|
+ System.arraycopy(buffer, 0, copy, 0, cursor);
|
294
|
322
|
emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
|
295
|
323
|
}
|
296
|
324
|
else
|
|
@@ -299,8 +327,12 @@ public class RNFetchBlobFS {
|
299
|
327
|
SystemClock.sleep(tick);
|
300
|
328
|
}
|
301
|
329
|
} else {
|
302
|
|
- String msg = "unrecognized encoding `" + encoding + "`";
|
303
|
|
- emitStreamEvent(streamId, "error", msg);
|
|
330
|
+ emitStreamEvent(
|
|
331
|
+ streamId,
|
|
332
|
+ "error",
|
|
333
|
+ "EINVAL",
|
|
334
|
+ "Unrecognized encoding `" + encoding + "`, should be one of `base64`, `utf8`, `ascii`"
|
|
335
|
+ );
|
304
|
336
|
error = true;
|
305
|
337
|
}
|
306
|
338
|
|
|
@@ -308,10 +340,20 @@ public class RNFetchBlobFS {
|
308
|
340
|
emitStreamEvent(streamId, "end", "");
|
309
|
341
|
fs.close();
|
310
|
342
|
buffer = null;
|
311
|
|
-
|
|
343
|
+ } catch (FileNotFoundException err) {
|
|
344
|
+ emitStreamEvent(
|
|
345
|
+ streamId,
|
|
346
|
+ "error",
|
|
347
|
+ "ENOENT",
|
|
348
|
+ "No such file '" + path + "'"
|
|
349
|
+ );
|
312
|
350
|
} catch (Exception err) {
|
313
|
|
- emitStreamEvent(streamId, "warn", "Failed to convert data to " + encoding +
|
314
|
|
- " encoded string, this might due to the source data is not able to convert using this encoding.");
|
|
351
|
+ emitStreamEvent(
|
|
352
|
+ streamId,
|
|
353
|
+ "error",
|
|
354
|
+ "EUNSPECIFIED",
|
|
355
|
+ "Failed to convert data to " + encoding + " encoded string. This might be because this encoding cannot be used for this data."
|
|
356
|
+ );
|
315
|
357
|
err.printStackTrace();
|
316
|
358
|
}
|
317
|
359
|
}
|
|
@@ -319,28 +361,40 @@ public class RNFetchBlobFS {
|
319
|
361
|
/**
|
320
|
362
|
* Create a write stream and store its instance in RNFetchBlobFS.fileStreams
|
321
|
363
|
* @param path Target file path
|
322
|
|
- * @param encoding Should be one of `base64`, `utf8`, `ascii`
|
323
|
|
- * @param append Flag represents if the file stream overwrite existing content
|
324
|
|
- * @param callback
|
|
364
|
+ * @param encoding Should be one of `base64`, `utf8`, `ascii`
|
|
365
|
+ * @param append Flag represents if the file stream overwrite existing content
|
|
366
|
+ * @param callback Callback
|
325
|
367
|
*/
|
326
|
|
- public void writeStream(String path, String encoding, boolean append, Callback callback) {
|
327
|
|
- File dest = new File(path);
|
328
|
|
- if(!dest.exists() || dest.isDirectory()) {
|
329
|
|
- callback.invoke("target path `" + path + "` may not exist or it is a folder");
|
330
|
|
- return;
|
331
|
|
- }
|
|
368
|
+ void writeStream(String path, String encoding, boolean append, Callback callback) {
|
332
|
369
|
try {
|
|
370
|
+ File dest = new File(path);
|
|
371
|
+ File dir = dest.getParentFile();
|
|
372
|
+
|
|
373
|
+ if(!dest.exists()) {
|
|
374
|
+ if(dir != null && !dir.exists()) {
|
|
375
|
+ if (!dir.mkdirs()) {
|
|
376
|
+ callback.invoke("ENOTDIR", "Failed to create parent directory of '" + path + "'");
|
|
377
|
+ return;
|
|
378
|
+ }
|
|
379
|
+ }
|
|
380
|
+ if(!dest.createNewFile()) {
|
|
381
|
+ callback.invoke("ENOENT", "File '" + path + "' does not exist and could not be created");
|
|
382
|
+ return;
|
|
383
|
+ }
|
|
384
|
+ } else if(dest.isDirectory()) {
|
|
385
|
+ callback.invoke("EISDIR", "Expecting a file but '" + path + "' is a directory");
|
|
386
|
+ return;
|
|
387
|
+ }
|
|
388
|
+
|
333
|
389
|
OutputStream fs = new FileOutputStream(path, append);
|
334
|
390
|
this.encoding = encoding;
|
335
|
|
- this.append = append;
|
336
|
391
|
String streamId = UUID.randomUUID().toString();
|
337
|
392
|
RNFetchBlobFS.fileStreams.put(streamId, this);
|
338
|
393
|
this.writeStreamInstance = fs;
|
339
|
|
- callback.invoke(null, streamId);
|
|
394
|
+ callback.invoke(null, null, streamId);
|
340
|
395
|
} catch(Exception err) {
|
341
|
|
- callback.invoke("failed to create write stream at path `" + path + "` " + err.getLocalizedMessage());
|
|
396
|
+ callback.invoke("EUNSPECIFIED", "Failed to create write stream at path `" + path + "`; " + err.getLocalizedMessage());
|
342
|
397
|
}
|
343
|
|
-
|
344
|
398
|
}
|
345
|
399
|
|
346
|
400
|
/**
|
|
@@ -350,11 +404,9 @@ public class RNFetchBlobFS {
|
350
|
404
|
* @param callback JS context callback
|
351
|
405
|
*/
|
352
|
406
|
static void writeChunk(String streamId, String data, Callback callback) {
|
353
|
|
-
|
354
|
407
|
RNFetchBlobFS fs = fileStreams.get(streamId);
|
355
|
408
|
OutputStream stream = fs.writeStreamInstance;
|
356
|
|
- byte [] chunk = RNFetchBlobFS.stringToBytes(data, fs.encoding);
|
357
|
|
-
|
|
409
|
+ byte[] chunk = RNFetchBlobFS.stringToBytes(data, fs.encoding);
|
358
|
410
|
try {
|
359
|
411
|
stream.write(chunk);
|
360
|
412
|
callback.invoke();
|
|
@@ -370,11 +422,10 @@ public class RNFetchBlobFS {
|
370
|
422
|
* @param callback JS context callback
|
371
|
423
|
*/
|
372
|
424
|
static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
|
373
|
|
-
|
374
|
425
|
try {
|
375
|
426
|
RNFetchBlobFS fs = fileStreams.get(streamId);
|
376
|
427
|
OutputStream stream = fs.writeStreamInstance;
|
377
|
|
- byte [] chunk = new byte[data.size()];
|
|
428
|
+ byte[] chunk = new byte[data.size()];
|
378
|
429
|
for(int i =0; i< data.size();i++) {
|
379
|
430
|
chunk[i] = (byte) data.getInt(i);
|
380
|
431
|
}
|
|
@@ -412,34 +463,49 @@ public class RNFetchBlobFS {
|
412
|
463
|
RNFetchBlobFS.deleteRecursive(new File(path));
|
413
|
464
|
callback.invoke(null, true);
|
414
|
465
|
} catch(Exception err) {
|
415
|
|
- if(err != null)
|
416
|
466
|
callback.invoke(err.getLocalizedMessage(), false);
|
417
|
467
|
}
|
418
|
468
|
}
|
419
|
469
|
|
420
|
|
- static void deleteRecursive(File fileOrDirectory) {
|
421
|
|
-
|
|
470
|
+ private static void deleteRecursive(File fileOrDirectory) throws IOException {
|
422
|
471
|
if (fileOrDirectory.isDirectory()) {
|
423
|
|
- for (File child : fileOrDirectory.listFiles()) {
|
424
|
|
- deleteRecursive(child);
|
|
472
|
+ File[] files = fileOrDirectory.listFiles();
|
|
473
|
+ if (files == null) {
|
|
474
|
+ throw new NullPointerException("Received null trying to list files of directory '" + fileOrDirectory + "'");
|
|
475
|
+ } else {
|
|
476
|
+ for (File child : files) {
|
|
477
|
+ deleteRecursive(child);
|
|
478
|
+ }
|
425
|
479
|
}
|
426
|
480
|
}
|
427
|
|
- fileOrDirectory.delete();
|
|
481
|
+ boolean result = fileOrDirectory.delete();
|
|
482
|
+ if (!result) {
|
|
483
|
+ throw new IOException("Failed to delete '" + fileOrDirectory + "'");
|
|
484
|
+ }
|
428
|
485
|
}
|
429
|
486
|
|
430
|
487
|
/**
|
431
|
488
|
* Make a folder
|
432
|
|
- * @param path Source path
|
433
|
|
- * @param callback JS context callback
|
|
489
|
+ * @param path Source path
|
|
490
|
+ * @param promise JS promise
|
434
|
491
|
*/
|
435
|
|
- static void mkdir(String path, Callback callback) {
|
|
492
|
+ static void mkdir(String path, Promise promise) {
|
436
|
493
|
File dest = new File(path);
|
437
|
494
|
if(dest.exists()) {
|
438
|
|
- callback.invoke("mkdir failed, folder already exists at " + path);
|
|
495
|
+ promise.reject("EEXIST", dest.isDirectory() ? "Folder" : "File" + " '" + path + "' already exists");
|
439
|
496
|
return;
|
440
|
497
|
}
|
441
|
|
- dest.mkdirs();
|
442
|
|
- callback.invoke();
|
|
498
|
+ try {
|
|
499
|
+ boolean result = dest.mkdirs();
|
|
500
|
+ if (!result) {
|
|
501
|
+ promise.reject("EUNSPECIFIED", "mkdir failed to create some or all directories in '" + path + "'");
|
|
502
|
+ return;
|
|
503
|
+ }
|
|
504
|
+ } catch (Exception e) {
|
|
505
|
+ promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
|
|
506
|
+ return;
|
|
507
|
+ }
|
|
508
|
+ promise.resolve(true);
|
443
|
509
|
}
|
444
|
510
|
|
445
|
511
|
/**
|
|
@@ -449,19 +515,22 @@ public class RNFetchBlobFS {
|
449
|
515
|
* @param callback JS context callback
|
450
|
516
|
*/
|
451
|
517
|
static void cp(String path, String dest, Callback callback) {
|
452
|
|
-
|
453
|
518
|
path = normalizePath(path);
|
454
|
519
|
InputStream in = null;
|
455
|
520
|
OutputStream out = null;
|
456
|
521
|
|
457
|
522
|
try {
|
458
|
|
-
|
459
|
523
|
if(!isPathExists(path)) {
|
460
|
|
- callback.invoke("source file at path`" + path + "` does not exist");
|
|
524
|
+ callback.invoke("Source file at path`" + path + "` does not exist");
|
461
|
525
|
return;
|
462
|
526
|
}
|
463
|
|
- if(!new File(dest).exists())
|
464
|
|
- new File(dest).createNewFile();
|
|
527
|
+ if(!new File(dest).exists()) {
|
|
528
|
+ boolean result = new File(dest).createNewFile();
|
|
529
|
+ if (!result) {
|
|
530
|
+ callback.invoke("Destination file at '" + dest + "' already exists");
|
|
531
|
+ return;
|
|
532
|
+ }
|
|
533
|
+ }
|
465
|
534
|
|
466
|
535
|
in = inputStreamFromPath(path);
|
467
|
536
|
out = new FileOutputStream(dest);
|
|
@@ -471,7 +540,6 @@ public class RNFetchBlobFS {
|
471
|
540
|
while ((len = in.read(buf)) > 0) {
|
472
|
541
|
out.write(buf, 0, len);
|
473
|
542
|
}
|
474
|
|
-
|
475
|
543
|
} catch (Exception err) {
|
476
|
544
|
callback.invoke(err.getLocalizedMessage());
|
477
|
545
|
} finally {
|
|
@@ -498,10 +566,19 @@ public class RNFetchBlobFS {
|
498
|
566
|
static void mv(String path, String dest, Callback callback) {
|
499
|
567
|
File src = new File(path);
|
500
|
568
|
if(!src.exists()) {
|
501
|
|
- callback.invoke("source file at path `" + path + "` does not exist");
|
|
569
|
+ callback.invoke("Source file at path `" + path + "` does not exist");
|
|
570
|
+ return;
|
|
571
|
+ }
|
|
572
|
+ try {
|
|
573
|
+ boolean result = src.renameTo(new File(dest));
|
|
574
|
+ if (!result) {
|
|
575
|
+ callback.invoke("mv failed for unknown reasons");
|
|
576
|
+ return;
|
|
577
|
+ }
|
|
578
|
+ } catch (Exception e) {
|
|
579
|
+ callback.invoke(e.getLocalizedMessage());
|
502
|
580
|
return;
|
503
|
581
|
}
|
504
|
|
- src.renameTo(new File(dest));
|
505
|
582
|
callback.invoke();
|
506
|
583
|
}
|
507
|
584
|
|
|
@@ -511,11 +588,10 @@ public class RNFetchBlobFS {
|
511
|
588
|
* @param callback JS context callback
|
512
|
589
|
*/
|
513
|
590
|
static void exists(String path, Callback callback) {
|
514
|
|
-
|
515
|
591
|
if(isAsset(path)) {
|
516
|
592
|
try {
|
517
|
593
|
String filename = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
|
518
|
|
- AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(filename);
|
|
594
|
+ com.RNFetchBlob.RNFetchBlob.RCTContext.getAssets().openFd(filename);
|
519
|
595
|
callback.invoke(true, false);
|
520
|
596
|
} catch (IOException e) {
|
521
|
597
|
callback.invoke(false, false);
|
|
@@ -534,48 +610,67 @@ public class RNFetchBlobFS {
|
534
|
610
|
* @param path Target folder
|
535
|
611
|
* @param callback JS context callback
|
536
|
612
|
*/
|
537
|
|
- static void ls(String path, Callback callback) {
|
538
|
|
- path = normalizePath(path);
|
539
|
|
- File src = new File(path);
|
540
|
|
- if (!src.exists() || !src.isDirectory()) {
|
541
|
|
- callback.invoke("failed to list path `" + path + "` for it is not exist or it is not a folder");
|
542
|
|
- return;
|
543
|
|
- }
|
544
|
|
- String[] files = new File(path).list();
|
545
|
|
- WritableArray arg = Arguments.createArray();
|
546
|
|
- for (String i : files) {
|
547
|
|
- arg.pushString(i);
|
|
613
|
+ static void ls(String path, Promise promise) {
|
|
614
|
+ try {
|
|
615
|
+ path = normalizePath(path);
|
|
616
|
+ File src = new File(path);
|
|
617
|
+ if (!src.exists()) {
|
|
618
|
+ promise.reject("ENOENT", "No such file '" + path + "'");
|
|
619
|
+ return;
|
|
620
|
+ }
|
|
621
|
+ if (!src.isDirectory()) {
|
|
622
|
+ promise.reject("ENOTDIR", "Not a directory '" + path + "'");
|
|
623
|
+ return;
|
|
624
|
+ }
|
|
625
|
+ String[] files = new File(path).list();
|
|
626
|
+ WritableArray arg = Arguments.createArray();
|
|
627
|
+ // File => list(): "If this abstract pathname does not denote a directory, then this method returns null."
|
|
628
|
+ // We excluded that possibility above - ignore the "can produce NullPointerException" warning of the IDE.
|
|
629
|
+ for (String i : files) {
|
|
630
|
+ arg.pushString(i);
|
|
631
|
+ }
|
|
632
|
+ promise.resolve(arg);
|
|
633
|
+ } catch (Exception e) {
|
|
634
|
+ e.printStackTrace();
|
|
635
|
+ promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
|
548
|
636
|
}
|
549
|
|
- callback.invoke(null, arg);
|
550
|
637
|
}
|
551
|
638
|
|
552
|
639
|
/**
|
553
|
640
|
* Create a file by slicing given file path
|
554
|
|
- * @param src Source file path
|
|
641
|
+ * @param path Source file path
|
555
|
642
|
* @param dest Destination of created file
|
556
|
643
|
* @param start Start byte offset in source file
|
557
|
644
|
* @param end End byte offset
|
558
|
645
|
* @param encode NOT IMPLEMENTED
|
559
|
646
|
*/
|
560
|
|
- public static void slice(String src, String dest, int start, int end, String encode, Promise promise) {
|
|
647
|
+ static void slice(String path, String dest, int start, int end, String encode, Promise promise) {
|
561
|
648
|
try {
|
562
|
|
- src = normalizePath(src);
|
563
|
|
- File source = new File(src);
|
564
|
|
- if(!source.exists()) {
|
565
|
|
- promise.reject("RNFetchBlob slice error", "source file : " + src + " does not exist");
|
|
649
|
+ path = normalizePath(path);
|
|
650
|
+ File source = new File(path);
|
|
651
|
+ if(source.isDirectory()){
|
|
652
|
+ promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
|
|
653
|
+ return;
|
|
654
|
+ }
|
|
655
|
+ if(!source.exists()){
|
|
656
|
+ promise.reject("ENOENT", "No such file '" + path + "'");
|
566
|
657
|
return;
|
567
|
658
|
}
|
568
|
|
- long size = source.length();
|
569
|
|
- long max = Math.min(size, end);
|
570
|
|
- long expected = max - start;
|
571
|
|
- long now = 0;
|
572
|
|
- FileInputStream in = new FileInputStream(new File(src));
|
|
659
|
+ int size = (int) source.length();
|
|
660
|
+ int max = Math.min(size, end);
|
|
661
|
+ int expected = max - start;
|
|
662
|
+ int now = 0;
|
|
663
|
+ FileInputStream in = new FileInputStream(new File(path));
|
573
|
664
|
FileOutputStream out = new FileOutputStream(new File(dest));
|
574
|
|
- in.skip(start);
|
575
|
|
- byte [] buffer = new byte[10240];
|
|
665
|
+ int skipped = (int) in.skip(start);
|
|
666
|
+ if (skipped != start) {
|
|
667
|
+ promise.reject("EUNSPECIFIED", "Skipped " + skipped + " instead of the specified " + start + " bytes, size is " + size);
|
|
668
|
+ return;
|
|
669
|
+ }
|
|
670
|
+ byte[] buffer = new byte[10240];
|
576
|
671
|
while(now < expected) {
|
577
|
|
- long read = in.read(buffer, 0, 10240);
|
578
|
|
- long remain = expected - now;
|
|
672
|
+ int read = in.read(buffer, 0, 10240);
|
|
673
|
+ int remain = expected - now;
|
579
|
674
|
if(read <= 0) {
|
580
|
675
|
break;
|
581
|
676
|
}
|
|
@@ -588,7 +683,7 @@ public class RNFetchBlobFS {
|
588
|
683
|
promise.resolve(dest);
|
589
|
684
|
} catch (Exception e) {
|
590
|
685
|
e.printStackTrace();
|
591
|
|
- promise.reject("RNFetchBlob slice error", e.getLocalizedMessage());
|
|
686
|
+ promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
|
592
|
687
|
}
|
593
|
688
|
}
|
594
|
689
|
|
|
@@ -610,6 +705,8 @@ public class RNFetchBlobFS {
|
610
|
705
|
}
|
611
|
706
|
if(src.isDirectory()) {
|
612
|
707
|
String [] files = src.list();
|
|
708
|
+ // File => list(): "If this abstract pathname does not denote a directory, then this method returns null."
|
|
709
|
+ // We excluded that possibility above - ignore the "can produce NullPointerException" warning of the IDE.
|
613
|
710
|
for(String p : files) {
|
614
|
711
|
res.pushMap(statFile(src.getPath() + "/" + p));
|
615
|
712
|
}
|
|
@@ -625,8 +722,8 @@ public class RNFetchBlobFS {
|
625
|
722
|
|
626
|
723
|
/**
|
627
|
724
|
* show status of a file or directory
|
628
|
|
- * @param path
|
629
|
|
- * @param callback
|
|
725
|
+ * @param path Path
|
|
726
|
+ * @param callback Callback
|
630
|
727
|
*/
|
631
|
728
|
static void stat(String path, Callback callback) {
|
632
|
729
|
try {
|
|
@@ -643,8 +740,8 @@ public class RNFetchBlobFS {
|
643
|
740
|
|
644
|
741
|
/**
|
645
|
742
|
* Basic stat method
|
646
|
|
- * @param path
|
647
|
|
- * @return Stat result of a file or path
|
|
743
|
+ * @param path Path
|
|
744
|
+ * @return Stat Result of a file or path
|
648
|
745
|
*/
|
649
|
746
|
static WritableMap statFile(String path) {
|
650
|
747
|
try {
|
|
@@ -680,9 +777,9 @@ public class RNFetchBlobFS {
|
680
|
777
|
|
681
|
778
|
/**
|
682
|
779
|
* Media scanner scan file
|
683
|
|
- * @param path
|
684
|
|
- * @param mimes
|
685
|
|
- * @param callback
|
|
780
|
+ * @param path Path to file
|
|
781
|
+ * @param mimes Array of MIME type strings
|
|
782
|
+ * @param callback Callback for results
|
686
|
783
|
*/
|
687
|
784
|
void scanFile(String [] path, String[] mimes, final Callback callback) {
|
688
|
785
|
try {
|
|
@@ -708,17 +805,20 @@ public class RNFetchBlobFS {
|
708
|
805
|
algorithms.put("sha384", "SHA-384");
|
709
|
806
|
algorithms.put("sha512", "SHA-512");
|
710
|
807
|
|
711
|
|
- if (!algorithms.containsKey(algorithm)) throw new Exception("Invalid hash algorithm");
|
|
808
|
+ if (!algorithms.containsKey(algorithm)) {
|
|
809
|
+ promise.reject("EINVAL", "Invalid algorithm '" + algorithm + "', must be one of md5, sha1, sha224, sha256, sha384, sha512");
|
|
810
|
+ return;
|
|
811
|
+ }
|
712
|
812
|
|
713
|
813
|
File file = new File(path);
|
714
|
814
|
|
715
|
815
|
if (file.isDirectory()) {
|
716
|
|
- promise.reject("hash error", "EISDIR: illegal operation on a directory, read");
|
|
816
|
+ promise.reject("EISDIR", "Expecting a file but '" + path + "' is a directory");
|
717
|
817
|
return;
|
718
|
818
|
}
|
719
|
819
|
|
720
|
820
|
if (!file.exists()) {
|
721
|
|
- promise.reject("hash error", "ENOENT: no such file or directory, open '" + path + "'");
|
|
821
|
+ promise.reject("ENOENT", "No such file '" + path + "'");
|
722
|
822
|
return;
|
723
|
823
|
}
|
724
|
824
|
|
|
@@ -737,9 +837,9 @@ public class RNFetchBlobFS {
|
737
|
837
|
hexString.append(String.format("%02x", digestByte));
|
738
|
838
|
|
739
|
839
|
promise.resolve(hexString.toString());
|
740
|
|
- } catch (Exception ex) {
|
741
|
|
- ex.printStackTrace();
|
742
|
|
- promise.reject("hash error", ex.getLocalizedMessage());
|
|
840
|
+ } catch (Exception e) {
|
|
841
|
+ e.printStackTrace();
|
|
842
|
+ promise.reject("EUNSPECIFIED", e.getLocalizedMessage());
|
743
|
843
|
}
|
744
|
844
|
}
|
745
|
845
|
|
|
@@ -748,9 +848,9 @@ public class RNFetchBlobFS {
|
748
|
848
|
* @param path The destination path of the new file.
|
749
|
849
|
* @param data Initial data of the new file.
|
750
|
850
|
* @param encoding Encoding of initial data.
|
751
|
|
- * @param callback RCT bridge callback.
|
|
851
|
+ * @param promise Promise for Javascript
|
752
|
852
|
*/
|
753
|
|
- static void createFile(String path, String data, String encoding, Callback callback) {
|
|
853
|
+ static void createFile(String path, String data, String encoding, Promise promise) {
|
754
|
854
|
try {
|
755
|
855
|
File dest = new File(path);
|
756
|
856
|
boolean created = dest.createNewFile();
|
|
@@ -758,7 +858,7 @@ public class RNFetchBlobFS {
|
758
|
858
|
String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
|
759
|
859
|
File src = new File(orgPath);
|
760
|
860
|
if(!src.exists()) {
|
761
|
|
- callback.invoke("source file : " + data + " does not exist");
|
|
861
|
+ promise.reject("ENOENT", "Source file : " + data + " does not exist");
|
762
|
862
|
return ;
|
763
|
863
|
}
|
764
|
864
|
FileInputStream fin = new FileInputStream(src);
|
|
@@ -773,17 +873,15 @@ public class RNFetchBlobFS {
|
773
|
873
|
ostream.close();
|
774
|
874
|
} else {
|
775
|
875
|
if (!created) {
|
776
|
|
- callback.invoke("failed to create new file at path `" + path + "` because its parent path " +
|
777
|
|
- "may not exist, or the file already exists. If you intended to overwrite the " +
|
778
|
|
- "existing file use fs.writeFile instead.");
|
|
876
|
+ promise.reject("EEXIST", "File `" + path + "` already exists");
|
779
|
877
|
return;
|
780
|
878
|
}
|
781
|
879
|
OutputStream ostream = new FileOutputStream(dest);
|
782
|
880
|
ostream.write(RNFetchBlobFS.stringToBytes(data, encoding));
|
783
|
881
|
}
|
784
|
|
- callback.invoke(null, path);
|
|
882
|
+ promise.resolve(path);
|
785
|
883
|
} catch(Exception err) {
|
786
|
|
- callback.invoke(err.getLocalizedMessage());
|
|
884
|
+ promise.reject("EUNSPECIFIED", err.getLocalizedMessage());
|
787
|
885
|
}
|
788
|
886
|
}
|
789
|
887
|
|
|
@@ -791,30 +889,25 @@ public class RNFetchBlobFS {
|
791
|
889
|
* Create file for ASCII encoding
|
792
|
890
|
* @param path Path of new file.
|
793
|
891
|
* @param data Content of new file
|
794
|
|
- * @param callback JS context callback
|
|
892
|
+ * @param promise JS Promise
|
795
|
893
|
*/
|
796
|
|
- static void createFileASCII(String path, ReadableArray data, Callback callback) {
|
|
894
|
+ static void createFileASCII(String path, ReadableArray data, Promise promise) {
|
797
|
895
|
try {
|
798
|
896
|
File dest = new File(path);
|
799
|
|
- if(dest.exists()) {
|
800
|
|
- callback.invoke("failed to create new file at path `" + path + "`, file already exists.");
|
801
|
|
- return;
|
802
|
|
- }
|
803
|
897
|
boolean created = dest.createNewFile();
|
804
|
898
|
if(!created) {
|
805
|
|
- callback.invoke("failed to create new file at path `" + path + "` because its parent path may not exist");
|
|
899
|
+ promise.reject("EEXIST", "File at path `" + path + "` already exists");
|
806
|
900
|
return;
|
807
|
901
|
}
|
808
|
902
|
OutputStream ostream = new FileOutputStream(dest);
|
809
|
|
- byte [] chunk = new byte[data.size()];
|
810
|
|
- for(int i =0; i<data.size();i++) {
|
|
903
|
+ byte[] chunk = new byte[data.size()];
|
|
904
|
+ for(int i=0; i<data.size(); i++) {
|
811
|
905
|
chunk[i] = (byte) data.getInt(i);
|
812
|
906
|
}
|
813
|
907
|
ostream.write(chunk);
|
814
|
|
- chunk = null;
|
815
|
|
- callback.invoke(null, path);
|
|
908
|
+ promise.resolve(path);
|
816
|
909
|
} catch(Exception err) {
|
817
|
|
- callback.invoke(err.getLocalizedMessage());
|
|
910
|
+ promise.reject("EUNSPECIFIED", err.getLocalizedMessage());
|
818
|
911
|
}
|
819
|
912
|
}
|
820
|
913
|
|
|
@@ -838,17 +931,31 @@ public class RNFetchBlobFS {
|
838
|
931
|
* @param callback JS contest callback
|
839
|
932
|
*/
|
840
|
933
|
static void removeSession(ReadableArray paths, final Callback callback) {
|
841
|
|
-
|
842
|
934
|
AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
|
843
|
935
|
@Override
|
844
|
936
|
protected Integer doInBackground(ReadableArray ...paths) {
|
845
|
937
|
try {
|
|
938
|
+ ArrayList<String> failuresToDelete = new ArrayList<>();
|
846
|
939
|
for (int i = 0; i < paths[0].size(); i++) {
|
847
|
|
- File f = new File(paths[0].getString(i));
|
848
|
|
- if (f.exists())
|
849
|
|
- f.delete();
|
|
940
|
+ String fileName = paths[0].getString(i);
|
|
941
|
+ File f = new File(fileName);
|
|
942
|
+ if (f.exists()) {
|
|
943
|
+ boolean result = f.delete();
|
|
944
|
+ if (!result) {
|
|
945
|
+ failuresToDelete.add(fileName);
|
|
946
|
+ }
|
|
947
|
+ }
|
|
948
|
+ }
|
|
949
|
+ if (failuresToDelete.isEmpty()) {
|
|
950
|
+ callback.invoke(null, true);
|
|
951
|
+ } else {
|
|
952
|
+ StringBuilder listString = new StringBuilder();
|
|
953
|
+ listString.append("Failed to delete: ");
|
|
954
|
+ for (String s : failuresToDelete) {
|
|
955
|
+ listString.append(s).append(", ");
|
|
956
|
+ }
|
|
957
|
+ callback.invoke(listString.toString());
|
850
|
958
|
}
|
851
|
|
- callback.invoke(null, true);
|
852
|
959
|
} catch(Exception err) {
|
853
|
960
|
callback.invoke(err.getLocalizedMessage());
|
854
|
961
|
}
|
|
@@ -891,6 +998,7 @@ public class RNFetchBlobFS {
|
891
|
998
|
this.emitter.emit(streamName, eventData);
|
892
|
999
|
}
|
893
|
1000
|
|
|
1001
|
+ // "event" always is "data"...
|
894
|
1002
|
private void emitStreamEvent(String streamName, String event, WritableArray data) {
|
895
|
1003
|
WritableMap eventData = Arguments.createMap();
|
896
|
1004
|
eventData.putString("event", event);
|
|
@@ -898,12 +1006,13 @@ public class RNFetchBlobFS {
|
898
|
1006
|
this.emitter.emit(streamName, eventData);
|
899
|
1007
|
}
|
900
|
1008
|
|
901
|
|
- // TODO : should we remove this ?
|
902
|
|
- void emitFSData(String taskId, String event, String data) {
|
|
1009
|
+ // "event" always is "error"...
|
|
1010
|
+ private void emitStreamEvent(String streamName, String event, String code, String message) {
|
903
|
1011
|
WritableMap eventData = Arguments.createMap();
|
904
|
1012
|
eventData.putString("event", event);
|
905
|
|
- eventData.putString("detail", data);
|
906
|
|
- this.emitter.emit("RNFetchBlobStream" + taskId, eventData);
|
|
1013
|
+ eventData.putString("code", code);
|
|
1014
|
+ eventData.putString("detail", message);
|
|
1015
|
+ this.emitter.emit(streamName, eventData);
|
907
|
1016
|
}
|
908
|
1017
|
|
909
|
1018
|
/**
|
|
@@ -911,9 +1020,9 @@ public class RNFetchBlobFS {
|
911
|
1020
|
* the stream is created by Assets Manager, otherwise use FileInputStream.
|
912
|
1021
|
* @param path The file to open stream
|
913
|
1022
|
* @return InputStream instance
|
914
|
|
- * @throws IOException
|
|
1023
|
+ * @throws IOException If the given file does not exist or is a directory FileInputStream will throw a FileNotFoundException
|
915
|
1024
|
*/
|
916
|
|
- static InputStream inputStreamFromPath(String path) throws IOException {
|
|
1025
|
+ private static InputStream inputStreamFromPath(String path) throws IOException {
|
917
|
1026
|
if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
|
918
|
1027
|
return RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
|
919
|
1028
|
}
|
|
@@ -925,10 +1034,10 @@ public class RNFetchBlobFS {
|
925
|
1034
|
* @param path A file path URI string
|
926
|
1035
|
* @return A boolean value represents if the path exists.
|
927
|
1036
|
*/
|
928
|
|
- static boolean isPathExists(String path) {
|
|
1037
|
+ private static boolean isPathExists(String path) {
|
929
|
1038
|
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
|
930
|
1039
|
try {
|
931
|
|
- RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
|
|
1040
|
+ RNFetchBlob.RCTContext.getAssets().open(path.replace(com.RNFetchBlob.RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
|
932
|
1041
|
} catch (IOException e) {
|
933
|
1042
|
return false;
|
934
|
1043
|
}
|
|
@@ -941,9 +1050,7 @@ public class RNFetchBlobFS {
|
941
|
1050
|
}
|
942
|
1051
|
|
943
|
1052
|
static boolean isAsset(String path) {
|
944
|
|
- if(path != null)
|
945
|
|
- return path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET);
|
946
|
|
- return false;
|
|
1053
|
+ return path != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET);
|
947
|
1054
|
}
|
948
|
1055
|
|
949
|
1056
|
/**
|