Skip to content

Commit 7d3b28c

Browse files
author
Wonday
committed
fix file successfully download check
1 parent 9a89595 commit 7d3b28c

File tree

1 file changed

+66
-55
lines changed

1 file changed

+66
-55
lines changed

index.js

+66-55
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
View,
1212
Image,
1313
ImageBackground,
14-
Platform,
14+
Platform
1515
} from 'react-native';
1616

1717
import RNFetchBlob from 'rn-fetch-blob';
@@ -144,47 +144,50 @@ export default class CachedImage extends Component {
144144
this.state.source = this.props.source;
145145
}
146146

147-
let children = (this.state.source) ? this.props.children : (
148-
<View {...this.props} style={this.props.style ? [this.props.style, {
149-
alignItems: 'center',
150-
justifyContent: 'center'
151-
}] : {alignItems: 'center', justifyContent: 'center'}}>
152-
{this.props.activityIndicator}
153-
</View>);
154-
155-
const renderImage = (props, children) => (children ? <ImageBackground {...props}>{children}</ImageBackground> :
156-
<Image {...props}/>);
157-
158-
const result = renderImage({
159-
...this.props,
160-
source: this.state.source,
161-
onError: (error) => {
162-
// error happened, delete cache
163-
if (this.props.source && this.props.source.uri) {
164-
CachedImage.deleteCache(this.props.source.uri);
165-
}
166-
if (this.props.onError) {
167-
this.props.onError(error);
168-
} else {
169-
if (!this._useDefaultSource && this.props.defaultSource) {
170-
this._useDefaultSource = true;
171-
setTimeout(() => {
172-
this.setState({source: this.props.defaultSource});
173-
}, 0);
147+
if (this.state.source) {
148+
149+
const renderImage = (props, children) => (children ?
150+
<ImageBackground {...props}>{children}</ImageBackground> :
151+
<Image {...props}/>);
152+
153+
const result = renderImage({
154+
...this.props,
155+
source: this.state.source,
156+
onError: (error) => {
157+
// error happened, delete cache
158+
if (this.props.source && this.props.source.uri) {
159+
CachedImage.deleteCache(this.props.source.uri);
160+
}
161+
if (this.props.onError) {
162+
this.props.onError(error);
163+
} else {
164+
if (!this._useDefaultSource && this.props.defaultSource) {
165+
this._useDefaultSource = true;
166+
setTimeout(() => {
167+
this.setState({source: this.props.defaultSource});
168+
}, 0);
169+
}
174170
}
175171
}
176-
}
177-
}, children);
172+
}, this.props.children);
178173

179-
return (result);
174+
return (result);
175+
} else {
176+
return (
177+
<View {...this.props} style={this.props.style ? [this.props.style, {
178+
alignItems: 'center',
179+
justifyContent: 'center'
180+
}] : {alignItems: 'center', justifyContent: 'center'}}>
181+
{this.props.activityIndicator}
182+
</View>);
183+
}
180184
}
181185
}
182186

183187
async function _unlinkFile(file) {
184188
try {
185189
return await RNFetchBlob.fs.unlink(file);
186-
}catch (e) {
187-
190+
} catch (e) {
188191
}
189192
}
190193

@@ -230,33 +233,41 @@ async function _saveCacheFile(url: string, success: Function, failure: Function)
230233
url
231234
)
232235
.then(async (res) => {
233-
let {status} = res.respInfo;
234-
235-
switch (status) {
236-
case 200: /* OK */
237-
case 204: /* No content */
238-
case 304: /* Not modified */
239-
{
240-
_unlinkFile(cacheFile);
241-
RNFetchBlob.fs
242-
.mv(tempCacheFile, cacheFile)
243-
.then(() => {
244-
success && success(cacheFile);
245-
})
246-
.catch(async (error) => {
247-
failure && failure(error);
248-
});
249-
break;
236+
237+
if (res && res.respInfo && res.respInfo.headers && res.respInfo.headers["Content-Length"]) {
238+
const expectedContentLength = res.respInfo.headers["Content-Length"];
239+
let actualContentLength;
240+
241+
try {
242+
const fileStats = await RNFetchBlob.fs.stat(res.path());
243+
244+
if (!fileStats || !fileStats.size) {
245+
throw new Error("FileNotFound:"+url);
246+
}
247+
248+
actualContentLength = fileStats.size;
249+
} catch (error) {
250+
throw new Error("DownloadFailed:"+url);
251+
}
252+
253+
if (expectedContentLength != actualContentLength) {
254+
throw new Error("DownloadFailed:"+url);
250255
}
251-
default:
252-
_unlinkFile(tempCacheFile);
253-
failure && failure("status code:" + status);
254-
break;
255256
}
256257

258+
_unlinkFile(cacheFile);
259+
RNFetchBlob.fs
260+
.mv(tempCacheFile, cacheFile)
261+
.then(() => {
262+
success && success(cacheFile);
263+
})
264+
.catch(async (error) => {
265+
throw error;
266+
});
257267
})
258268
.catch(async (error) => {
259269
_unlinkFile(tempCacheFile);
270+
_unlinkFile(cacheFile);
260271
failure && failure(error);
261272
});
262273
} else if (isBase64) {
@@ -271,7 +282,7 @@ async function _saveCacheFile(url: string, success: Function, failure: Function)
271282
failure && failure(error);
272283
});
273284
} else {
274-
failure && failure("not support url");
285+
failure && failure(new Error("NotSupportedUrl"));
275286
}
276287
} catch (error) {
277288
failure && failure(error);

0 commit comments

Comments
 (0)