Skip to content

Commit

Permalink
feat(): support saving cached images with extension (#113)
Browse files Browse the repository at this point in the history
Some plugins of saving images to library/album doesn’t support source file path without extension

This PR add options to enable/disable add an extension to the filename of cache and by default is disabled
  • Loading branch information
salkhwlani authored and ihadeed committed Jan 17, 2018
1 parent 45201ce commit fec6e51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,22 @@ this.imageLoaderConfig.setHttpRequestOptions({
});
```
---
#### setFileNameCachedWithExtension(enable: boolean)
Enable/Disable the save filename of cached images with extension. Defaults to false.
Example:
```ts
this.imageLoaderConfig.setFileNameCachedWithExtension(true);
```
---
#### setFallbackFileNameCachedExtension(extension: string)
Sometime url missing extension, in this case you can set fallback as default extension. Defaults to '.jpg'
Example:
```ts
this.imageLoaderConfig.setFallbackFileNameCachedExtension('.png');
```
---
# Preloading images
```typescript
Expand Down
19 changes: 19 additions & 0 deletions src/providers/image-loader-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class ImageLoaderConfig {

httpHeaders: HttpHeaders;

fileNameCachedWithExtension: boolean = false;

fallbackFileNameCachedExtension: string = '.jpg';

private _cacheDirectoryName: string = 'image-loader-cache';

set cacheDirectoryName(name: string) {
Expand Down Expand Up @@ -202,4 +206,19 @@ export class ImageLoaderConfig {
// do nothing, plugin deprecated.
}

/**
* Enable/Disable the save filename of cached images with extension. Defaults to false.
* @param enable {boolean} set to true to enable
*/
setFileNameCachedWithExtension(enable: boolean) {
this.fileNameCachedWithExtension = enable;
}

/**
* Set fallback extension filename of cached images. Defaults to '.jpg'.
* @param extension {string} fallback extension (e.x .jpg)
*/
setFallbackFileNameCachedExtension(extension: string) {
this.fallbackFileNameCachedExtension = extension;
}
}
11 changes: 10 additions & 1 deletion src/providers/image-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ export class ImageLoader {
*/
private createFileName(url: string): string {
// hash the url to get a unique file name
return this.hashString(url).toString();
return this.hashString(url).toString() + (this.config.fileNameCachedWithExtension ? this.getExtensionFromFileName(url) : '');
}

/**
Expand All @@ -584,4 +584,13 @@ export class ImageLoader {
return hash;
}

/**
* extract extension from filename or url
*
* @param filename
* @returns {string}
*/
private getExtensionFromFileName(filename) {
return filename.substr((~-filename.lastIndexOf(".") >>> 0) + 1) || this.config.fallbackFileNameCachedExtension;
}
}

0 comments on commit fec6e51

Please sign in to comment.