@@ -11,7 +11,7 @@ import {
11
11
View ,
12
12
Image ,
13
13
ImageBackground ,
14
- Platform ,
14
+ Platform
15
15
} from 'react-native' ;
16
16
17
17
import RNFetchBlob from 'rn-fetch-blob' ;
@@ -144,47 +144,50 @@ export default class CachedImage extends Component {
144
144
this . state . source = this . props . source ;
145
145
}
146
146
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
+ }
174
170
}
175
171
}
176
- }
177
- } , children ) ;
172
+ } , this . props . children ) ;
178
173
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
+ }
180
184
}
181
185
}
182
186
183
187
async function _unlinkFile ( file ) {
184
188
try {
185
189
return await RNFetchBlob . fs . unlink ( file ) ;
186
- } catch ( e ) {
187
-
190
+ } catch ( e ) {
188
191
}
189
192
}
190
193
@@ -230,33 +233,41 @@ async function _saveCacheFile(url: string, success: Function, failure: Function)
230
233
url
231
234
)
232
235
. 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 ) ;
250
255
}
251
- default :
252
- _unlinkFile ( tempCacheFile ) ;
253
- failure && failure ( "status code:" + status ) ;
254
- break ;
255
256
}
256
257
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
+ } ) ;
257
267
} )
258
268
. catch ( async ( error ) => {
259
269
_unlinkFile ( tempCacheFile ) ;
270
+ _unlinkFile ( cacheFile ) ;
260
271
failure && failure ( error ) ;
261
272
} ) ;
262
273
} else if ( isBase64 ) {
@@ -271,7 +282,7 @@ async function _saveCacheFile(url: string, success: Function, failure: Function)
271
282
failure && failure ( error ) ;
272
283
} ) ;
273
284
} else {
274
- failure && failure ( "not support url" ) ;
285
+ failure && failure ( new Error ( "NotSupportedUrl" ) ) ;
275
286
}
276
287
} catch ( error ) {
277
288
failure && failure ( error ) ;
0 commit comments