Permalink
Browse files

Merge pull request #498 from burningice2866/fix-482

Making it possible to set Quality-property on ResizingOptions
  • Loading branch information...
napernik committed Nov 17, 2017
2 parents 4afc17e + 0385338 commit 0f037a59f181cdb34ecec6a2c56b43eca8a9b56a
Showing with 42 additions and 62 deletions.
  1. +42 −62 Composite/Core/WebClient/Media/ResizingOptions.cs
@@ -8,21 +8,20 @@
using System.Xml.Linq;
using Composite.Core.IO;
using Composite.Core.Xml;
using Composite.Core.Extensions;
using Composite.Core.Configuration;
namespace Composite.Core.WebClient.Media
{
/// <summary>
/// Resizing options for <see ref="Composite.Core.WebClient.Media.ImageResizer" />
/// </summary>
/// <exclude />
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public class ResizingOptions
{
private const string ResizedImageKeys = "~/App_Data/Composite/Media/ResizingOptions.xml";
private static string _resizedImageKeysFilePath;
private int? _qualityOverride;
/// <summary>
/// Image heigth
/// </summary>
@@ -43,33 +42,31 @@ public class ResizingOptions
/// </summary>
public int? MaxWidth { get; set; }
/// <summary>
/// Image quality (when doing lossy compression)
/// </summary>
private int? QualityOverride { get; set; }
/// <summary>
/// Indicate if resizing options has a default or non-default quality setting (used when doing lossy compression).
/// </summary>
public bool CustomQuality {
get
{
return QualityOverride.HasValue;
}
}
public bool CustomQuality => _qualityOverride.HasValue;
/// <summary>
/// Image quality (when doing lossy compression)
/// </summary>
public int Quality
{
get
get => _qualityOverride ?? GlobalSettingsFacade.ImageQuality;
set
{
if (QualityOverride.HasValue)
_qualityOverride = value;
if (_qualityOverride < 1)
{
_qualityOverride = 1;
}
if (_qualityOverride > 100)
{
return QualityOverride.Value;
_qualityOverride = 100;
}
return GlobalSettingsFacade.ImageQuality;
}
}
@@ -81,20 +78,10 @@ public int Quality
/// <summary>
/// Indicates whether any options were specified
/// </summary>
public bool IsEmpty
{
get
{
return Height == null && Width == null && MaxHeight == null && MaxWidth == null && QualityOverride == null;
}
}
public bool IsEmpty => Height == null && Width == null && MaxHeight == null && MaxWidth == null && _qualityOverride == null;
/// <exclude />
public ResizingOptions()
{
}
public ResizingOptions() { }
/// <summary>
/// Parses resizing options from query string collection
@@ -115,13 +102,9 @@ public static ResizingOptions Parse(HttpServerUtility httpServerUtility, NameVal
/// <returns>Resizing options</returns>
public static ResizingOptions Parse(NameValueCollection queryString)
{
string resizingKey = queryString["k"];
if (!string.IsNullOrEmpty(resizingKey))
{
return new ResizingOptions(resizingKey);
}
var resizingKey = queryString["k"];
return FromQueryString(queryString);
return string.IsNullOrEmpty(resizingKey) ? FromQueryString(queryString) : new ResizingOptions(resizingKey);
}
/// <exclude />
@@ -130,13 +113,13 @@ internal ResizingOptions(string predefinedOptionsName)
//Load the xml file
var options = GetPredefinedResizingOptions().Elements("image");
foreach (XElement e in options.Where(e => (string)e.Attribute("name") == predefinedOptionsName))
foreach (var e in options.Where(e => (string)e.Attribute("name") == predefinedOptionsName))
{
Height = ParseOptionalIntAttribute(e, "height");
Width = ParseOptionalIntAttribute(e, "width");
MaxHeight = ParseOptionalIntAttribute(e, "maxheight");
MaxWidth = ParseOptionalIntAttribute(e, "maxwidth");
QualityOverride = ParseOptionalIntAttribute(e, "quality");
_qualityOverride = ParseOptionalIntAttribute(e, "quality");
var attr = e.Attribute("action");
if (attr != null)
@@ -148,8 +131,9 @@ internal ResizingOptions(string predefinedOptionsName)
private static int? ParseOptionalIntAttribute(XElement element, string attributeName)
{
XAttribute attribute = element.Attribute(attributeName);
return attribute == null ? (int?) null : int.Parse(attribute.Value);
var attribute = element.Attribute(attributeName);
return attribute == null ? (int?)null : int.Parse(attribute.Value);
}
/// <summary>
@@ -161,7 +145,7 @@ private static ResizingOptions FromQueryString(NameValueCollection queryString)
{
var result = new ResizingOptions();
string str = queryString["w"];
var str = queryString["w"];
if (!string.IsNullOrEmpty(str))
{
result.Width = int.Parse(str);
@@ -188,31 +172,25 @@ private static ResizingOptions FromQueryString(NameValueCollection queryString)
str = queryString["q"];
if (!string.IsNullOrEmpty(str))
{
result.QualityOverride = int.Parse(str);
if (result.QualityOverride < 1) result.QualityOverride = 1;
if (result.QualityOverride > 100) result.QualityOverride = 100;
result.Quality = int.Parse(str);
}
ResizingAction resizingAction;
string action = queryString["action"];
if (!action.IsNullOrEmpty() && Enum.TryParse(action, true, out resizingAction))
var action = queryString["action"];
if (!string.IsNullOrEmpty(action) && Enum.TryParse(action, true, out ResizingAction resizingAction))
{
result.ResizingAction = resizingAction;
}
else
{
result.ResizingAction = Media.ResizingAction.Stretch;
result.ResizingAction = ResizingAction.Stretch;
}
return result;
}
private static XElement GetPredefinedResizingOptions()
{
XElement xel = HttpRuntime.Cache.Get("ResizedImageKeys") as XElement;
//If it's not there, load the xml document and then add it to the cache
if (xel == null)
if (!(HttpRuntime.Cache.Get("ResizedImageKeys") is XElement xel))
{
if (_resizedImageKeysFilePath == null)
{
@@ -221,7 +199,7 @@ private static XElement GetPredefinedResizingOptions()
if (!C1File.Exists(_resizedImageKeysFilePath))
{
string directoryPath = Path.GetDirectoryName(_resizedImageKeysFilePath);
var directoryPath = Path.GetDirectoryName(_resizedImageKeysFilePath);
if (!C1Directory.Exists(directoryPath)) C1Directory.CreateDirectory(directoryPath);
var config = new XElement("ResizedImages",
@@ -241,35 +219,37 @@ private static XElement GetPredefinedResizingOptions()
}
xel = XElementUtils.Load(_resizedImageKeysFilePath);
var cd = new CacheDependency(_resizedImageKeysFilePath);
var cacheExpirationTimeSpan = new TimeSpan(24, 0, 0);
HttpRuntime.Cache.Add("ResizedImageKeys", xel, cd, Cache.NoAbsoluteExpiration, cacheExpirationTimeSpan, CacheItemPriority.Default, null);
}
return xel;
}
/// <exclude />
override public string ToString()
public override string ToString()
{
var sb = new StringBuilder();
var parameters = new [] { Width, Height, MaxWidth, MaxHeight, QualityOverride };
var parameters = new[] { Width, Height, MaxWidth, MaxHeight, _qualityOverride };
var parameterNames = new[] { "w", "h", "mw", "mh", "q" };
for (int i = 0; i < parameters.Length; i++)
for (var i = 0; i < parameters.Length; i++)
{
if (parameters[i] != null)
if (parameters[i] == null)
{
sb.Append(sb.Length == 0 ? "" : "&");
sb.Append(parameterNames[i]).Append("=").Append((int)parameters[i]);
continue;
}
sb.Append(sb.Length == 0 ? String.Empty : "&");
sb.Append(parameterNames[i]).Append("=").Append((int)parameters[i]);
}
if (ResizingAction != ResizingAction.Stretch)
{
sb.Append(sb.Length == 0 ? "" : "&");
sb.Append(sb.Length == 0 ? String.Empty : "&");
sb.Append("action=").Append(ResizingAction.ToString().ToLowerInvariant());
}

0 comments on commit 0f037a5

Please sign in to comment.