Skip to content

Commit

Permalink
Merge pull request #96 from jgsrodrigues/master
Browse files Browse the repository at this point in the history
Validate if downloaded file is complete.
  • Loading branch information
kfiroo authored Oct 20, 2017
2 parents 23d27b4 + 6f7c092 commit 1c0a9c4
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions utils/fsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,27 @@ module.exports = {
let status = Math.floor(res.respInfo.status / 100);
if (status !== 2) {
// TODO - log / return error?
throw new Error('Download failed');
return Promise.reject();
}
// the download is complete and rename the temporary file
return fs.mv(tmpFile, toFile);

return RNFetchBlob.fs.stat(tmpFile)
.then(fileStats => {
// Verify if the content was fully downloaded!
if (res.respInfo.headers['Content-Length'] && res.respInfo.headers['Content-Length'] != fileStats.size) {
return Promise.reject();
}

// the download is complete and rename the temporary file
return fs.mv(tmpFile, toFile);
});


})
.catch(error => {
// cleanup. will try re-download on next CachedImage mount.
this.deleteFile(tmpFile);
delete activeDownloads[toFile];
return Promise.reject('Download failed');
})
.then(() => {
// cleanup
Expand Down

0 comments on commit 1c0a9c4

Please sign in to comment.