@@ -131,6 +131,13 @@ export class ImageLoader {
131131 return this . getImagePath ( imageUrl ) ;
132132 }
133133
134+ getFileCacheDirectory ( ) {
135+ if ( this . config . cacheDirectoryType == 'data' ) {
136+ return this . file . dataDirectory ;
137+ }
138+ return this . file . cacheDirectory ;
139+ }
140+
134141 /**
135142 * Clears the cache
136143 */
@@ -149,11 +156,7 @@ export class ImageLoader {
149156 // pause any operations
150157 this . isInit = false ;
151158
152- this . file
153- . removeRecursively (
154- this . file . cacheDirectory ,
155- this . config . cacheDirectoryName ,
156- )
159+ this . file . removeRecursively ( this . getFileCacheDirectory ( ) , this . config . cacheDirectoryName )
157160 . then ( ( ) => {
158161 if ( this . isWKWebView && ! this . isIonicWKWebView ) {
159162 // also clear the temp files
@@ -262,11 +265,56 @@ export class ImageLoader {
262265 // take the first item from queue
263266 const currentItem : QueueItem = this . queue . splice ( 0 , 1 ) [ 0 ] ;
264267 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 ) ;
270318 }
271319
272320 // function to call when done processing this item
@@ -397,8 +445,8 @@ export class ImageLoader {
397445 private indexCache ( ) : Promise < void > {
398446 this . cacheIndex = [ ] ;
399447
400- return this . file
401- . listDir ( this . file . cacheDirectory , this . config . cacheDirectoryName )
448+ return
449+ this . file . listDir ( this . getFileCacheDirectory ( ) , this . config . cacheDirectoryName )
402450 . then ( files => Promise . all ( files . map ( this . addFileToIndex . bind ( this ) ) ) )
403451 . then ( ( ) => {
404452 // Sort items by date. Most recent to oldest.
@@ -454,10 +502,7 @@ export class ImageLoader {
454502 */
455503 private removeFile ( file : string ) : Promise < any > {
456504 return this . file
457- . removeFile (
458- this . file . cacheDirectory + this . config . cacheDirectoryName ,
459- file ,
460- )
505+ . removeFile ( this . getFileCacheDirectory ( ) + this . config . cacheDirectoryName , file )
461506 . then ( ( ) => {
462507 if ( this . isWKWebView && ! this . isIonicWKWebView ) {
463508 return this . file
@@ -493,7 +538,7 @@ export class ImageLoader {
493538 const fileName = this . createFileName ( url ) ;
494539
495540 // get full path
496- const dirPath = this . file . cacheDirectory + this . config . cacheDirectoryName ,
541+ const dirPath = this . getFileCacheDirectory ( ) + this . config . cacheDirectoryName ,
497542 tempDirPath = this . file . tempDirectory + this . config . cacheDirectoryName ;
498543
499544 // check if exists
@@ -581,7 +626,7 @@ export class ImageLoader {
581626
582627 /**
583628 * 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()
585630 * @returns {Promise<boolean|FileError> } Returns a promise that resolves if exists, and rejects if it doesn't
586631 */
587632 private cacheDirectoryExists ( directory : string) : Promise < boolean > {
@@ -598,23 +643,12 @@ export class ImageLoader {
598643
599644 if ( replace ) {
600645 // 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 ) ;
606647 } else {
607648 // check if the cache directory exists.
608649 // 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 ) ) ;
618652 }
619653
620654 if ( this . isWKWebView && ! this . isIonicWKWebView ) {
0 commit comments