Skip to content

support save cached images with extension #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ this.imageLoaderConfig.setFileTransferOptions({
});
```
---
#### 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 @@ -39,6 +39,10 @@ export class ImageLoaderConfig {
trustAllHosts: false
};

fileNameCachedWithExtension: boolean = false;

fallbackFileNameCachedExtension: string = '.jpg';

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

set cacheDirectoryName(name: string) {
Expand Down Expand Up @@ -194,4 +198,19 @@ export class ImageLoaderConfig {
this.fileTransferOptions = options;
}

/**
* 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 @@ -576,7 +576,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 @@ -595,4 +595,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;
}
}