Skip to content

Commit 022f522

Browse files
committed
Adds configuration option for setting a fallback max age, in case the response doesn't have one
1 parent eebcbca commit 022f522

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

docs/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The `RemoteImageProviderOptions` class provides the following configuration opti
44

55
- `Settings`: A list of the different allowed sources for images.
66

7+
- `FallbackMaxAge`: Specifies a fallback max age for the image being loaded. Used if the server does not return a cache-control header. By default, it is set to `0.01:00:00` (1 hour).
8+
-
79
Each setting (`RemoteImageProviderSetting`) provides the following configuration options:
810

911
- `Prefix`: Specified in the constructor, and defines the local path to prefix all remote image requests with. For example, setting this to `/remote` allows requests like `/remote/https://test.com/test.png` to pass through this provider.
@@ -26,6 +28,8 @@ Each setting (`RemoteImageProviderSetting`) provides the following configuration
2628

2729
- `AdditionalOptions`: Allows specifying additional `RemoteImageProviderOptions` instances. This can be useful when you have multiple configurations with different prefixes or other settings.
2830

31+
- `VerifyUrl`: Boolean value. If set to true, the URL will be verified before downloading the image. This can be useful to prevent downloading and processing images returning 404 or other error codes. By default, it is set to `truee`.`
32+
2933
Please note that these options provide customization and control over how remote images are loaded and processed. You can adjust these options according to your specific requirements.
3034

3135
Don't forget to configure these options in your application's services configuration as shown in the Usage section of this README.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
namespace ImageSharpCommunity.Providers.Remote.Configuration;
1+
namespace ImageSharpCommunity.Providers.Remote.Configuration;
22
public class RemoteImageProviderOptions
33
{
44
/// <summary>
55
/// A list of settings for remote image providers. Here you define your url prefixes, and which domains are allowed to fetch images from.
66
/// </summary>
77
public List<RemoteImageProviderSetting> Settings { get; set; } = new List<RemoteImageProviderSetting>();
8-
}
8+
9+
/// <summary>
10+
/// Fallback max age for the image. If the server does not return a cache-control header, this value is used.
11+
/// </summary>
12+
public TimeSpan FallbackMaxAge { get; set; } = TimeSpan.FromHours(1);
13+
}

src/ImageSharpCommunity.Providers.Remote/RemoteImageProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public bool IsValidRequest(HttpContext context)
5757
else
5858
{
5959
_logger.LogDebug("Found matching remote image provider setting for path: {path}", context.Request.Path);
60-
return Task.FromResult((IImageResolver?)new RemoteImageResolver(_clientFactory, url, options, _resolverLogger));
60+
return Task.FromResult((IImageResolver?)new RemoteImageResolver(_clientFactory, url, options, _resolverLogger, _options));
6161
}
6262
}
6363
private bool IsMatch(HttpContext context)
@@ -85,7 +85,7 @@ private bool UrlReturnsSuccess(RemoteImageProviderSetting setting, Uri uri)
8585

8686
if (response.Headers.CacheControl?.MaxAge is not null)
8787
{
88-
_cache.Set(nameof(RemoteImageProvider) + uri, response.IsSuccessStatusCode, response.Headers.CacheControl.MaxAge ?? TimeSpan.Zero);
88+
_cache.Set(nameof(RemoteImageProvider) + uri, response.IsSuccessStatusCode, response.Headers.CacheControl.MaxAge ?? _options.FallbackMaxAge);
8989
}
9090

9191
return response.IsSuccessStatusCode;

src/ImageSharpCommunity.Providers.Remote/RemoteImageResolver.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public class RemoteImageResolver : IImageResolver
99
private readonly string _url;
1010
private readonly RemoteImageProviderSetting _setting;
1111
private readonly ILogger<RemoteImageResolver> _logger;
12+
private readonly RemoteImageProviderOptions _options;
1213

13-
public RemoteImageResolver(IHttpClientFactory clientFactory, string url, RemoteImageProviderSetting setting, ILogger<RemoteImageResolver> logger)
14+
public RemoteImageResolver(IHttpClientFactory clientFactory, string url, RemoteImageProviderSetting setting, ILogger<RemoteImageResolver> logger, RemoteImageProviderOptions options)
1415
{
1516
_clientFactory = clientFactory;
1617
_url = url;
1718
_setting = setting;
1819
_logger = logger;
20+
_options = options;
1921
}
2022

2123
public async Task<ImageMetadata> GetMetaDataAsync()
@@ -38,10 +40,14 @@ public async Task<ImageMetadata> GetMetaDataAsync()
3840

3941
if (response.Headers.CacheControl?.MaxAge is null)
4042
{
41-
_logger.LogDebug("MaxAge header is null from {Url}", _url);
43+
_logger.LogDebug("MaxAge header is null from {Url}, falling back to configured FallbackMaxAge {FallbackMaxAge}", _url, _options.FallbackMaxAge);
4244
}
4345

44-
return new ImageMetadata(response.Content.Headers.LastModified.GetValueOrDefault().UtcDateTime, (response.Headers.CacheControl?.MaxAge).GetValueOrDefault(), response.Content.Headers.ContentLength.GetValueOrDefault());
46+
return new ImageMetadata(
47+
response.Content.Headers.LastModified.GetValueOrDefault().UtcDateTime,
48+
response.Headers.CacheControl?.MaxAge ?? _options.FallbackMaxAge,
49+
response.Content.Headers.ContentLength.GetValueOrDefault()
50+
);
4551
}
4652

4753
public async Task<Stream> OpenReadAsync()

src/Umbraco.Community.ImageSharpRemoteImages/appsettings-schema.umbraco-community-imagesharpremoteimages.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
"items": {
4646
"$ref": "#/definitions/UmbracoCommunityImageSharpRemoteImagesSettingDefinition"
4747
}
48+
},
49+
"FallbackMaxAge": {
50+
"type": "string",
51+
"description": "Fallback max age for the image. If the server does not return a cache-control header, this value is used.",
52+
"format": "duration",
53+
"default": "0.01:00:00"
4854
}
4955
}
5056
},
@@ -96,6 +102,10 @@
96102
"AllowAllDomains": {
97103
"type": "boolean",
98104
"description": "Allows all domains to be processed."
105+
},
106+
"VerifyUrl": {
107+
"type": "boolean",
108+
"description": "Verify that the input url returns a succesful status code."
99109
}
100110
},
101111
"required": [ "Prefix" ]

0 commit comments

Comments
 (0)