https://dev.azure.com/umbraco/D-Team%20Tracker/_workitems/edit/6586 - Migrated ImageUrlGeneratorController + Made usage some strings into the enums in the ImageUrlGenerationOptions

This commit is contained in:
Bjarke Berg
2020-05-20 17:39:07 +02:00
parent 0d65fccd52
commit 28c4f87236
10 changed files with 44 additions and 28 deletions

View File

@@ -1,4 +1,6 @@
namespace Umbraco.Core.Models using Umbraco.Web.Models;
namespace Umbraco.Core.Models
{ {
/// <summary> /// <summary>
/// These are options that are passed to the IImageUrlGenerator implementation to determine /// These are options that are passed to the IImageUrlGenerator implementation to determine
@@ -17,8 +19,8 @@
public decimal? WidthRatio { get; set; } public decimal? WidthRatio { get; set; }
public decimal? HeightRatio { get; set; } public decimal? HeightRatio { get; set; }
public int? Quality { get; set; } public int? Quality { get; set; }
public string ImageCropMode { get; set; } public ImageCropMode? ImageCropMode { get; set; }
public string ImageCropAnchor { get; set; } public ImageCropAnchor? ImageCropAnchor { get; set; }
public bool DefaultCrop { get; set; } public bool DefaultCrop { get; set; }
public FocalPointPosition FocalPoint { get; set; } public FocalPointPosition FocalPoint { get; set; }
public CropCoordinates Crop { get; set; } public CropCoordinates Crop { get; set; }

View File

@@ -3,6 +3,7 @@ using System.Text;
using Umbraco.Core; using Umbraco.Core;
using Umbraco.Core.Media; using Umbraco.Core.Media;
using Umbraco.Core.Models; using Umbraco.Core.Models;
using Umbraco.Web.Models;
namespace Umbraco.Infrastructure.Media namespace Umbraco.Infrastructure.Media
{ {
@@ -19,9 +20,9 @@ namespace Umbraco.Infrastructure.Media
else if (options.DefaultCrop) imageProcessorUrl.Append("?anchor=center&mode=crop"); else if (options.DefaultCrop) imageProcessorUrl.Append("?anchor=center&mode=crop");
else else
{ {
imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? "crop").ToLower()); imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? ImageCropMode.Crop).ToString().ToLower());
if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToLower()); if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToString().ToLower());
} }
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format="); var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");

View File

@@ -10,6 +10,7 @@ using Umbraco.Core.Models.Membership;
using Umbraco.Core.Services; using Umbraco.Core.Services;
using Umbraco.Core.Security; using Umbraco.Core.Security;
using Umbraco.Core.Media; using Umbraco.Core.Media;
using Umbraco.Web.Models;
namespace Umbraco.Core.Models namespace Umbraco.Core.Models
{ {
@@ -78,11 +79,11 @@ namespace Umbraco.Core.Models
var avatarUrl = mediaFileSystem.GetUrl(user.Avatar); var avatarUrl = mediaFileSystem.GetUrl(user.Avatar);
return new[] return new[]
{ {
imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = "crop", Width = 30, Height = 30 }), imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = ImageCropMode.Crop, Width = 30, Height = 30 }),
imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = "crop", Width = 60, Height = 60 }), imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = ImageCropMode.Crop, Width = 60, Height = 60 }),
imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = "crop", Width = 90, Height = 90 }), imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = ImageCropMode.Crop, Width = 90, Height = 90 }),
imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = "crop", Width = 150, Height = 150 }), imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = ImageCropMode.Crop, Width = 150, Height = 150 }),
imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = "crop", Width = 300, Height = 300 }) imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(avatarUrl) { ImageCropMode = ImageCropMode.Crop, Width = 300, Height = 300 })
}; };
} }

View File

@@ -10,6 +10,7 @@ using Umbraco.Core.Media;
using Umbraco.Core.Models; using Umbraco.Core.Models;
using Umbraco.Core.Services; using Umbraco.Core.Services;
using Umbraco.Core.Strings; using Umbraco.Core.Strings;
using Umbraco.Web.Models;
using Umbraco.Web.Routing; using Umbraco.Web.Routing;
using Umbraco.Web.Templates; using Umbraco.Web.Templates;
@@ -120,7 +121,7 @@ namespace Umbraco.Web.PropertyEditors
if (width != int.MinValue && height != int.MinValue) if (width != int.MinValue && height != int.MinValue)
{ {
location = imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(location) { ImageCropMode = "max", Width = width, Height = height }); location = imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(location) { ImageCropMode = ImageCropMode.Max, Width = width, Height = height });
} }
img.SetAttributeValue("src", location); img.SetAttributeValue("src", location);

View File

@@ -20,6 +20,7 @@ using Umbraco.Web;
using Umbraco.Web.PropertyEditors; using Umbraco.Web.PropertyEditors;
using System.Text; using System.Text;
using Umbraco.Infrastructure.Media; using Umbraco.Infrastructure.Media;
using Umbraco.Web.Models;
namespace Umbraco.Tests.Models namespace Umbraco.Tests.Models
{ {
@@ -126,11 +127,11 @@ namespace Umbraco.Tests.Models
[Test] [Test]
public void GetCropUrl_SpecifiedCropModeTest() public void GetCropUrl_SpecifiedCropModeTest()
{ {
var urlStringMin = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "Min", Width = 300, Height = 150 }); var urlStringMin = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Min, Width = 300, Height = 150 });
var urlStringBoxPad = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "BoxPad", Width = 300, Height = 150 }); var urlStringBoxPad = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.BoxPad, Width = 300, Height = 150 });
var urlStringPad = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "Pad", Width = 300, Height = 150 }); var urlStringPad = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 300, Height = 150 });
var urlStringMax = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "Max", Width = 300, Height = 150 }); var urlStringMax = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Max, Width = 300, Height = 150 });
var urlStringStretch = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "Stretch", Width = 300, Height = 150 }); var urlStringStretch = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Stretch, Width = 300, Height = 150 });
Assert.AreEqual(MediaPath + "?mode=min&width=300&height=150", urlStringMin); Assert.AreEqual(MediaPath + "?mode=min&width=300&height=150", urlStringMin);
Assert.AreEqual(MediaPath + "?mode=boxpad&width=300&height=150", urlStringBoxPad); Assert.AreEqual(MediaPath + "?mode=boxpad&width=300&height=150", urlStringBoxPad);
@@ -145,7 +146,7 @@ namespace Umbraco.Tests.Models
[Test] [Test]
public void GetCropUrl_UploadTypeTest() public void GetCropUrl_UploadTypeTest()
{ {
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "Crop", ImageCropAnchor = "Center", Width = 100, Height = 270 }); var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Crop, ImageCropAnchor = ImageCropAnchor.Center, Width = 100, Height = 270 });
Assert.AreEqual(MediaPath + "?mode=crop&anchor=center&width=100&height=270", urlString); Assert.AreEqual(MediaPath + "?mode=crop&anchor=center&width=100&height=270", urlString);
} }
@@ -225,7 +226,7 @@ namespace Umbraco.Tests.Models
[Test] [Test]
public void GetCropUrl_BackgroundColorParameter() public void GetCropUrl_BackgroundColorParameter()
{ {
var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = "Pad", Width = 400, Height = 400, FurtherOptions = "&bgcolor=fff" }); var urlString = Generator.GetImageUrl(new ImageUrlGenerationOptions(MediaPath) { ImageCropMode = ImageCropMode.Pad, Width = 400, Height = 400, FurtherOptions = "&bgcolor=fff" });
Assert.AreEqual(MediaPath + "?mode=pad&width=400&height=400&bgcolor=fff", urlString); Assert.AreEqual(MediaPath + "?mode=pad&width=400&height=400&bgcolor=fff", urlString);
} }
} }

View File

@@ -4,6 +4,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Umbraco.Core.Media; using Umbraco.Core.Media;
using Umbraco.Core.Models;
using Umbraco.Web.Common.Attributes;
using Umbraco.Web.Models; using Umbraco.Web.Models;
using Umbraco.Web.Mvc; using Umbraco.Web.Mvc;
@@ -20,6 +22,7 @@ namespace Umbraco.Web.Editors
/// building to generate correct URLs /// building to generate correct URLs
/// </para> /// </para>
/// </remarks> /// </remarks>
[PluginController("UmbracoApi")]
public class ImageUrlGeneratorController : UmbracoAuthorizedJsonController public class ImageUrlGeneratorController : UmbracoAuthorizedJsonController
{ {
private readonly IImageUrlGenerator _imageUrlGenerator; private readonly IImageUrlGenerator _imageUrlGenerator;
@@ -31,7 +34,13 @@ namespace Umbraco.Web.Editors
public string GetCropUrl(string mediaPath, int? width = null, int? height = null, ImageCropMode? imageCropMode = null, string animationProcessMode = null) public string GetCropUrl(string mediaPath, int? width = null, int? height = null, ImageCropMode? imageCropMode = null, string animationProcessMode = null)
{ {
return mediaPath.GetCropUrl(_imageUrlGenerator, null, width: width, height: height, imageCropMode: imageCropMode, animationProcessMode: animationProcessMode); return _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(mediaPath)
{
Width = width,
Height = height,
ImageCropMode = imageCropMode,
AnimationProcessMode = animationProcessMode
});
} }
} }
} }

View File

@@ -9,6 +9,7 @@ using Umbraco.Core.Media;
using Umbraco.Core.Models; using Umbraco.Core.Models;
using Umbraco.Web.BackOffice.Controllers; using Umbraco.Web.BackOffice.Controllers;
using Umbraco.Web.Common.Attributes; using Umbraco.Web.Common.Attributes;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc; using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi; using Umbraco.Web.WebApi;
@@ -79,7 +80,7 @@ namespace Umbraco.Web.Editors
} }
var rnd = imageLastModified.HasValue ? $"&rnd={imageLastModified:yyyyMMddHHmmss}" : null; var rnd = imageLastModified.HasValue ? $"&rnd={imageLastModified:yyyyMMddHHmmss}" : null;
var imageUrl = _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(imagePath) { UpScale = false, Width = width, AnimationProcessMode = "first", ImageCropMode = "max", CacheBusterValue = rnd }); var imageUrl = _imageUrlGenerator.GetImageUrl(new ImageUrlGenerationOptions(imagePath) { UpScale = false, Width = width, AnimationProcessMode = "first", ImageCropMode = ImageCropMode.Max, CacheBusterValue = rnd });
return new RedirectResult(imageUrl, false); return new RedirectResult(imageUrl, false);
} }
@@ -105,7 +106,7 @@ namespace Umbraco.Web.Editors
int? focalPointLeft = null, int? focalPointLeft = null,
int? focalPointTop = null, int? focalPointTop = null,
string animationProcessMode = "first", string animationProcessMode = "first",
string mode = "max", ImageCropMode mode = ImageCropMode.Max,
bool upscale = false, bool upscale = false,
string cacheBusterValue = "") string cacheBusterValue = "")
{ {

View File

@@ -340,10 +340,11 @@ namespace Umbraco.Web.Editors
"tinyMceApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl<TinyMceController>( "tinyMceApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl<TinyMceController>(
controller => controller.UploadImage()) controller => controller.UploadImage())
}, },
{ //TODO Reintroduce
"imageUrlGeneratorApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl<ImageUrlGeneratorController>( // {
controller => controller.GetCropUrl(null, null, null, null, null)) // "imageUrlGeneratorApiBaseUrl", _urlHelper.GetUmbracoApiServiceBaseUrl<ImageUrlGeneratorController>(
}, // controller => controller.GetCropUrl(null, null, null, null, null))
// },
} }
}, },
{ {

View File

@@ -346,8 +346,8 @@ namespace Umbraco.Web
{ {
options = new ImageUrlGenerationOptions (imageUrl) options = new ImageUrlGenerationOptions (imageUrl)
{ {
ImageCropMode = (imageCropMode ?? ImageCropMode.Pad).ToString().ToLowerInvariant(), ImageCropMode = (imageCropMode ?? ImageCropMode.Pad),
ImageCropAnchor = imageCropAnchor?.ToString().ToLowerInvariant() ImageCropAnchor = imageCropAnchor
}; };
} }

View File

@@ -158,7 +158,6 @@
<Compile Include="Editors\Filters\ContentSaveModelValidator.cs" /> <Compile Include="Editors\Filters\ContentSaveModelValidator.cs" />
<Compile Include="Editors\Filters\DataTypeValidateAttribute.cs" /> <Compile Include="Editors\Filters\DataTypeValidateAttribute.cs" />
<Compile Include="Editors\Filters\MediaSaveModelValidator.cs" /> <Compile Include="Editors\Filters\MediaSaveModelValidator.cs" />
<Compile Include="Editors\ImageUrlGeneratorController.cs" />
<Compile Include="Editors\PackageController.cs" /> <Compile Include="Editors\PackageController.cs" />
<Compile Include="Editors\KeepAliveController.cs" /> <Compile Include="Editors\KeepAliveController.cs" />
<Compile Include="Editors\MacrosController.cs" /> <Compile Include="Editors\MacrosController.cs" />