@@ -131,6 +131,13 @@ export class ImageLoader {
131
131
return this . getImagePath ( imageUrl ) ;
132
132
}
133
133
134
+ getFileCacheDirectory ( ) {
135
+ if ( this . config . cacheDirectoryType == 'data' ) {
136
+ return this . file . dataDirectory ;
137
+ }
138
+ return this . file . cacheDirectory ;
139
+ }
140
+
134
141
/**
135
142
* Clears the cache
136
143
*/
@@ -149,11 +156,7 @@ export class ImageLoader {
149
156
// pause any operations
150
157
this . isInit = false ;
151
158
152
- this . file
153
- . removeRecursively (
154
- this . file . cacheDirectory ,
155
- this . config . cacheDirectoryName ,
156
- )
159
+ this . file . removeRecursively ( this . getFileCacheDirectory ( ) , this . config . cacheDirectoryName )
157
160
. then ( ( ) => {
158
161
if ( this . isWKWebView && ! this . isIonicWKWebView ) {
159
162
// also clear the temp files
@@ -262,11 +265,56 @@ export class ImageLoader {
262
265
// take the first item from queue
263
266
const currentItem : QueueItem = this . queue . splice ( 0 , 1 ) [ 0 ] ;
264
267
if ( this . currentlyProcessing [ currentItem . imageUrl ] === undefined ) {
265
- this . currentlyProcessing [ currentItem . imageUrl ] = new Promise (
266
- ( resolve , reject ) => {
267
- // process more items concurrently if we can
268
- if ( this . canProcess ) {
269
- this . processQueue ( ) ;
268
+ this . currentlyProcessing [ currentItem . imageUrl ] = new Promise ( ( resolve , reject ) => {
269
+ // process more items concurrently if we can
270
+ if ( this . canProcess ) this . processQueue ( ) ;
271
+
272
+ // function to call when done processing this item
273
+ // this will reduce the processing number
274
+ // then will execute this function again to process any remaining items
275
+ const done = ( ) => {
276
+ this . processing -- ;
277
+ this . processQueue ( ) ;
278
+
279
+ if ( this . currentlyProcessing [ currentItem . imageUrl ] !== undefined ) {
280
+ delete this . currentlyProcessing [ currentItem . imageUrl ] ;
281
+ }
282
+ } ;
283
+
284
+ const error = ( e ) => {
285
+ currentItem . reject ( ) ;
286
+ this . throwError ( e ) ;
287
+ done ( ) ;
288
+ } ;
289
+
290
+ const localDir = this . getFileCacheDirectory ( ) + this . config . cacheDirectoryName + '/' ;
291
+ const fileName = this . createFileName ( currentItem . imageUrl ) ;
292
+
293
+ this . http . get ( currentItem . imageUrl , {
294
+ responseType : 'blob' ,
295
+ headers : this . config . httpHeaders
296
+ } ) . subscribe (
297
+ ( data : Blob ) => {
298
+ this . file . writeFile ( localDir , fileName , data , { replace : true } ) . then ( ( file : FileEntry ) => {
299
+ if ( this . isCacheSpaceExceeded ) {
300
+ this . maintainCacheSize ( ) ;
301
+ }
302
+ this . addFileToIndex ( file ) . then ( ( ) => {
303
+ this . getCachedImagePath ( currentItem . imageUrl ) . then ( ( localUrl ) => {
304
+ currentItem . resolve ( localUrl ) ;
305
+ resolve ( ) ;
306
+ done ( ) ;
307
+ this . maintainCacheSize ( ) ;
308
+ } ) ;
309
+ } ) ;
310
+ } ) . catch ( ( e ) => {
311
+ //Could not write image
312
+ error ( e ) ;
313
+ } ) ;
314
+ } ,
315
+ ( e ) => {
316
+ //Could not get image via httpClient
317
+ error ( e ) ;
270
318
}
271
319
272
320
// function to call when done processing this item
@@ -397,8 +445,8 @@ export class ImageLoader {
397
445
private indexCache ( ) : Promise < void > {
398
446
this . cacheIndex = [ ] ;
399
447
400
- return this . file
401
- . listDir ( this . file . cacheDirectory , this . config . cacheDirectoryName )
448
+ return
449
+ this . file . listDir ( this . getFileCacheDirectory ( ) , this . config . cacheDirectoryName )
402
450
. then ( files => Promise . all ( files . map ( this . addFileToIndex . bind ( this ) ) ) )
403
451
. then ( ( ) => {
404
452
// Sort items by date. Most recent to oldest.
@@ -454,10 +502,7 @@ export class ImageLoader {
454
502
*/
455
503
private removeFile ( file : string ) : Promise < any > {
456
504
return this . file
457
- . removeFile (
458
- this . file . cacheDirectory + this . config . cacheDirectoryName ,
459
- file ,
460
- )
505
+ . removeFile ( this . getFileCacheDirectory ( ) + this . config . cacheDirectoryName , file )
461
506
. then ( ( ) => {
462
507
if ( this . isWKWebView && ! this . isIonicWKWebView ) {
463
508
return this . file
@@ -493,7 +538,7 @@ export class ImageLoader {
493
538
const fileName = this . createFileName ( url ) ;
494
539
495
540
// get full path
496
- const dirPath = this . file . cacheDirectory + this . config . cacheDirectoryName ,
541
+ const dirPath = this . getFileCacheDirectory ( ) + this . config . cacheDirectoryName ,
497
542
tempDirPath = this . file . tempDirectory + this . config . cacheDirectoryName ;
498
543
499
544
// check if exists
@@ -581,7 +626,7 @@ export class ImageLoader {
581
626
582
627
/**
583
628
* Check if the cache directory exists
584
- * @param directory {string} The directory to check. Either this.file.tempDirectory or this.file.cacheDirectory
629
+ * @param directory {string} The directory to check. Either this.file.tempDirectory or this.getFileCacheDirectory()
585
630
* @returns {Promise<boolean|FileError> } Returns a promise that resolves if exists, and rejects if it doesn't
586
631
*/
587
632
private cacheDirectoryExists ( directory : string) : Promise < boolean > {
@@ -598,23 +643,12 @@ export class ImageLoader {
598
643
599
644
if ( replace ) {
600
645
// create or replace the cache directory
601
- cacheDirectoryPromise = this . file . createDir (
602
- this . file . cacheDirectory ,
603
- this . config . cacheDirectoryName ,
604
- replace ,
605
- ) ;
646
+ cacheDirectoryPromise = this . file . createDir ( this . getFileCacheDirectory ( ) , this . config . cacheDirectoryName , replace ) ;
606
647
} else {
607
648
// check if the cache directory exists.
608
649
// if it does not exist create it!
609
- cacheDirectoryPromise = this . cacheDirectoryExists (
610
- this . file . cacheDirectory ,
611
- ) . catch ( ( ) =>
612
- this . file . createDir (
613
- this . file . cacheDirectory ,
614
- this . config . cacheDirectoryName ,
615
- false ,
616
- ) ,
617
- ) ;
650
+ cacheDirectoryPromise = this . cacheDirectoryExists ( this . getFileCacheDirectory ( ) )
651
+ . catch ( ( ) => this . file . createDir ( this . getFileCacheDirectory ( ) , this . config . cacheDirectoryName , false ) ) ;
618
652
}
619
653
620
654
if ( this . isWKWebView && ! this . isIonicWKWebView ) {
0 commit comments