Bladeren bron

Add Android increment HTTP support

Ben Hsieh 8 jaren geleden
bovenliggende
commit
810833049b

+ 2
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java Bestand weergeven

@@ -19,6 +19,7 @@ public class RNFetchBlobConfig {
19 19
     public String mime;
20 20
     public Boolean auto;
21 21
     public long timeout = 60000;
22
+    public Boolean increment = false;
22 23
     public ReadableArray binaryContentTypes = null;
23 24
 
24 25
     RNFetchBlobConfig(ReadableMap options) {
@@ -35,6 +36,7 @@ public class RNFetchBlobConfig {
35 36
             this.binaryContentTypes = options.getArray("binaryContentTypes");
36 37
         this.key = options.hasKey("key") ? options.getString("key") : null;
37 38
         this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
39
+        this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;
38 40
         this.auto = options.hasKey("auto") ? options.getBoolean("auto") : false;
39 41
         if(options.hasKey("timeout")) {
40 42
             this.timeout = options.getInt("timeout");

+ 4
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Bestand weergeven

@@ -292,7 +292,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
292 292
                                 extended = new RNFetchBlobDefaultResp(
293 293
                                         RNFetchBlob.RCTContext,
294 294
                                         taskId,
295
-                                        originalResponse.body());
295
+                                        originalResponse.body(),
296
+                                        options.increment);
296 297
                                 break;
297 298
                             case FileStorage:
298 299
                                 extended = new RNFetchBlobFileResp(
@@ -305,7 +306,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
305 306
                                 extended = new RNFetchBlobDefaultResp(
306 307
                                         RNFetchBlob.RCTContext,
307 308
                                         taskId,
308
-                                        originalResponse.body());
309
+                                        originalResponse.body(),
310
+                                        options.increment);
309 311
                                 break;
310 312
                         }
311 313
                         return originalResponse.newBuilder().body(extended).build();

+ 11
- 1
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobDefaultResp.java Bestand weergeven

@@ -9,6 +9,7 @@ import com.facebook.react.bridge.WritableMap;
9 9
 import com.facebook.react.modules.core.DeviceEventManagerModule;
10 10
 
11 11
 import java.io.IOException;
12
+import java.nio.charset.Charset;
12 13
 
13 14
 import okhttp3.Call;
14 15
 import okhttp3.Callback;
@@ -30,11 +31,13 @@ public class RNFetchBlobDefaultResp extends ResponseBody {
30 31
     String mTaskId;
31 32
     ReactApplicationContext rctContext;
32 33
     ResponseBody originalBody;
34
+    boolean isIncrement = false;
33 35
 
34
-    public RNFetchBlobDefaultResp(ReactApplicationContext ctx, String taskId, ResponseBody body) {
36
+    public RNFetchBlobDefaultResp(ReactApplicationContext ctx, String taskId, ResponseBody body, boolean isIncrement) {
35 37
         this.rctContext = ctx;
36 38
         this.mTaskId = taskId;
37 39
         this.originalBody = body;
40
+        this.isIncrement = isIncrement;
38 41
     }
39 42
 
40 43
     @Override
@@ -71,6 +74,13 @@ public class RNFetchBlobDefaultResp extends ResponseBody {
71 74
                 args.putString("taskId", mTaskId);
72 75
                 args.putString("written", String.valueOf(bytesRead));
73 76
                 args.putString("total", String.valueOf(contentLength()));
77
+                if(isIncrement) {
78
+                    args.putString("chunk", sink.readString(Charset.defaultCharset()));
79
+                }
80
+                else {
81
+                    args.putString("chunk", "");
82
+                }
83
+
74 84
                 rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
75 85
                         .emit(RNFetchBlobConst.EVENT_PROGRESS, args);
76 86
             }