Browse Source

Fix zero division error

Ben Hsieh 8 years ago
parent
commit
ab110cebdc

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

372
      */
372
      */
373
     private void emitUploadProgress(int written) {
373
     private void emitUploadProgress(int written) {
374
         RNFetchBlobProgressConfig config = RNFetchBlobReq.getReportUploadProgress(mTaskId);
374
         RNFetchBlobProgressConfig config = RNFetchBlobReq.getReportUploadProgress(mTaskId);
375
-        if(config.enable && config.shouldReport((float)written/contentLength)) {
375
+        if(config != null && contentLength != 0 && config.shouldReport((float)written/contentLength)) {
376
             WritableMap args = Arguments.createMap();
376
             WritableMap args = Arguments.createMap();
377
             args.putString("taskId", mTaskId);
377
             args.putString("taskId", mTaskId);
378
             args.putString("written", String.valueOf(written));
378
             args.putString("written", String.valueOf(written));

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

92
     ResponseFormat responseFormat = ResponseFormat.Auto;
92
     ResponseFormat responseFormat = ResponseFormat.Auto;
93
     WritableMap respInfo;
93
     WritableMap respInfo;
94
     boolean timeout = false;
94
     boolean timeout = false;
95
-
96
     ArrayList<String> redirects = new ArrayList<>();
95
     ArrayList<String> redirects = new ArrayList<>();
97
 
96
 
98
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
97
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {

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

63
             long read =  mOriginalSource.read(sink, byteCount);
63
             long read =  mOriginalSource.read(sink, byteCount);
64
             bytesRead += read > 0 ? read : 0;
64
             bytesRead += read > 0 ? read : 0;
65
             RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
65
             RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
66
-            if(reportConfig != null && reportConfig.shouldReport(bytesRead/contentLength())) {
66
+            long cLen = contentLength();
67
+            if(reportConfig != null && cLen != 0 && reportConfig.shouldReport(bytesRead/contentLength())) {
67
                 WritableMap args = Arguments.createMap();
68
                 WritableMap args = Arguments.createMap();
68
                 args.putString("taskId", mTaskId);
69
                 args.putString("taskId", mTaskId);
69
                 args.putString("written", String.valueOf(bytesRead));
70
                 args.putString("written", String.valueOf(bytesRead));

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

80
                     ofStream.write(bytes, 0, (int) read);
80
                     ofStream.write(bytes, 0, (int) read);
81
                 }
81
                 }
82
                 RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
82
                 RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
83
-                if (reportConfig != null && reportConfig.shouldReport(bytesDownloaded / contentLength())) {
83
+                if (reportConfig != null && contentLength() != 0 &&reportConfig.shouldReport(bytesDownloaded / contentLength())) {
84
                     WritableMap args = Arguments.createMap();
84
                     WritableMap args = Arguments.createMap();
85
                     args.putString("taskId", mTaskId);
85
                     args.putString("taskId", mTaskId);
86
                     args.putString("written", String.valueOf(bytesDownloaded));
86
                     args.putString("written", String.valueOf(bytesDownloaded));

+ 4
- 0
src/ios/RNFetchBlobNetwork.m View File

325
         [writeStream write:[data bytes] maxLength:[data length]];
325
         [writeStream write:[data bytes] maxLength:[data length]];
326
     }
326
     }
327
     RNFetchBlobProgress * pconfig = [progressTable valueForKey:taskId];
327
     RNFetchBlobProgress * pconfig = [progressTable valueForKey:taskId];
328
+    if(expectedBytes == 0)
329
+        return;
328
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
330
     NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
329
     if(pconfig != nil && [pconfig shouldReport:now])
331
     if(pconfig != nil && [pconfig shouldReport:now])
330
     {
332
     {
430
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
432
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
431
 {
433
 {
432
     RNFetchBlobProgress * pconfig = [uploadProgressTable valueForKey:taskId];
434
     RNFetchBlobProgress * pconfig = [uploadProgressTable valueForKey:taskId];
435
+    if(totalBytesExpectedToWrite == 0)
436
+        return;
433
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
437
     NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
434
     if(pconfig != nil && [pconfig shouldReport:now]) {
438
     if(pconfig != nil && [pconfig shouldReport:now]) {
435
         [self.bridge.eventDispatcher
439
         [self.bridge.eventDispatcher