v10: Fix build warnings in Web.Common (#12349)
* Run code cleanup * Run dotnet format * Start manual cleanup in Web.Common * Finish up manual cleanup * Fix tests * Fix up InMemoryModelFactory.cs * Inject proper macroRenderer * Update src/Umbraco.Web.Common/Filters/JsonDateTimeFormatAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Headers;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -10,79 +8,80 @@ using SixLabors.ImageSharp.Web.Middleware;
|
||||
using SixLabors.ImageSharp.Web.Processors;
|
||||
using Umbraco.Cms.Core.Configuration.Models;
|
||||
|
||||
namespace Umbraco.Cms.Web.Common.DependencyInjection
|
||||
namespace Umbraco.Cms.Web.Common.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the ImageSharp middleware options.
|
||||
/// </summary>
|
||||
/// <seealso cref="IConfigureOptions{ImageSharpMiddlewareOptions}" />
|
||||
public sealed class ConfigureImageSharpMiddlewareOptions : IConfigureOptions<ImageSharpMiddlewareOptions>
|
||||
{
|
||||
private readonly Configuration _configuration;
|
||||
private readonly ImagingSettings _imagingSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Configures the ImageSharp middleware options.
|
||||
/// Initializes a new instance of the <see cref="ConfigureImageSharpMiddlewareOptions" /> class.
|
||||
/// </summary>
|
||||
/// <seealso cref="IConfigureOptions{ImageSharpMiddlewareOptions}" />
|
||||
public sealed class ConfigureImageSharpMiddlewareOptions : IConfigureOptions<ImageSharpMiddlewareOptions>
|
||||
/// <param name="configuration">The ImageSharp configuration.</param>
|
||||
/// <param name="imagingSettings">The Umbraco imaging settings.</param>
|
||||
public ConfigureImageSharpMiddlewareOptions(Configuration configuration, IOptions<ImagingSettings> imagingSettings)
|
||||
{
|
||||
private readonly Configuration _configuration;
|
||||
private readonly ImagingSettings _imagingSettings;
|
||||
_configuration = configuration;
|
||||
_imagingSettings = imagingSettings.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConfigureImageSharpMiddlewareOptions" /> class.
|
||||
/// </summary>
|
||||
/// <param name="configuration">The ImageSharp configuration.</param>
|
||||
/// <param name="imagingSettings">The Umbraco imaging settings.</param>
|
||||
public ConfigureImageSharpMiddlewareOptions(Configuration configuration, IOptions<ImagingSettings> imagingSettings)
|
||||
/// <inheritdoc />
|
||||
public void Configure(ImageSharpMiddlewareOptions options)
|
||||
{
|
||||
options.Configuration = _configuration;
|
||||
|
||||
options.BrowserMaxAge = _imagingSettings.Cache.BrowserMaxAge;
|
||||
options.CacheMaxAge = _imagingSettings.Cache.CacheMaxAge;
|
||||
options.CacheHashLength = _imagingSettings.Cache.CacheHashLength;
|
||||
|
||||
// Use configurable maximum width and height
|
||||
options.OnParseCommandsAsync = context =>
|
||||
{
|
||||
_configuration = configuration;
|
||||
_imagingSettings = imagingSettings.Value;
|
||||
}
|
||||
if (context.Commands.Count == 0)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Configure(ImageSharpMiddlewareOptions options)
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
// Change Cache-Control header when cache buster value is present
|
||||
options.OnPrepareResponseAsync = context =>
|
||||
{
|
||||
options.Configuration = _configuration;
|
||||
|
||||
options.BrowserMaxAge = _imagingSettings.Cache.BrowserMaxAge;
|
||||
options.CacheMaxAge = _imagingSettings.Cache.CacheMaxAge;
|
||||
options.CacheHashLength = _imagingSettings.Cache.CacheHashLength;
|
||||
|
||||
// Use configurable maximum width and height
|
||||
options.OnParseCommandsAsync = context =>
|
||||
if (context.Request.Query.ContainsKey("rnd") || context.Request.Query.ContainsKey("v"))
|
||||
{
|
||||
if (context.Commands.Count == 0)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
ResponseHeaders headers = context.Response.GetTypedHeaders();
|
||||
|
||||
int width = context.Parser.ParseValue<int>(context.Commands.GetValueOrDefault(ResizeWebProcessor.Width), context.Culture);
|
||||
if (width <= 0 || width > _imagingSettings.Resize.MaxWidth)
|
||||
{
|
||||
context.Commands.Remove(ResizeWebProcessor.Width);
|
||||
}
|
||||
CacheControlHeaderValue cacheControl =
|
||||
headers.CacheControl ?? new CacheControlHeaderValue { Public = true };
|
||||
cacheControl.MustRevalidate = false; // ImageSharp enables this by default
|
||||
cacheControl.Extensions.Add(new NameValueHeaderValue("immutable"));
|
||||
|
||||
int height = context.Parser.ParseValue<int>(context.Commands.GetValueOrDefault(ResizeWebProcessor.Height), context.Culture);
|
||||
if (height <= 0 || height > _imagingSettings.Resize.MaxHeight)
|
||||
{
|
||||
context.Commands.Remove(ResizeWebProcessor.Height);
|
||||
}
|
||||
headers.CacheControl = cacheControl;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
// Change Cache-Control header when cache buster value is present
|
||||
options.OnPrepareResponseAsync = context =>
|
||||
{
|
||||
if (context.Request.Query.ContainsKey("rnd") || context.Request.Query.ContainsKey("v"))
|
||||
{
|
||||
ResponseHeaders headers = context.Response.GetTypedHeaders();
|
||||
|
||||
CacheControlHeaderValue cacheControl = headers.CacheControl ?? new CacheControlHeaderValue()
|
||||
{
|
||||
Public = true
|
||||
};
|
||||
cacheControl.MustRevalidate = false; // ImageSharp enables this by default
|
||||
cacheControl.Extensions.Add(new NameValueHeaderValue("immutable"));
|
||||
|
||||
headers.CacheControl = cacheControl;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user