From 6520e0993d0375cd73efe3c2d5bf77b33e2827b0 Mon Sep 17 00:00:00 2001 From: Ben Hsieh Date: Tue, 14 Feb 2017 15:35:31 +0800 Subject: [PATCH] Apply fix to #264 --- .../java/com/RNFetchBlob/RNFetchBlobReq.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java b/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java index f91adf733..1ca79ba30 100644 --- a/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java +++ b/src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java @@ -500,11 +500,32 @@ private void done(Response resp) { // It uses customized response body which is able to report download progress // and write response data to destination path. resp.body().bytes(); + } catch (Exception ignored) { // ignored.printStackTrace(); } this.destPath = this.destPath.replace("?append=true", ""); - callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath); + + try { + long expectedLength = resp.body().contentLength(); + // when response contains Content-Length, check if the stream length is correct + if(expectedLength > 0) { + long actualLength = new File(this.destPath).length(); + if(actualLength != expectedLength) { + callback.invoke("RNFetchBlob failed to write data to storage : expected " + expectedLength + " bytes but got " + actualLength + " bytes", null); + } + else { + callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath); + } + } + else { + callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath); + } + } + catch (Exception err) { + callback.invoke(err.getMessage()); + err.printStackTrace(); + } break; default: try {