From fec6e51091c443da0ee62c4eb67412af4e0abc64 Mon Sep 17 00:00:00 2001 From: Salah Alkhwlani Date: Thu, 18 Jan 2018 01:26:13 +0300 Subject: [PATCH] feat(): support saving cached images with extension (#113) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- README.md | 16 ++++++++++++++++ src/providers/image-loader-config.ts | 19 +++++++++++++++++++ src/providers/image-loader.ts | 11 ++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 991cf66..82846b2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/providers/image-loader-config.ts b/src/providers/image-loader-config.ts index d802b0d..cdb69ec 100644 --- a/src/providers/image-loader-config.ts +++ b/src/providers/image-loader-config.ts @@ -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) { @@ -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; + } } diff --git a/src/providers/image-loader.ts b/src/providers/image-loader.ts index 6d3461d..37c961d 100644 --- a/src/providers/image-loader.ts +++ b/src/providers/image-loader.ts @@ -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) : ''); } /** @@ -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; + } }