Browse Source

Bug fix for download interruption promise rejection

Sushant Gupta 5 years ago
parent
commit
f836385b10

+ 13
- 3
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

531
                 }
531
                 }
532
                 break;
532
                 break;
533
             case FileStorage:
533
             case FileStorage:
534
+                ResponseBody responseBody = resp.body();
535
+
534
                 try {
536
                 try {
535
                     // In order to write response data to `destPath` we have to invoke this method.
537
                     // In order to write response data to `destPath` we have to invoke this method.
536
                     // It uses customized response body which is able to report download progress
538
                     // It uses customized response body which is able to report download progress
537
                     // and write response data to destination path.
539
                     // and write response data to destination path.
538
-                    resp.body().bytes();
540
+                    responseBody.bytes();
539
                 } catch (Exception ignored) {
541
                 } catch (Exception ignored) {
540
 //                    ignored.printStackTrace();
542
 //                    ignored.printStackTrace();
541
                 }
543
                 }
542
-                this.destPath = this.destPath.replace("?append=true", "");
543
-                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
544
+
545
+                RNFetchBlobFileResp rnFetchBlobFileResp = (RNFetchBlobFileResp) responseBody;
546
+
547
+                if(rnFetchBlobFileResp != null && rnFetchBlobFileResp.isDownloadComplete() == false){
548
+                    callback.invoke("RNFetchBlob failed. Download interrupted.", null);
549
+                }
550
+                else {
551
+                    this.destPath = this.destPath.replace("?append=true", "");
552
+                    callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
553
+                }
544
                 break;
554
                 break;
545
             default:
555
             default:
546
                 try {
556
                 try {

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

69
         return originalBody.contentLength();
69
         return originalBody.contentLength();
70
     }
70
     }
71
 
71
 
72
+    public boolean isDownloadComplete() {
73
+        return bytesDownloaded == contentLength();
74
+    }
75
+
72
     @Override
76
     @Override
73
     public BufferedSource source() {
77
     public BufferedSource source() {
74
         ProgressReportingSource countable = new ProgressReportingSource();
78
         ProgressReportingSource countable = new ProgressReportingSource();