Skip to content

Commit a9c330f

Browse files
swiftyoneihadeed
authored andcommitted
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
1 parent d90a02f commit a9c330f

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

src/providers/image-loader.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,11 @@ export class ImageLoader {
270270

271271
this.concurrency = this.config.concurrency;
272272

273-
this.cacheDirectoryExists
274-
.catch(() => {
275-
// doesn't exist
276-
return this.createCacheDirectory(replace)
277-
.catch(e => {
278-
279-
this.throwError(e);
280-
this.isInit = true;
281-
282-
});
273+
// create cache directories if they do not exist
274+
this.createCacheDirectory(replace)
275+
.catch(e => {
276+
this.throwError(e);
277+
this.isInit = true;
283278
})
284279
.then(() => this.indexCache())
285280
.then(() => {
@@ -502,27 +497,52 @@ export class ImageLoader {
502497

503498
/**
504499
* Check if the cache directory exists
500+
* @param directory {string} The directory to check. Either this.file.tempDirectory or this.file.cacheDirectory
505501
* @returns {Promise<boolean|FileError>} Returns a promise that resolves if exists, and rejects if it doesn't
506502
*/
507-
private get cacheDirectoryExists(): Promise<boolean> {
508-
return this.file
509-
.checkDir(this.file.cacheDirectory, this.config.cacheDirectoryName)
510-
.then(() => {
511-
return (this.isWKWebView ? this.file.checkDir(this.file.tempDirectory, this.config.cacheDirectoryName) : Promise.resolve(true));
512-
});
503+
private cacheDirectoryExists(directory: string): Promise<boolean> {
504+
return this.file.checkDir(directory, this.config.cacheDirectoryName);
513505
}
514506

515507
/**
516-
* Creates the cache directory
508+
* Create the cache directories
517509
* @param replace {boolean} override directory if exists
518-
* @returns {Promise<DirectoryEntry|FileError>} Returns a promise that resolves if the directory was created, and rejects on error
510+
* @returns {Promise<DirectoryEntry|FileError>} Returns a promise that resolves if the directories were created, and rejects on error
519511
*/
520-
private createCacheDirectory(replace: boolean = false): Promise<DirectoryEntry> {
521-
return this.file
522-
.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, replace)
523-
.then((res: DirectoryEntry) => {
524-
return this.isWKWebView ? this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace) : Promise.resolve(res);
525-
});
512+
private createCacheDirectory(replace: boolean = false): Promise<any> {
513+
let cacheDirectoryPromise: Promise<any>,
514+
tempDirectoryPromise: Promise<any>;
515+
516+
517+
if (replace) {
518+
// create or replace the cache directory
519+
cacheDirectoryPromise = this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, replace);
520+
} else {
521+
// check if the cache directory exists.
522+
// if it does not exist create it!
523+
cacheDirectoryPromise = this.cacheDirectoryExists(this.file.cacheDirectory)
524+
.catch(() => {
525+
return this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName);
526+
});
527+
}
528+
529+
if (this.isWKWebView) {
530+
if (replace) {
531+
// create or replace the temp directory
532+
tempDirectoryPromise = this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace);
533+
} else {
534+
// check if the temp directory exists.
535+
// if it does not exist create it!
536+
tempDirectoryPromise = this.cacheDirectoryExists(this.file.tempDirectory)
537+
.catch(() => {
538+
return this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName);
539+
});
540+
}
541+
} else {
542+
tempDirectoryPromise = Promise.resolve();
543+
}
544+
545+
return Promise.all([cacheDirectoryPromise, tempDirectoryPromise]);
526546
}
527547

528548
/**

0 commit comments

Comments
 (0)