Move ImageSharpImageUrlGenerator to Web.Common and add NoopImageUrlGenerator

This commit is contained in:
Ronald Barendse
2021-08-13 15:53:36 +02:00
parent 74ea311e3c
commit 36568a0fdc
5 changed files with 52 additions and 18 deletions

View File

@@ -144,7 +144,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection
builder.PropertyValueConverters()
.Remove<SimpleTinyMceValueConverter>();
builder.Services.AddUnique<IImageUrlGenerator, ImageSharpImageUrlGenerator>();
builder.Services.AddUnique<IImageUrlGenerator, NoopImageUrlGenerator>();
// register *all* checks, except those marked [HideFromTypeFinder] of course
builder.Services.AddUnique<IMarkdownToHtmlConverter, MarkdownToHtmlConverter>();

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
namespace Umbraco.Cms.Infrastructure.Media
{
public class NoopImageUrlGenerator : IImageUrlGenerator
{
/// <inheritdoc />
public IEnumerable<string> SupportedImageFileTypes => Enumerable.Empty<string>();
/// <inheritdoc />
public string GetImageUrl(ImageUrlGenerationOptions options) => options?.ImageUrl;
}
}

View File

@@ -3,9 +3,9 @@
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Infrastructure.Media;
using Umbraco.Cms.Web.Common.Media;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Media
{
[TestFixture]
public class ImageSharpImageUrlGeneratorTests
@@ -14,7 +14,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Media
private static readonly ImageUrlGenerationOptions.CropCoordinates s_crop = new ImageUrlGenerationOptions.CropCoordinates(0.58729977382575338m, 0.055768992440203169m, 0m, 0.32457553600198386m);
private static readonly ImageUrlGenerationOptions.FocalPointPosition s_focus1 = new ImageUrlGenerationOptions.FocalPointPosition(0.96m, 0.80827067669172936m);
private static readonly ImageUrlGenerationOptions.FocalPointPosition s_focus2 = new ImageUrlGenerationOptions.FocalPointPosition(0.4275m, 0.41m);
private static readonly ImageSharpImageUrlGenerator s_generator = new ImageSharpImageUrlGenerator();
private static readonly ImageSharpImageUrlGenerator s_generator = new ImageSharpImageUrlGenerator(new string[0]);
[Test]
public void GetCropUrl_CropAliasTest()

View File

@@ -8,7 +8,9 @@ using SixLabors.ImageSharp.Web.DependencyInjection;
using SixLabors.ImageSharp.Web.Processors;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Web.Common.ImageProcessors;
using Umbraco.Cms.Web.Common.Media;
namespace Umbraco.Extensions
{
@@ -27,7 +29,6 @@ namespace Umbraco.Extensions
services.AddImageSharp(options =>
{
// We use the same default configuration instance in ImageSharpImageUrlGenerator, so we don't want to create a new instance here
options.Configuration = SixLabors.ImageSharp.Configuration.Default;
options.BrowserMaxAge = imagingSettings.Cache.BrowserMaxAge;
options.CacheMaxAge = imagingSettings.Cache.CacheMaxAge;
@@ -66,6 +67,8 @@ namespace Umbraco.Extensions
.AddProcessor<CropWebProcessor>()
.AddProcessor<ResizeWebProcessor>();
builder.Services.AddUnique<IImageUrlGenerator, ImageSharpImageUrlGenerator>();
return services;
}
}

View File

@@ -3,11 +3,14 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using SixLabors.ImageSharp;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Web.Middleware;
using SixLabors.ImageSharp.Web.Processors;
using Umbraco.Cms.Core.Media;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Web.Common.ImageProcessors;
namespace Umbraco.Cms.Infrastructure.Media
namespace Umbraco.Cms.Web.Common.Media
{
/// <summary>
/// Exposes a method that generates an image URL based on the specified options that can be processed by ImageSharp.
@@ -15,13 +18,25 @@ namespace Umbraco.Cms.Infrastructure.Media
/// <seealso cref="Umbraco.Cms.Core.Media.IImageUrlGenerator" />
public class ImageSharpImageUrlGenerator : IImageUrlGenerator
{
private static readonly string[] s_supportedImageFileTypes = Configuration.Default.ImageFormats.SelectMany(f => f.FileExtensions).ToArray();
/// <inheritdoc />
public IEnumerable<string> SupportedImageFileTypes { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ImageSharpImageUrlGenerator" /> class.
/// </summary>
/// <param name="options">The options.</param>
public ImageSharpImageUrlGenerator(IOptions<ImageSharpMiddlewareOptions> options)
: this(options.Value.Configuration.ImageFormats.SelectMany(f => f.FileExtensions).ToArray())
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ImageSharpImageUrlGenerator" /> class.
/// </summary>
/// <param name="supportedImageFileTypes">The supported image file types/extensions.</param>
/// <remarks>
/// This uses the default instance of the ImageSharp configuration, so we need to ensure we don't new up a different instance when configuring the middleware.
/// This constructor is only used for testing.
/// </remarks>
public IEnumerable<string> SupportedImageFileTypes { get; } = s_supportedImageFileTypes;
internal ImageSharpImageUrlGenerator(IEnumerable<string> supportedImageFileTypes) => SupportedImageFileTypes = supportedImageFileTypes;
/// <inheritdoc/>
public string GetImageUrl(ImageUrlGenerationOptions options)
@@ -46,37 +61,37 @@ namespace Umbraco.Cms.Infrastructure.Media
if (options.FocalPoint != null)
{
AddQueryString("rxy", options.FocalPoint.Left, options.FocalPoint.Top);
AddQueryString(ResizeWebProcessor.Xy, options.FocalPoint.Left, options.FocalPoint.Top);
}
if (options.Crop != null)
{
AddQueryString("cc", options.Crop.Left, options.Crop.Top, options.Crop.Right, options.Crop.Bottom);
AddQueryString(CropWebProcessor.Coordinates, options.Crop.Left, options.Crop.Top, options.Crop.Right, options.Crop.Bottom);
}
if (options.ImageCropMode.HasValue)
{
AddQueryString("rmode", options.ImageCropMode.Value.ToString().ToLowerInvariant());
AddQueryString(ResizeWebProcessor.Mode, options.ImageCropMode.Value.ToString().ToLowerInvariant());
}
if (options.ImageCropAnchor.HasValue)
{
AddQueryString("ranchor", options.ImageCropAnchor.Value.ToString().ToLowerInvariant());
AddQueryString(ResizeWebProcessor.Anchor, options.ImageCropAnchor.Value.ToString().ToLowerInvariant());
}
if (options.Width.HasValue)
{
AddQueryString("width", options.Width.Value);
AddQueryString(ResizeWebProcessor.Width, options.Width.Value);
}
if (options.Height.HasValue)
{
AddQueryString("height", options.Height.Value);
AddQueryString(ResizeWebProcessor.Height, options.Height.Value);
}
if (options.Quality.HasValue)
{
AddQueryString("quality", options.Quality.Value);
AddQueryString(JpegQualityWebProcessor.Quality, options.Quality.Value);
}
if (string.IsNullOrWhiteSpace(options.FurtherOptions) == false)