Browse Source

Add Android increment HTTP support

Ben Hsieh 8 years ago
parent
commit
810833049b

+ 2
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java View File

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

+ 4
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

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

+ 11
- 1
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobDefaultResp.java View File

9
 import com.facebook.react.modules.core.DeviceEventManagerModule;
9
 import com.facebook.react.modules.core.DeviceEventManagerModule;
10
 
10
 
11
 import java.io.IOException;
11
 import java.io.IOException;
12
+import java.nio.charset.Charset;
12
 
13
 
13
 import okhttp3.Call;
14
 import okhttp3.Call;
14
 import okhttp3.Callback;
15
 import okhttp3.Callback;
30
     String mTaskId;
31
     String mTaskId;
31
     ReactApplicationContext rctContext;
32
     ReactApplicationContext rctContext;
32
     ResponseBody originalBody;
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
         this.rctContext = ctx;
37
         this.rctContext = ctx;
36
         this.mTaskId = taskId;
38
         this.mTaskId = taskId;
37
         this.originalBody = body;
39
         this.originalBody = body;
40
+        this.isIncrement = isIncrement;
38
     }
41
     }
39
 
42
 
40
     @Override
43
     @Override
71
                 args.putString("taskId", mTaskId);
74
                 args.putString("taskId", mTaskId);
72
                 args.putString("written", String.valueOf(bytesRead));
75
                 args.putString("written", String.valueOf(bytesRead));
73
                 args.putString("total", String.valueOf(contentLength()));
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
                 rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
84
                 rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
75
                         .emit(RNFetchBlobConst.EVENT_PROGRESS, args);
85
                         .emit(RNFetchBlobConst.EVENT_PROGRESS, args);
76
             }
86
             }