/
FileSystemModule.java
1084 lines (980 loc) · 38.7 KB
/
FileSystemModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package expo.modules.filesystem;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Base64;
import android.util.Log;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.unimodules.core.ExportedModule;
import org.unimodules.core.ModuleRegistry;
import org.unimodules.core.Promise;
import org.unimodules.core.interfaces.ActivityProvider;
import org.unimodules.core.interfaces.ExpoMethod;
import org.unimodules.core.interfaces.services.EventEmitter;
import org.unimodules.interfaces.filesystem.FilePermissionModuleInterface;
import org.unimodules.interfaces.filesystem.Permission;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.net.CookieHandler;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import androidx.annotation.Nullable;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.JavaNetCookieJar;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;
public class FileSystemModule extends ExportedModule {
private static final String NAME = "ExponentFileSystem";
private static final String TAG = FileSystemModule.class.getSimpleName();
private static final String EXDownloadProgressEventName = "Exponent.downloadProgress";
private static final long MIN_EVENT_DT_MS = 100;
private static final String HEADER_KEY = "headers";
private ModuleRegistry mModuleRegistry;
private OkHttpClient mClient;
private final Map<String, DownloadResumable> mDownloadResumableMap = new HashMap<>();
private enum UploadType {
INVALID(-1),
BINARY_CONTENT(0),
MULTIPART(1);
private int value;
UploadType(int value) {
this.value = value;
}
public static UploadType fromInteger(@Nullable Integer value) {
if (value == null) {
return INVALID;
}
for (UploadType method : values()) {
if (value.equals(method.value)) {
return method;
}
}
return INVALID;
}
}
public FileSystemModule(Context context) {
super(context);
try {
ensureDirExists(getContext().getFilesDir());
ensureDirExists(getContext().getCacheDir());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onCreate(ModuleRegistry moduleRegistry) {
mModuleRegistry = moduleRegistry;
}
@Override
public String getName() {
return NAME;
}
@Override
public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
constants.put("documentDirectory", Uri.fromFile(getContext().getFilesDir()).toString() + "/");
constants.put("cacheDirectory", Uri.fromFile(getContext().getCacheDir()).toString() + "/");
constants.put("bundleDirectory", "asset:///");
return constants;
}
private File uriToFile(Uri uri) {
return new File(uri.getPath());
}
private void checkIfFileExists(Uri uri) throws IOException {
File file = uriToFile(uri);
if (!file.exists()) {
throw new IOException("Directory for " + file.getPath() + " doesn't exist.");
}
}
private void checkIfFileDirExists(Uri uri) throws IOException {
File file = uriToFile(uri);
File dir = file.getParentFile();
if (!dir.exists()) {
throw new IOException("Directory for " + file.getPath() + " doesn't exist. Please make sure directory '" + file.getParent() + "' exists before calling downloadAsync.");
}
}
private EnumSet<Permission> permissionsForPath(String path) {
return mModuleRegistry.getModule(FilePermissionModuleInterface.class).getPathPermissions(getContext(), path);
}
private EnumSet<Permission> permissionsForUri(Uri uri) {
if ("content".equals(uri.getScheme())) {
return EnumSet.of(Permission.READ);
}
if ("asset".equals(uri.getScheme())) {
return EnumSet.of(Permission.READ);
}
if ("file".equals(uri.getScheme())) {
return permissionsForPath(uri.getPath());
}
if (uri.getScheme() == null) {
// this is probably an asset embedded by the packager in resources
return EnumSet.of(Permission.READ);
}
return EnumSet.noneOf(Permission.class);
}
// For now we only need to ensure one permission at a time, this allows easier error message strings,
// we can generalize this when needed later
private void ensurePermission(Uri uri, Permission permission, String errorMsg) throws IOException {
if (!permissionsForUri(uri).contains(permission)) {
throw new IOException(errorMsg);
}
}
private void ensurePermission(Uri uri, Permission permission) throws IOException {
if (permission.equals(Permission.READ)) {
ensurePermission(uri, permission, "Location '" + uri + "' isn't readable.");
}
if (permission.equals(Permission.WRITE)) {
ensurePermission(uri, permission, "Location '" + uri + "' isn't writable.");
}
ensurePermission(uri, permission, "Location '" + uri + "' doesn't have permission '" + permission.name() + "'.");
}
private InputStream openAssetInputStream(Uri uri) throws IOException {
// AssetManager expects no leading slash.
String asset = uri.getPath().substring(1);
return getContext().getAssets().open(asset);
}
private InputStream openResourceInputStream(String resourceName) throws IOException {
int resourceId = getContext().getResources().getIdentifier(resourceName, "raw", getContext().getPackageName());
if (resourceId == 0) {
// this resource doesn't exist in the raw folder, so try drawable
resourceId = getContext().getResources().getIdentifier(resourceName, "drawable", getContext().getPackageName());
if (resourceId == 0) {
throw new FileNotFoundException("No resource found with the name " + resourceName);
}
}
return getContext().getResources().openRawResource(resourceId);
}
@ExpoMethod
public void getInfoAsync(String uriStr, Map<String, Object> options, Promise promise) {
try {
Uri uri = Uri.parse(uriStr);
ensurePermission(uri, Permission.READ);
if ("file".equals(uri.getScheme())) {
File file = uriToFile(uri);
Bundle result = new Bundle();
if (file.exists()) {
result.putBoolean("exists", true);
result.putBoolean("isDirectory", file.isDirectory());
result.putString("uri", Uri.fromFile(file).toString());
if (options.containsKey("md5") && (Boolean) options.get("md5")) {
result.putString("md5", md5(file));
}
result.putDouble("size", file.length());
result.putDouble("modificationTime", 0.001 * file.lastModified());
promise.resolve(result);
} else {
result.putBoolean("exists", false);
result.putBoolean("isDirectory", false);
promise.resolve(result);
}
} else if ("content".equals(uri.getScheme()) || "asset".equals(uri.getScheme()) || uri.getScheme() == null) {
Bundle result = new Bundle();
try {
InputStream is;
if ("content".equals(uri.getScheme())) {
is = getContext().getContentResolver().openInputStream(uri);
} else if ("asset".equals(uri.getScheme())) {
is = openAssetInputStream(uri);
} else {
is = openResourceInputStream(uriStr);
}
if (is == null) {
throw new FileNotFoundException();
}
result.putBoolean("exists", true);
result.putBoolean("isDirectory", false);
result.putString("uri", uri.toString());
// NOTE: `.available()` is supposedly not a reliable source of size info, but it's been
// more reliable than querying `OpenableColumns.SIZE` in practice in tests ¯\_(ツ)_/¯
result.putDouble("size", is.available());
if (options.containsKey("md5") && (Boolean) options.get("md5")) {
byte[] md5bytes = DigestUtils.md5(is);
result.putString("md5", String.valueOf(Hex.encodeHex(md5bytes)));
}
promise.resolve(result);
} catch (FileNotFoundException e) {
result.putBoolean("exists", false);
result.putBoolean("isDirectory", false);
promise.resolve(result);
}
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void readAsStringAsync(String uriStr, Map<String, Object> options, Promise promise) {
try {
Uri uri = Uri.parse(uriStr);
ensurePermission(uri, Permission.READ);
// TODO:Bacon: Add more encoding types to match iOS
String encoding = "utf8";
if (options.containsKey("encoding") && options.get("encoding") instanceof String) {
encoding = ((String) options.get("encoding")).toLowerCase();
}
String contents;
if (encoding.equalsIgnoreCase("base64")) {
InputStream inputStream;
if ("file".equals(uri.getScheme())) {
inputStream = new FileInputStream(uriToFile(uri));
} else if ("asset".equals(uri.getScheme())) {
inputStream = openAssetInputStream(uri);
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
if (options.containsKey("length") && options.containsKey("position")) {
int length = ((Number) options.get("length")).intValue();
int position = ((Number) options.get("position")).intValue();
byte[] buffer = new byte[length];
inputStream.skip(position);
int bytesRead = inputStream.read(buffer, 0, length);
contents = Base64.encodeToString(buffer, 0, bytesRead, Base64.NO_WRAP);
} else {
byte[] inputData = getInputStreamBytes(inputStream);
contents = Base64.encodeToString(inputData, Base64.NO_WRAP);
}
} else {
if ("file".equals(uri.getScheme())) {
contents = IOUtils.toString(new FileInputStream(uriToFile(uri)));
} else if ("asset".equals(uri.getScheme())) {
contents = IOUtils.toString(openAssetInputStream(uri));
} else if (uri.getScheme() == null) {
// this is probably an asset embedded by the packager in resources
contents = IOUtils.toString(openResourceInputStream(uriStr));
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
}
promise.resolve(contents);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void writeAsStringAsync(String uriStr, String string, Map<String, Object> options, Promise promise) {
try {
Uri uri = Uri.parse(uriStr);
ensurePermission(uri, Permission.WRITE);
if ("file".equals(uri.getScheme())) {
String encoding = "utf8";
if (options.containsKey("encoding") && options.get("encoding") instanceof String) {
encoding = ((String) options.get("encoding")).toLowerCase();
}
FileOutputStream out = new FileOutputStream(uriToFile(uri));
if (encoding.equals("base64")) {
byte[] bytes = Base64.decode(string, Base64.DEFAULT);
out.write(bytes);
} else {
OutputStreamWriter writer = new OutputStreamWriter(out);
writer.write(string);
writer.close();
}
out.close();
promise.resolve(null);
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void deleteAsync(String uriStr, Map<String, Object> options, Promise promise) {
try {
Uri uri = Uri.parse(uriStr);
Uri appendedUri = Uri.withAppendedPath(uri, "..");
ensurePermission(appendedUri, Permission.WRITE, "Location '" + uri + "' isn't deletable.");
if ("file".equals(uri.getScheme())) {
File file = uriToFile(uri);
if (file.exists()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
FileUtils.forceDelete(file);
} else {
// to be removed once Android SDK 25 support is dropped
forceDelete(file);
}
promise.resolve(null);
} else {
if (options.containsKey("idempotent") && (Boolean) options.get("idempotent")) {
promise.resolve(null);
} else {
promise.reject("ERR_FILESYSTEM_CANNOT_FIND_FILE",
"File '" + uri + "' could not be deleted because it could not be found");
}
}
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void moveAsync(Map<String, Object> options, Promise promise) {
try {
if (!options.containsKey("from")) {
promise.reject("ERR_FILESYSTEM_MISSING_PARAMETER", "`FileSystem.moveAsync` needs a `from` path.");
return;
}
Uri fromUri = Uri.parse((String) options.get("from"));
ensurePermission(Uri.withAppendedPath(fromUri, ".."), Permission.WRITE, "Location '" + fromUri + "' isn't movable.");
if (!options.containsKey("to")) {
promise.reject("ERR_FILESYSTEM_MISSING_PARAMETER", "`FileSystem.moveAsync` needs a `to` path.");
return;
}
Uri toUri = Uri.parse((String) options.get("to"));
ensurePermission(toUri, Permission.WRITE);
if ("file".equals(fromUri.getScheme())) {
File from = uriToFile(fromUri);
File to = uriToFile(toUri);
if (from.renameTo(to)) {
promise.resolve(null);
} else {
promise.reject("ERR_FILESYSTEM_CANNOT_MOVE_FILE",
"File '" + fromUri + "' could not be moved to '" + toUri + "'");
}
} else {
throw new IOException("Unsupported scheme for location '" + fromUri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void copyAsync(Map<String, Object> options, Promise promise) {
try {
if (!options.containsKey("from")) {
promise.reject("ERR_FILESYSTEM_MISSING_PARAMETER", "`FileSystem.moveAsync` needs a `from` path.");
return;
}
Uri fromUri = Uri.parse((String) options.get("from"));
ensurePermission(fromUri, Permission.READ);
if (!options.containsKey("to")) {
promise.reject("ERR_FILESYSTEM_MISSING_PARAMETER", "`FileSystem.moveAsync` needs a `to` path.");
return;
}
Uri toUri = Uri.parse((String) options.get("to"));
ensurePermission(toUri, Permission.WRITE);
if ("file".equals(fromUri.getScheme())) {
File from = uriToFile(fromUri);
File to = uriToFile(toUri);
if (from.isDirectory()) {
FileUtils.copyDirectory(from, to);
promise.resolve(null);
} else {
FileUtils.copyFile(from, to);
promise.resolve(null);
}
} else if ("content".equals(fromUri.getScheme())) {
InputStream in = getContext().getContentResolver().openInputStream(fromUri);
OutputStream out = new FileOutputStream(uriToFile(toUri));
IOUtils.copy(in, out);
promise.resolve(null);
} else if ("asset".equals(fromUri.getScheme())) {
InputStream in = openAssetInputStream(fromUri);
OutputStream out = new FileOutputStream(uriToFile(toUri));
IOUtils.copy(in, out);
promise.resolve(null);
} else if (fromUri.getScheme() == null) {
// this is probably an asset embedded by the packager in resources
InputStream in = openResourceInputStream((String) options.get("from"));
OutputStream out = new FileOutputStream(uriToFile(toUri));
IOUtils.copy(in, out);
promise.resolve(null);
} else {
throw new IOException("Unsupported scheme for location '" + fromUri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void makeDirectoryAsync(String uriStr, Map<String, Object> options, Promise promise) {
try {
Uri uri = Uri.parse(uriStr);
ensurePermission(uri, Permission.WRITE);
if ("file".equals(uri.getScheme())) {
File file = uriToFile(uri);
boolean previouslyCreated = file.isDirectory();
boolean setIntermediates = options.containsKey("intermediates") && (Boolean) options.get("intermediates");
boolean success = setIntermediates ? file.mkdirs() : file.mkdir();
if (success || (setIntermediates && previouslyCreated)) {
promise.resolve(null);
} else {
promise.reject("ERR_FILESYSTEM_CANNOT_CREATE_DIRECTORY",
"Directory '" + uri + "' could not be created or already exists.");
}
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void readDirectoryAsync(String uriStr, Map<String, Object> options, Promise promise) {
try {
Uri uri = Uri.parse(uriStr);
ensurePermission(uri, Permission.READ);
if ("file".equals(uri.getScheme())) {
File file = uriToFile(uri);
File[] children = file.listFiles();
if (children != null) {
List<String> result = new ArrayList<>();
for (File child : children) {
result.add(child.getName());
}
promise.resolve(result);
} else {
promise.reject("ERR_FILESYSTEM_CANNOT_READ_DIRECTORY",
"Directory '" + uri + "' could not be read.");
}
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void uploadAsync(final String fileUriString, final String url, final Map<String, Object> options, final Promise promise) {
try {
final Uri fileUri = Uri.parse(fileUriString);
ensurePermission(fileUri, Permission.READ);
checkIfFileExists(fileUri);
if (!options.containsKey("httpMethod")) {
promise.reject("ERR_FILESYSTEM_MISSING_HTTP_METHOD", "Missing HTTP method.", null);
return;
}
String method = (String) options.get("httpMethod");
if (!options.containsKey("uploadType")) {
promise.reject("ERR_FILESYSTEM_MISSING_UPLOAD_TYPE", "Missing upload type.", null);
return;
}
UploadType uploadType = UploadType.fromInteger((Integer) options.get("uploadType"));
Request.Builder requestBuilder = new Request.Builder().url(url);
if (options.containsKey(HEADER_KEY)) {
final Map<String, Object> headers = (Map<String, Object>) options.get(HEADER_KEY);
for (String key : headers.keySet()) {
requestBuilder.addHeader(key, headers.get(key).toString());
}
}
File file = uriToFile(fileUri);
if (uploadType == UploadType.BINARY_CONTENT) {
RequestBody body = RequestBody.create(null, file);
requestBuilder.method(method, body);
} else if (uploadType == UploadType.MULTIPART) {
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (options.containsKey("parameters")) {
Map<String, Object> parametersMap = (Map<String, Object>) options.get("parameters");
for (String key : parametersMap.keySet()) {
bodyBuilder.addFormDataPart(key, String.valueOf(parametersMap.get(key)));
}
}
String mimeType;
if (options.containsKey("mimeType")) {
mimeType = (String) options.get("mimeType");
} else {
mimeType = URLConnection.guessContentTypeFromName(file.getName());
}
String fieldName = file.getName();
if (options.containsKey("fieldName")) {
fieldName = (String) options.get("fieldName");
}
bodyBuilder.addFormDataPart(fieldName, file.getName(), RequestBody.create(mimeType != null ? MediaType.parse(mimeType) : null, file));
requestBuilder.method(method, bodyBuilder.build());
} else {
promise.reject("ERR_FILESYSTEM_INVALID_UPLOAD_TYPE", String.format("Invalid upload type: %s.", options.get("uploadType")), null);
return;
}
getOkHttpClient().newCall(requestBuilder.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, String.valueOf(e.getMessage()));
promise.reject(e);
}
@Override
public void onResponse(Call call, Response response) {
Bundle result = new Bundle();
try {
if (response.body() != null) {
result.putString("body", response.body().string());
} else {
result.putString("body", null);
}
} catch (IOException exception) {
promise.reject(exception);
return;
}
result.putInt("status", response.code());
result.putBundle("headers", translateHeaders(response.headers()));
response.close();
promise.resolve(result);
}
});
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void downloadAsync(String url, final String uriStr, final Map<String, Object> options, final Promise promise) {
try {
final Uri uri = Uri.parse(uriStr);
ensurePermission(uri, Permission.WRITE);
checkIfFileDirExists(uri);
if (!url.contains(":")) {
Context context = getContext();
Resources resources = context.getResources();
String packageName = context.getPackageName();
int resourceId = resources.getIdentifier(url, "raw", packageName);
BufferedSource bufferedSource = Okio.buffer(Okio.source(context.getResources().openRawResource(resourceId)));
File file = uriToFile(uri);
file.delete();
BufferedSink sink = Okio.buffer(Okio.sink(file));
sink.writeAll(bufferedSource);
sink.close();
Bundle result = new Bundle();
result.putString("uri", Uri.fromFile(file).toString());
if (options != null && options.containsKey("md5") && (Boolean) options.get("md5")) {
result.putString("md5", md5(file));
}
promise.resolve(result);
} else if ("file".equals(uri.getScheme())) {
Request.Builder requestBuilder = new Request.Builder().url(url);
if (options != null && options.containsKey(HEADER_KEY)) {
try {
final Map<String, Object> headers = (Map<String, Object>) options.get(HEADER_KEY);
for (String key : headers.keySet()) {
requestBuilder.addHeader(key, (String) headers.get(key));
}
} catch (ClassCastException exception) {
promise.reject("ERR_FILESYSTEM_INVALID_HEADERS", "Invalid headers dictionary. Keys and values should be strings.", exception);
return;
}
}
getOkHttpClient().newCall(requestBuilder.build()).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, String.valueOf(e.getMessage()));
promise.reject(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
File file = uriToFile(uri);
file.delete();
BufferedSink sink = Okio.buffer(Okio.sink(file));
sink.writeAll(response.body().source());
sink.close();
Bundle result = new Bundle();
result.putString("uri", Uri.fromFile(file).toString());
if (options != null && options.containsKey("md5") && (Boolean) options.get("md5")) {
result.putString("md5", md5(file));
}
result.putInt("status", response.code());
result.putBundle("headers", translateHeaders(response.headers()));
response.close();
promise.resolve(result);
}
});
} else {
throw new IOException("Unsupported scheme for location '" + uri + "'.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void getTotalDiskCapacityAsync(Promise promise) {
try {
StatFs root = new StatFs(Environment.getDataDirectory().getAbsolutePath());
long blockCount = root.getBlockCountLong();
long blockSize = root.getBlockSizeLong();
BigInteger capacity = BigInteger.valueOf(blockCount).multiply(BigInteger.valueOf(blockSize));
//cast down to avoid overflow
Double capacityDouble = Math.min(capacity.doubleValue(), Math.pow(2, 53) - 1);
promise.resolve(capacityDouble);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject("ERR_FILESYSTEM_CANNOT_DETERMINE_DISK_CAPACITY", "Unable to access total disk capacity", e);
}
}
@ExpoMethod
public void getFreeDiskStorageAsync(Promise promise) {
try {
StatFs external = new StatFs(Environment.getDataDirectory().getAbsolutePath());
long availableBlocks = external.getAvailableBlocksLong();
long blockSize = external.getBlockSizeLong();
BigInteger storage = BigInteger.valueOf(availableBlocks).multiply(BigInteger.valueOf(blockSize));
//cast down to avoid overflow
Double storageDouble = Math.min(storage.doubleValue(), Math.pow(2, 53) - 1);
promise.resolve(storageDouble);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject("ERR_FILESYSTEM_CANNOT_DETERMINE_DISK_CAPACITY", "Unable to determine free disk storage capacity", e);
}
}
@ExpoMethod
public void getContentUriAsync(String uri, Promise promise) {
try {
final Uri fileUri = Uri.parse(uri);
ensurePermission(fileUri, Permission.WRITE);
ensurePermission(fileUri, Permission.READ);
checkIfFileDirExists(fileUri);
if ("file".equals(fileUri.getScheme())) {
File file = uriToFile(fileUri);
promise.resolve(contentUriFromFile(file).toString());
} else {
promise.reject("ERR_FILESYSTEM_CANNOT_READ_DIRECTORY", "No readable files with the uri: " + uri + ". Please use other uri.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
private Uri contentUriFromFile(File file) {
try {
Application application = mModuleRegistry.getModule(ActivityProvider.class).getCurrentActivity().getApplication();
return FileSystemFileProvider.getUriForFile(application, application.getPackageName() + ".FileSystemFileProvider", file);
} catch (Exception e) {
throw e;
}
}
@ExpoMethod
public void downloadResumableStartAsync(String url, final String fileUriStr, final String uuid, final Map<String, Object> options, final String resumeData, final Promise promise) {
try {
final Uri fileUri = Uri.parse(fileUriStr);
checkIfFileDirExists(fileUri);
if (!("file".equals(fileUri.getScheme()))) {
throw new IOException("Unsupported scheme for location '" + fileUri + "'.");
}
final boolean isResume = resumeData != null;
final ProgressListener progressListener = new ProgressListener() {
long mLastUpdate = -1;
@Override
public void update(long bytesRead, long contentLength, boolean done) {
EventEmitter eventEmitter = mModuleRegistry.getModule(EventEmitter.class);
if (eventEmitter != null) {
Bundle downloadProgress = new Bundle();
Bundle downloadProgressData = new Bundle();
long totalBytesWritten = isResume ? bytesRead + Long.parseLong(resumeData) : bytesRead;
long totalBytesExpectedToWrite = isResume ? contentLength + Long.parseLong(resumeData) : contentLength;
long currentTime = System.currentTimeMillis();
// Throttle events. Sending too many events will block the JS event loop.
// Make sure to send the last event when we're at 100%.
if (currentTime > mLastUpdate + MIN_EVENT_DT_MS || totalBytesWritten == totalBytesExpectedToWrite) {
mLastUpdate = currentTime;
downloadProgressData.putDouble("totalBytesWritten", totalBytesWritten);
downloadProgressData.putDouble("totalBytesExpectedToWrite", totalBytesExpectedToWrite);
downloadProgress.putString("uuid", uuid);
downloadProgress.putBundle("data", downloadProgressData);
eventEmitter.emit(EXDownloadProgressEventName, downloadProgress);
}
}
}
};
OkHttpClient client =
getOkHttpClient().newBuilder()
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), progressListener))
.build();
}
})
.build();
Request.Builder requestBuilder = new Request.Builder();
if (isResume) {
requestBuilder.addHeader("Range", "bytes=" + resumeData + "-");
}
if (options != null && options.containsKey(HEADER_KEY)) {
final Map<String, Object> headers = (Map<String, Object>) options.get(HEADER_KEY);
for (String key : headers.keySet()) {
requestBuilder.addHeader(key, headers.get(key).toString());
}
}
Request request = requestBuilder.url(url).build();
Call call = client.newCall(request);
DownloadResumable downloadResumable = new DownloadResumable(uuid, url, fileUri, call);
this.mDownloadResumableMap.put(uuid, downloadResumable);
File file = uriToFile(fileUri);
DownloadResumableTaskParams params = new DownloadResumableTaskParams(options, call, file, isResume, promise);
DownloadResumableTask task = new DownloadResumableTask();
task.execute(params);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
@ExpoMethod
public void downloadResumablePauseAsync(final String uuid, final Promise promise) {
DownloadResumable downloadResumable = this.mDownloadResumableMap.get(uuid);
if (downloadResumable != null) {
downloadResumable.call.cancel();
this.mDownloadResumableMap.remove(downloadResumable.uuid);
try {
File file = uriToFile(downloadResumable.fileUri);
Bundle result = new Bundle();
result.putString("resumeData", String.valueOf(file.length()));
promise.resolve(result);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
}
} else {
Exception e = new IOException("No download object available");
Log.e(TAG, e.getMessage());
promise.reject(e);
}
}
private static byte[] getInputStreamBytes(InputStream inputStream) throws IOException {
byte[] bytesResult;
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
try {
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
bytesResult = byteBuffer.toByteArray();
} finally {
try {
byteBuffer.close();
} catch (IOException ignored) {
}
}
return bytesResult;
}
private static class DownloadResumableTaskParams {
Map<String, Object> options;
Call call;
File file;
boolean isResume;
Promise promise;
DownloadResumableTaskParams(Map<String, Object> options, Call call, File file, boolean isResume, Promise promise) {
this.options = options;
this.call = call;
this.file = file;
this.isResume = isResume;
this.promise = promise;
}
}
private class DownloadResumableTask extends AsyncTask<DownloadResumableTaskParams, Void, Void> {
@Override
protected Void doInBackground(DownloadResumableTaskParams... params) {
Call call = params[0].call;
Promise promise = params[0].promise;
File file = params[0].file;
boolean isResume = params[0].isResume;
Map<String, Object> options = params[0].options;
try {
Response response = call.execute();
ResponseBody responseBody = response.body();
BufferedInputStream input = new BufferedInputStream(responseBody.byteStream());
OutputStream output;
if (isResume) {
output = new FileOutputStream(file, true);
} else {
output = new FileOutputStream(file, false);
}
byte[] data = new byte[1024];
int count = 0;
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
Bundle result = new Bundle();
result.putString("uri", Uri.fromFile(file).toString());
if (options != null && options.containsKey("md5") && (Boolean) options.get("md5")) {
result.putString("md5", md5(file));
}
result.putInt("status", response.code());
result.putBundle("headers", translateHeaders(response.headers()));
response.close();
promise.resolve(result);
return null;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
promise.reject(e);
return null;
}
}
}
// Copied out of React Native's `NetworkingModule.java`
private static Bundle translateHeaders(Headers headers) {
Bundle responseHeaders = new Bundle();
for (int i = 0; i < headers.size(); i++) {
String headerName = headers.name(i);
// multiple values for the same header
if (responseHeaders.get(headerName) != null) {
responseHeaders.putString(
headerName,
responseHeaders.getString(headerName) + ", " + headers.value(i));
} else {
responseHeaders.putString(headerName, headers.value(i));
}
}
return responseHeaders;
}
private static class DownloadResumable {
public final String uuid;
public final String url;
public final Uri fileUri;
public final Call call;
public DownloadResumable(String uuid, String url, Uri fileUri, Call call) {
this.uuid = uuid;
this.url = url;
this.fileUri = fileUri;
this.call = call;
}
}
// https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/Progress.java
private static class ProgressResponseBody extends ResponseBody {
private final ResponseBody responseBody;
private final ProgressListener progressListener;
private BufferedSource bufferedSource;
ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {
this.responseBody = responseBody;
this.progressListener = progressListener;
}
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {