From 38e1c58ee82ec6098f48eddc105a0f77c38c0d05 Mon Sep 17 00:00:00 2001 From: stefan Date: Mon, 16 Jul 2018 11:32:20 +0200 Subject: [PATCH] allow the user to switch cache directory to data directory via config --- src/providers/image-loader-config.ts | 2 ++ src/providers/image-loader.ts | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/providers/image-loader-config.ts b/src/providers/image-loader-config.ts index e9e9d60..3cdbd7b 100644 --- a/src/providers/image-loader-config.ts +++ b/src/providers/image-loader-config.ts @@ -42,6 +42,8 @@ export class ImageLoaderConfig { fallbackFileNameCachedExtension: string = '.jpg'; + cacheDirectoryType: 'cache'|'data' = 'cache'; + private _cacheDirectoryName: string = 'image-loader-cache'; get cacheDirectoryName(): string { diff --git a/src/providers/image-loader.ts b/src/providers/image-loader.ts index d619691..6216f8d 100644 --- a/src/providers/image-loader.ts +++ b/src/providers/image-loader.ts @@ -117,6 +117,13 @@ export class ImageLoader { return this.getImagePath(imageUrl); } + getFileCacheDirectory() { + if(this.config.cacheDirectoryType == 'data') { + return this.file.dataDirectory; + } + return this.file.cacheDirectory; + } + /** * Clears the cache */ @@ -135,7 +142,7 @@ export class ImageLoader { // pause any operations this.isInit = false; - this.file.removeRecursively(this.file.cacheDirectory, this.config.cacheDirectoryName) + this.file.removeRecursively(this.getFileCacheDirectory(), this.config.cacheDirectoryName) .then(() => { if (this.isWKWebView && !this.isIonicWKWebView) { @@ -271,7 +278,7 @@ export class ImageLoader { done(); }; - const localDir = this.file.cacheDirectory + this.config.cacheDirectoryName + '/'; + const localDir = this.getFileCacheDirectory() + this.config.cacheDirectoryName + '/'; const fileName = this.createFileName(currentItem.imageUrl); this.http.get(currentItem.imageUrl, { @@ -377,7 +384,7 @@ export class ImageLoader { this.cacheIndex = []; - return this.file.listDir(this.file.cacheDirectory, this.config.cacheDirectoryName) + return this.file.listDir(this.getFileCacheDirectory(), this.config.cacheDirectoryName) .then(files => Promise.all(files.map(this.addFileToIndex.bind(this)))) .then(() => { // Sort items by date. Most recent to oldest. @@ -433,7 +440,7 @@ export class ImageLoader { */ private removeFile(file: string): Promise { return this.file - .removeFile(this.file.cacheDirectory + this.config.cacheDirectoryName, file) + .removeFile(this.getFileCacheDirectory() + this.config.cacheDirectoryName, file) .then(() => { if (this.isWKWebView && !this.isIonicWKWebView) { return this.file @@ -467,7 +474,7 @@ export class ImageLoader { const fileName = this.createFileName(url); // get full path - const dirPath = this.file.cacheDirectory + this.config.cacheDirectoryName, + const dirPath = this.getFileCacheDirectory() + this.config.cacheDirectoryName, tempDirPath = this.file.tempDirectory + this.config.cacheDirectoryName; // check if exists @@ -555,7 +562,7 @@ export class ImageLoader { /** * Check if the cache directory exists - * @param directory {string} The directory to check. Either this.file.tempDirectory or this.file.cacheDirectory + * @param directory {string} The directory to check. Either this.file.tempDirectory or this.getFileCacheDirectory() * @returns {Promise} Returns a promise that resolves if exists, and rejects if it doesn't */ private cacheDirectoryExists(directory: string): Promise { @@ -574,12 +581,12 @@ export class ImageLoader { if (replace) { // create or replace the cache directory - cacheDirectoryPromise = this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, replace); + cacheDirectoryPromise = this.file.createDir(this.getFileCacheDirectory(), this.config.cacheDirectoryName, replace); } else { // check if the cache directory exists. // if it does not exist create it! - cacheDirectoryPromise = this.cacheDirectoryExists(this.file.cacheDirectory) - .catch(() => this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, false)); + cacheDirectoryPromise = this.cacheDirectoryExists(this.getFileCacheDirectory()) + .catch(() => this.file.createDir(this.getFileCacheDirectory(), this.config.cacheDirectoryName, false)); } if (this.isWKWebView && !this.isIonicWKWebView) {