Permalink
Browse files

Resharping preparing to make changes to the ResizingOptions class

  • Loading branch information...
burningice2866 committed Nov 16, 2017
1 parent 4afc17e commit 16852ffc03ea5694ae04f942ca8731c932bb4f2d
Showing with 37 additions and 62 deletions.
  1. +37 −62 Composite/Core/WebClient/Media/ResizingOptions.cs
@@ -8,16 +8,13 @@
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";
@@ -51,27 +48,12 @@ public class ResizingOptions
/// <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
{
if (QualityOverride.HasValue)
{
return QualityOverride.Value;
}
return GlobalSettingsFacade.ImageQuality;
}
}
public int Quality => QualityOverride ?? GlobalSettingsFacade.ImageQuality;
/// <summary>
/// Resizing action
@@ -81,20 +63,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 +87,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,7 +98,7 @@ 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");
@@ -148,8 +116,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 +130,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);
@@ -189,30 +158,34 @@ private static ResizingOptions FromQueryString(NameValueCollection queryString)
if (!string.IsNullOrEmpty(str))
{
result.QualityOverride = int.Parse(str);
if (result.QualityOverride < 1) result.QualityOverride = 1;
if (result.QualityOverride > 100) result.QualityOverride = 100;
if (result.QualityOverride < 1)
{
result.QualityOverride = 1;
}
if (result.QualityOverride > 100)
{
result.QualityOverride = 100;
}
}
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 +194,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 +214,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 16852ff

Please sign in to comment.