File tree Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -277,6 +277,22 @@ this.imageLoaderConfig.setHttpRequestOptions({
277277});
278278```
279279---
280+ #### setFileNameCachedWithExtension(enable: boolean)
281+ Enable/Disable the save filename of cached images with extension. Defaults to false.
282+
283+ Example:
284+ ```ts
285+ this.imageLoaderConfig.setFileNameCachedWithExtension(true);
286+ ```
287+ ---
288+ #### setFallbackFileNameCachedExtension(extension: string)
289+ Sometime url missing extension, in this case you can set fallback as default extension. Defaults to '.jpg'
290+
291+ Example:
292+ ```ts
293+ this.imageLoaderConfig.setFallbackFileNameCachedExtension('.png');
294+ ```
295+ ---
280296
281297# Preloading images
282298```typescript
Original file line number Diff line number Diff line change @@ -38,6 +38,10 @@ export class ImageLoaderConfig {
3838
3939 httpHeaders : HttpHeaders ;
4040
41+ fileNameCachedWithExtension : boolean = false ;
42+
43+ fallbackFileNameCachedExtension : string = '.jpg' ;
44+
4145 private _cacheDirectoryName : string = 'image-loader-cache' ;
4246
4347 set cacheDirectoryName ( name : string ) {
@@ -202,4 +206,19 @@ export class ImageLoaderConfig {
202206 // do nothing, plugin deprecated.
203207 }
204208
209+ /**
210+ * Enable/Disable the save filename of cached images with extension. Defaults to false.
211+ * @param enable {boolean} set to true to enable
212+ */
213+ setFileNameCachedWithExtension ( enable : boolean ) {
214+ this . fileNameCachedWithExtension = enable ;
215+ }
216+
217+ /**
218+ * Set fallback extension filename of cached images. Defaults to '.jpg'.
219+ * @param extension {string} fallback extension (e.x .jpg)
220+ */
221+ setFallbackFileNameCachedExtension ( extension : string ) {
222+ this . fallbackFileNameCachedExtension = extension ;
223+ }
205224}
Original file line number Diff line number Diff line change @@ -565,7 +565,7 @@ export class ImageLoader {
565565 */
566566 private createFileName ( url : string ) : string {
567567 // hash the url to get a unique file name
568- return this . hashString ( url ) . toString ( ) ;
568+ return this . hashString ( url ) . toString ( ) + ( this . config . fileNameCachedWithExtension ? this . getExtensionFromFileName ( url ) : '' ) ;
569569 }
570570
571571 /**
@@ -584,4 +584,13 @@ export class ImageLoader {
584584 return hash ;
585585 }
586586
587+ /**
588+ * extract extension from filename or url
589+ *
590+ * @param filename
591+ * @returns {string }
592+ */
593+ private getExtensionFromFileName ( filename ) {
594+ return filename . substr ( ( ~ - filename . lastIndexOf ( "." ) >>> 0 ) + 1 ) || this . config . fallbackFileNameCachedExtension ;
595+ }
587596}
You can’t perform that action at this time.
0 commit comments