Code cleanup, added unit tests

This commit is contained in:
Benjamin Carleski
2020-02-08 11:05:14 -08:00
parent e7248ea48f
commit 408ee452e1
5 changed files with 343 additions and 170 deletions

View File

@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Models;
@@ -13,13 +9,14 @@ namespace Umbraco.Web.Models
{
public string GetImageUrl(ImageUrlGenerationOptions options)
{
if (options == null) return null;
var imageProcessorUrl = new StringBuilder(options.ImageUrl ?? string.Empty);
if (options.FocalPoint != null)
{
imageProcessorUrl.Append("?center=");
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append(",");
imageProcessorUrl.Append(options.FocalPoint.Top.ToString(CultureInfo.InvariantCulture)).Append(",");
imageProcessorUrl.Append(options.FocalPoint.Left.ToString(CultureInfo.InvariantCulture));
imageProcessorUrl.Append("&mode=crop");
}
@@ -34,73 +31,31 @@ namespace Umbraco.Web.Models
}
else if (options.DefaultCrop)
{
imageProcessorUrl.Append("?anchor=center");
imageProcessorUrl.Append("&mode=crop");
imageProcessorUrl.Append("?anchor=center&mode=crop");
}
else
{
imageProcessorUrl.Append("?mode=" + options.ImageCropMode.ToString().ToLower());
imageProcessorUrl.Append("?mode=").Append((options.ImageCropMode ?? "crop").ToLower());
if (options.ImageCropAnchor != null)
{
imageProcessorUrl.Append("&anchor=" + options.ImageCropAnchor.ToString().ToLower());
}
if (options.ImageCropAnchor != null) imageProcessorUrl.Append("&anchor=").Append(options.ImageCropAnchor.ToLower());
}
var hasFormat = options.FurtherOptions != null && options.FurtherOptions.InvariantContains("&format=");
//Only put quality here, if we don't have a format specified.
//Otherwise we need to put quality at the end to avoid it being overridden by the format.
if (options.Quality != null && hasFormat == false)
{
imageProcessorUrl.Append("&quality=" + options.Quality);
}
if (options.HeightRatio != null)
{
imageProcessorUrl.Append("&heightratio=" + options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (options.WidthRatio != null)
{
imageProcessorUrl.Append("&widthratio=" + options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
}
if (options.Width != null)
{
imageProcessorUrl.Append("&width=" + options.Width);
}
if (options.Height != null)
{
imageProcessorUrl.Append("&height=" + options.Height);
}
if (options.UpScale == false)
{
imageProcessorUrl.Append("&upscale=false");
}
if (options.AnimationProcessMode != null)
{
imageProcessorUrl.Append("&animationprocessmode=" + options.AnimationProcessMode);
}
if (options.FurtherOptions != null)
{
imageProcessorUrl.Append(options.FurtherOptions);
}
if (options.Quality != null && hasFormat == false) imageProcessorUrl.Append("&quality=").Append(options.Quality);
if (options.HeightRatio != null) imageProcessorUrl.Append("&heightratio=").Append(options.HeightRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.WidthRatio != null) imageProcessorUrl.Append("&widthratio=").Append(options.WidthRatio.Value.ToString(CultureInfo.InvariantCulture));
if (options.Width != null) imageProcessorUrl.Append("&width=").Append(options.Width);
if (options.Height != null) imageProcessorUrl.Append("&height=").Append(options.Height);
if (options.UpScale == false) imageProcessorUrl.Append("&upscale=false");
if (options.AnimationProcessMode != null) imageProcessorUrl.Append("&animationprocessmode=").Append(options.AnimationProcessMode);
if (options.FurtherOptions != null) imageProcessorUrl.Append(options.FurtherOptions);
//If furtherOptions contains a format, we need to put the quality after the format.
if (options.Quality != null && hasFormat)
{
imageProcessorUrl.Append("&quality=" + options.Quality);
}
if (options.CacheBusterValue != null)
{
imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
}
if (options.Quality != null && hasFormat) imageProcessorUrl.Append("&quality=").Append(options.Quality);
if (options.CacheBusterValue != null) imageProcessorUrl.Append("&rnd=").Append(options.CacheBusterValue);
return imageProcessorUrl.ToString();
}