From a9c330f0d9f6f2635af6f933a5066869ca4a43a5 Mon Sep 17 00:00:00 2001 From: Lorenz an Mey Date: Thu, 20 Apr 2017 15:01:29 +0200 Subject: [PATCH] fix(image-loader): create both cache directories independently (#40) * Bugfix: create both cache directories independently Otherwise on iOS the temp directory will NOT be created if the cache directory exists * Bugfix: Only check existence of cache directories if they are not replaced anyhow --- src/providers/image-loader.ts | 68 ++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/src/providers/image-loader.ts b/src/providers/image-loader.ts index 16c9e24..82a2f3c 100644 --- a/src/providers/image-loader.ts +++ b/src/providers/image-loader.ts @@ -270,16 +270,11 @@ export class ImageLoader { this.concurrency = this.config.concurrency; - this.cacheDirectoryExists - .catch(() => { - // doesn't exist - return this.createCacheDirectory(replace) - .catch(e => { - - this.throwError(e); - this.isInit = true; - - }); + // create cache directories if they do not exist + this.createCacheDirectory(replace) + .catch(e => { + this.throwError(e); + this.isInit = true; }) .then(() => this.indexCache()) .then(() => { @@ -502,27 +497,52 @@ export class ImageLoader { /** * Check if the cache directory exists + * @param directory {string} The directory to check. Either this.file.tempDirectory or this.file.cacheDirectory * @returns {Promise} Returns a promise that resolves if exists, and rejects if it doesn't */ - private get cacheDirectoryExists(): Promise { - return this.file - .checkDir(this.file.cacheDirectory, this.config.cacheDirectoryName) - .then(() => { - return (this.isWKWebView ? this.file.checkDir(this.file.tempDirectory, this.config.cacheDirectoryName) : Promise.resolve(true)); - }); + private cacheDirectoryExists(directory: string): Promise { + return this.file.checkDir(directory, this.config.cacheDirectoryName); } /** - * Creates the cache directory + * Create the cache directories * @param replace {boolean} override directory if exists - * @returns {Promise} Returns a promise that resolves if the directory was created, and rejects on error + * @returns {Promise} Returns a promise that resolves if the directories were created, and rejects on error */ - private createCacheDirectory(replace: boolean = false): Promise { - return this.file - .createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, replace) - .then((res: DirectoryEntry) => { - return this.isWKWebView ? this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace) : Promise.resolve(res); - }); + private createCacheDirectory(replace: boolean = false): Promise { + let cacheDirectoryPromise: Promise, + tempDirectoryPromise: Promise; + + + if (replace) { + // create or replace the cache directory + cacheDirectoryPromise = this.file.createDir(this.file.cacheDirectory, 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(() => { + return this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName); + }); + } + + if (this.isWKWebView) { + if (replace) { + // create or replace the temp directory + tempDirectoryPromise = this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace); + } else { + // check if the temp directory exists. + // if it does not exist create it! + tempDirectoryPromise = this.cacheDirectoryExists(this.file.tempDirectory) + .catch(() => { + return this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName); + }); + } + } else { + tempDirectoryPromise = Promise.resolve(); + } + + return Promise.all([cacheDirectoryPromise, tempDirectoryPromise]); } /**