Skip to content

Commit 1c0a9c4

Browse files
authored
Merge pull request #96 from jgsrodrigues/master
Validate if downloaded file is complete.
2 parents 23d27b4 + 6f7c092 commit 1c0a9c4

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

utils/fsUtils.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,27 @@ module.exports = {
104104
let status = Math.floor(res.respInfo.status / 100);
105105
if (status !== 2) {
106106
// TODO - log / return error?
107-
throw new Error('Download failed');
107+
return Promise.reject();
108108
}
109-
// the download is complete and rename the temporary file
110-
return fs.mv(tmpFile, toFile);
109+
110+
return RNFetchBlob.fs.stat(tmpFile)
111+
.then(fileStats => {
112+
// Verify if the content was fully downloaded!
113+
if (res.respInfo.headers['Content-Length'] && res.respInfo.headers['Content-Length'] != fileStats.size) {
114+
return Promise.reject();
115+
}
116+
117+
// the download is complete and rename the temporary file
118+
return fs.mv(tmpFile, toFile);
119+
});
120+
121+
122+
})
123+
.catch(error => {
124+
// cleanup. will try re-download on next CachedImage mount.
125+
this.deleteFile(tmpFile);
126+
delete activeDownloads[toFile];
127+
return Promise.reject('Download failed');
111128
})
112129
.then(() => {
113130
// cleanup

0 commit comments

Comments
 (0)