2022-04-29 13:16:24 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Http.Headers;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Microsoft.Net.Http.Headers;
|
|
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
|
using SixLabors.ImageSharp.Web.Commands;
|
|
|
|
|
using SixLabors.ImageSharp.Web.Middleware;
|
|
|
|
|
using SixLabors.ImageSharp.Web.Processors;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configures the ImageSharp middleware options.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <seealso cref="IConfigureOptions{ImageSharpMiddlewareOptions}" />
|
|
|
|
|
public sealed class ConfigureImageSharpMiddlewareOptions : IConfigureOptions<ImageSharpMiddlewareOptions>
|
2022-04-29 13:16:24 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
private readonly Configuration _configuration;
|
|
|
|
|
private readonly ImagingSettings _imagingSettings;
|
|
|
|
|
|
2022-04-29 13:16:24 +02:00
|
|
|
/// <summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
/// Initializes a new instance of the <see cref="ConfigureImageSharpMiddlewareOptions" /> class.
|
2022-04-29 13:16:24 +02:00
|
|
|
/// </summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <param name="configuration">The ImageSharp configuration.</param>
|
|
|
|
|
/// <param name="imagingSettings">The Umbraco imaging settings.</param>
|
|
|
|
|
public ConfigureImageSharpMiddlewareOptions(Configuration configuration, IOptions<ImagingSettings> imagingSettings)
|
2022-04-29 13:16:24 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
_configuration = configuration;
|
|
|
|
|
_imagingSettings = imagingSettings.Value;
|
|
|
|
|
}
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Configure(ImageSharpMiddlewareOptions options)
|
|
|
|
|
{
|
|
|
|
|
options.Configuration = _configuration;
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
options.BrowserMaxAge = _imagingSettings.Cache.BrowserMaxAge;
|
|
|
|
|
options.CacheMaxAge = _imagingSettings.Cache.CacheMaxAge;
|
|
|
|
|
options.CacheHashLength = _imagingSettings.Cache.CacheHashLength;
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// Use configurable maximum width and height
|
|
|
|
|
options.OnParseCommandsAsync = context =>
|
|
|
|
|
{
|
|
|
|
|
if (context.Commands.Count == 0)
|
2022-04-29 13:16:24 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
var width = context.Parser.ParseValue<int>(
|
|
|
|
|
context.Commands.GetValueOrDefault(ResizeWebProcessor.Width),
|
|
|
|
|
context.Culture);
|
|
|
|
|
if (width <= 0 || width > _imagingSettings.Resize.MaxWidth)
|
|
|
|
|
{
|
|
|
|
|
context.Commands.Remove(ResizeWebProcessor.Width);
|
|
|
|
|
}
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
var height = context.Parser.ParseValue<int>(
|
|
|
|
|
context.Commands.GetValueOrDefault(ResizeWebProcessor.Height),
|
|
|
|
|
context.Culture);
|
|
|
|
|
if (height <= 0 || height > _imagingSettings.Resize.MaxHeight)
|
|
|
|
|
{
|
|
|
|
|
context.Commands.Remove(ResizeWebProcessor.Height);
|
|
|
|
|
}
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
};
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
// Change Cache-Control header when cache buster value is present
|
|
|
|
|
options.OnPrepareResponseAsync = context =>
|
|
|
|
|
{
|
|
|
|
|
if (context.Request.Query.ContainsKey("rnd") || context.Request.Query.ContainsKey("v"))
|
2022-04-29 13:16:24 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
ResponseHeaders headers = context.Response.GetTypedHeaders();
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
CacheControlHeaderValue cacheControl =
|
|
|
|
|
headers.CacheControl ?? new CacheControlHeaderValue { Public = true };
|
|
|
|
|
cacheControl.MustRevalidate = false; // ImageSharp enables this by default
|
|
|
|
|
cacheControl.Extensions.Add(new NameValueHeaderValue("immutable"));
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
headers.CacheControl = cacheControl;
|
|
|
|
|
}
|
2022-04-29 13:16:24 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
|
};
|
2022-04-29 13:16:24 +02:00
|
|
|
}
|
|
|
|
|
}
|