Ben Hsieh 8 years ago
parent
commit
ee0f38e380

+ 10
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

12
 
12
 
13
 public class RNFetchBlob extends ReactContextBaseJavaModule {
13
 public class RNFetchBlob extends ReactContextBaseJavaModule {
14
 
14
 
15
-    String filePathPrefix = "RNFetchBlob-file://";
16
     static ReactApplicationContext RCTContext;
15
     static ReactApplicationContext RCTContext;
17
 
16
 
18
     public RNFetchBlob(ReactApplicationContext reactContext) {
17
     public RNFetchBlob(ReactApplicationContext reactContext) {
165
         RNFetchBlobFS.slice(src, dest, start, end, encode, callback);
164
         RNFetchBlobFS.slice(src, dest, start, end, encode, callback);
166
     }
165
     }
167
 
166
 
167
+    @ReactMethod
168
+    public void enableProgressReport(String taskId) {
169
+        RNFetchBlobReq.progressReport.put(taskId, true);
170
+    }
171
+
172
+    @ReactMethod
173
+    public void enableUploadProgressReport(String taskId) {
174
+        RNFetchBlobReq.uploadProgressReport.put(taskId, true);
175
+    }
176
+
168
     @ReactMethod
177
     @ReactMethod
169
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
178
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
170
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();
179
         new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();

+ 5
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java View File

335
             args.putString("written", String.valueOf(bytesWritten));
335
             args.putString("written", String.valueOf(bytesWritten));
336
             args.putString("total", String.valueOf(contentLength));
336
             args.putString("total", String.valueOf(contentLength));
337
 
337
 
338
-            // emit event to js context
339
-            RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
340
-                    .emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
338
+            if(RNFetchBlobReq.isReportUploadProgress(mTaskId)) {
339
+                // emit event to js context
340
+                RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
341
+                        .emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
342
+            }
341
         }
343
         }
342
     }
344
     }
343
 }
345
 }

+ 31
- 5
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

64
         FileStorage
64
         FileStorage
65
     };
65
     };
66
 
66
 
67
-    static HashMap<String, Call> taskTable = new HashMap<>();
67
+    public static HashMap<String, Call> taskTable = new HashMap<>();
68
+    static HashMap<String, Boolean> progressReport = new HashMap<>();
69
+    static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
68
 
70
 
69
     MediaType contentType = RNFetchBlobConst.MIME_OCTET;
71
     MediaType contentType = RNFetchBlobConst.MIME_OCTET;
70
     ReactApplicationContext ctx;
72
     ReactApplicationContext ctx;
81
     long downloadManagerId;
83
     long downloadManagerId;
82
     RequestType requestType;
84
     RequestType requestType;
83
     ResponseType responseType;
85
     ResponseType responseType;
84
-    boolean timeout = false;
85
     WritableMap respInfo;
86
     WritableMap respInfo;
87
+    boolean timeout = false;
88
+    public boolean reportProgress = false;
89
+    public boolean reportUploadProgress = false;
86
 
90
 
87
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
91
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
88
         this.method = method;
92
         this.method = method;
304
             }
308
             }
305
 
309
 
306
             OkHttpClient client = clientBuilder.build();
310
             OkHttpClient client = clientBuilder.build();
307
-            Call call = client.newCall(req);
311
+            Call call =  client.newCall(req);
308
             taskTable.put(taskId, call);
312
             taskTable.put(taskId, call);
309
             call.enqueue(new okhttp3.Callback() {
313
             call.enqueue(new okhttp3.Callback() {
310
 
314
 
321
                     }
325
                     }
322
                     else
326
                     else
323
                         callback.invoke(e.getLocalizedMessage(), respInfo, null);
327
                         callback.invoke(e.getLocalizedMessage(), respInfo, null);
328
+                    removeTaskInfo();
324
                 }
329
                 }
325
 
330
 
326
                 @Override
331
                 @Override
356
         }
361
         }
357
     }
362
     }
358
 
363
 
364
+    /**
365
+     * Remove cached information of the HTTP task
366
+     */
367
+    private void removeTaskInfo() {
368
+        if(taskTable.containsKey(taskId))
369
+            taskTable.remove(taskId);
370
+        if(uploadProgressReport.containsKey(taskId))
371
+            uploadProgressReport.remove(taskId);
372
+        if(progressReport.containsKey(taskId))
373
+            progressReport.remove(taskId);
374
+    }
375
+
359
     /**
376
     /**
360
      * Send response data back to javascript context.
377
      * Send response data back to javascript context.
361
      * @param resp OkHttp response object
378
      * @param resp OkHttp response object
408
                 }
425
                 }
409
                 break;
426
                 break;
410
         }
427
         }
411
-        if(taskTable.containsKey(taskId))
412
-            taskTable.remove(taskId);
428
+        removeTaskInfo();
429
+    }
430
+
431
+    public static boolean isReportProgress(String taskId) {
432
+        if(!progressReport.containsKey(taskId)) return false;
433
+        return progressReport.get(taskId);
434
+    }
435
+
436
+    public static boolean isReportUploadProgress(String taskId) {
437
+        if(!uploadProgressReport.containsKey(taskId)) return false;
438
+        return uploadProgressReport.get(taskId);
413
     }
439
     }
414
 
440
 
415
     private WritableMap getResponseInfo(Response resp) {
441
     private WritableMap getResponseInfo(Response resp) {

+ 9
- 6
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobDefaultResp.java View File

1
 package com.RNFetchBlob.Response;
1
 package com.RNFetchBlob.Response;
2
 
2
 
3
 import com.RNFetchBlob.RNFetchBlob;
3
 import com.RNFetchBlob.RNFetchBlob;
4
+import com.RNFetchBlob.RNFetchBlobReq;
4
 import com.facebook.react.bridge.Arguments;
5
 import com.facebook.react.bridge.Arguments;
5
 import com.facebook.react.bridge.ReactApplicationContext;
6
 import com.facebook.react.bridge.ReactApplicationContext;
6
 import com.facebook.react.bridge.WritableMap;
7
 import com.facebook.react.bridge.WritableMap;
64
 
65
 
65
             long read =  mOriginalSource.read(sink, byteCount);
66
             long read =  mOriginalSource.read(sink, byteCount);
66
             bytesRead += read > 0 ? read : 0;
67
             bytesRead += read > 0 ? read : 0;
67
-            WritableMap args = Arguments.createMap();
68
-            args.putString("taskId", mTaskId);
69
-            args.putString("written", String.valueOf(bytesRead));
70
-            args.putString("total", String.valueOf(contentLength()));
71
-            rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
72
-                    .emit("RNFetchBlobProgress", args);
68
+            if(RNFetchBlobReq.isReportProgress(mTaskId)) {
69
+                WritableMap args = Arguments.createMap();
70
+                args.putString("taskId", mTaskId);
71
+                args.putString("written", String.valueOf(bytesRead));
72
+                args.putString("total", String.valueOf(contentLength()));
73
+                rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
74
+                        .emit("RNFetchBlobProgress", args);
75
+            }
73
             return read;
76
             return read;
74
         }
77
         }
75
 
78
 

+ 4
- 1
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java View File

2
 
2
 
3
 import android.util.Log;
3
 import android.util.Log;
4
 
4
 
5
+import com.RNFetchBlob.RNFetchBlobReq;
5
 import com.facebook.react.bridge.Arguments;
6
 import com.facebook.react.bridge.Arguments;
6
 import com.facebook.react.bridge.ReactApplicationContext;
7
 import com.facebook.react.bridge.ReactApplicationContext;
7
 import com.facebook.react.bridge.WritableMap;
8
 import com.facebook.react.bridge.WritableMap;
69
             long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
70
             long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
70
             bytesDownloaded += read > 0 ? read : 0;
71
             bytesDownloaded += read > 0 ? read : 0;
71
             Log.i("bytes downloaded", String.valueOf(byteCount) +"/"+ String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
72
             Log.i("bytes downloaded", String.valueOf(byteCount) +"/"+ String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
72
-            if(read > 0) {
73
+            if(read > 0 ) {
73
                 ofStream.write(bytes, 0, (int) read);
74
                 ofStream.write(bytes, 0, (int) read);
75
+            }
76
+            if(RNFetchBlobReq.isReportProgress(mTaskId)) {
74
                 WritableMap args = Arguments.createMap();
77
                 WritableMap args = Arguments.createMap();
75
                 args.putString("taskId", mTaskId);
78
                 args.putString("taskId", mTaskId);
76
                 args.putString("written", String.valueOf(bytesDownloaded));
79
                 args.putString("written", String.valueOf(bytesDownloaded));

+ 2
- 0
src/index.js View File

175
   // method for register progress event handler and cancel request.
175
   // method for register progress event handler and cancel request.
176
   promise.progress = (fn) => {
176
   promise.progress = (fn) => {
177
     promise.onProgress = fn
177
     promise.onProgress = fn
178
+    RNFetchBlob.enableProgressReport(taskId)
178
     return promise
179
     return promise
179
   }
180
   }
180
   promise.uploadProgress = (fn) => {
181
   promise.uploadProgress = (fn) => {
181
     promise.onUploadProgress = fn
182
     promise.onUploadProgress = fn
183
+    RNFetchBlob.enableUploadProgressReport(taskId)
182
     return promise
184
     return promise
183
   }
185
   }
184
   promise.stateChange = (fn) => {
186
   promise.stateChange = (fn) => {

+ 8
- 0
src/ios/RNFetchBlob/RNFetchBlob.m View File

358
 
358
 
359
 }
359
 }
360
 
360
 
361
+RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId {
362
+    [RNFetchBlobNetwork enableProgressReport:taskId];
363
+})
364
+
365
+RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId {
366
+    [RNFetchBlobNetwork enableUploadProgress:taskId];
367
+})
368
+
361
 RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(NSNumber *)start end:(NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
369
 RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(NSNumber *)start end:(NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
362
 {
370
 {
363
     [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
371
     [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];

+ 3
- 0
src/ios/RNFetchBlobConst.h View File

36
 extern NSString *const FS_EVENT_WARN;
36
 extern NSString *const FS_EVENT_WARN;
37
 extern NSString *const FS_EVENT_ERROR;
37
 extern NSString *const FS_EVENT_ERROR;
38
 
38
 
39
+extern NSString *const KEY_REPORT_PROGRESS;
40
+extern NSString *const KEY_REPORT_UPLOAD_PROGRESS;
41
+
39
 
42
 
40
 
43
 
41
 #endif /* RNFetchBlobConst_h */
44
 #endif /* RNFetchBlobConst_h */

+ 3
- 0
src/ios/RNFetchBlobConst.m View File

30
 extern NSString *const FS_EVENT_END = @"end";
30
 extern NSString *const FS_EVENT_END = @"end";
31
 extern NSString *const FS_EVENT_WARN = @"warn";
31
 extern NSString *const FS_EVENT_WARN = @"warn";
32
 extern NSString *const FS_EVENT_ERROR = @"error";
32
 extern NSString *const FS_EVENT_ERROR = @"error";
33
+
34
+extern NSString *const KEY_REPORT_PROGRESS = @"reportProgress";
35
+extern NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";

+ 2
- 0
src/ios/RNFetchBlobNetwork.h View File

36
 
36
 
37
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
37
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
38
 + (void) cancelRequest:(NSString *)taskId;
38
 + (void) cancelRequest:(NSString *)taskId;
39
++ (void) enableProgressReport:(NSString *) taskId;
40
++ (void) enableUploadProgress:(NSString *) taskId;
39
 - (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
41
 - (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
40
 
42
 
41
 
43
 

+ 49
- 20
src/ios/RNFetchBlobNetwork.m View File

24
 ////////////////////////////////////////
24
 ////////////////////////////////////////
25
 
25
 
26
 NSMutableDictionary * taskTable;
26
 NSMutableDictionary * taskTable;
27
+NSMutableDictionary * progressTable;
28
+NSMutableDictionary * uploadProgressTable;
27
 
29
 
28
 @interface RNFetchBlobNetwork ()
30
 @interface RNFetchBlobNetwork ()
29
 {
31
 {
61
     if(taskTable == nil) {
63
     if(taskTable == nil) {
62
         taskTable = [[NSMutableDictionary alloc] init];
64
         taskTable = [[NSMutableDictionary alloc] init];
63
     }
65
     }
66
+    if(progressTable == nil)
67
+    {
68
+        progressTable = [[NSMutableDictionary alloc] init];
69
+    }
70
+    if(uploadProgressTable == nil)
71
+    {
72
+        uploadProgressTable = [[NSMutableDictionary alloc] init];
73
+    }
64
     return self;
74
     return self;
65
 }
75
 }
66
 
76
 
77
++ (void) enableProgressReport:(NSString *) taskId
78
+{
79
+    [progressTable setValue:@YES forKey:taskId];
80
+}
81
+
82
++ (void) enableUploadProgress:(NSString *) taskId
83
+{
84
+    [uploadProgressTable setValue:@YES forKey:taskId];
85
+}
67
 
86
 
68
 // removing case from headers
87
 // removing case from headers
69
-+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
88
++ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
89
+{
70
 
90
 
71
     NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
91
     NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
72
     for(NSString * key in headers) {
92
     for(NSString * key in headers) {
149
         respFile = NO;
169
         respFile = NO;
150
     }
170
     }
151
     NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
171
     NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
152
-    [taskTable setValue:task forKey:taskId];
172
+    [taskTable setValue:@{ @"task": task, KEY_REPORT_PROGRESS : @NO, KEY_REPORT_UPLOAD_PROGRESS : @NO} forKey:taskId];
153
     [task resume];
173
     [task resume];
154
 
174
 
155
     // network status indicator
175
     // network status indicator
248
     {
268
     {
249
         [writeStream write:[data bytes] maxLength:[data length]];
269
         [writeStream write:[data bytes] maxLength:[data length]];
250
     }
270
     }
251
-
252
-    [self.bridge.eventDispatcher
253
-     sendDeviceEventWithName:@"RNFetchBlobProgress"
254
-     body:@{
255
-            @"taskId": taskId,
256
-            @"written": [NSString stringWithFormat:@"%d", receivedBytes],
257
-            @"total": [NSString stringWithFormat:@"%d", expectedBytes]
258
-            }
259
-     ];
271
+    
272
+    if([progressTable valueForKey:taskId] == @YES)
273
+    {
274
+        [self.bridge.eventDispatcher
275
+         sendDeviceEventWithName:@"RNFetchBlobProgress"
276
+         body:@{
277
+                @"taskId": taskId,
278
+                @"written": [NSString stringWithFormat:@"%d", receivedBytes],
279
+                @"total": [NSString stringWithFormat:@"%d", expectedBytes]
280
+                }
281
+         ];
282
+    }
260
 
283
 
261
 }
284
 }
262
 
285
 
286
                    [respData base64EncodedStringWithOptions:0]
309
                    [respData base64EncodedStringWithOptions:0]
287
                    ]);
310
                    ]);
288
     }
311
     }
312
+    
313
+    [taskTable removeObjectForKey:taskId];
314
+    [uploadProgressTable removeObjectForKey:taskId];
315
+    [progressTable removeObjectForKey:taskId];
289
 }
316
 }
290
 
317
 
291
 // upload progress handler
318
 // upload progress handler
292
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
319
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
293
 {
320
 {
294
-    [self.bridge.eventDispatcher
295
-     sendDeviceEventWithName:@"RNFetchBlobProgress-upload"
296
-     body:@{
297
-            @"taskId": taskId,
298
-            @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
299
-            @"total": [NSString stringWithFormat:@"%d", bodyLength]
300
-            }
301
-     ];
321
+    if([uploadProgressTable valueForKey:taskId] == @YES) {
322
+        [self.bridge.eventDispatcher
323
+         sendDeviceEventWithName:@"RNFetchBlobProgress-upload"
324
+         body:@{
325
+                @"taskId": taskId,
326
+                @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
327
+                @"total": [NSString stringWithFormat:@"%d", bodyLength]
328
+                }
329
+         ];
330
+    }
302
 }
331
 }
303
 
332
 
304
 + (void) cancelRequest:(NSString *)taskId
333
 + (void) cancelRequest:(NSString *)taskId
305
 {
334
 {
306
-    NSURLSessionDataTask * task = (NSURLSessionDataTask *)[taskTable objectForKey:taskId];
335
+    NSURLSessionDataTask * task = (NSURLSessionDataTask *)[[taskTable objectForKey:taskId] objectForKey:@"task"];
307
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
336
     if(task != nil && task.state == NSURLSessionTaskStateRunning)
308
         [task cancel];
337
         [task cancel];
309
 }
338
 }