diff --git a/Imagekit.UnitTests/ImageKitTestCasesNonAsync.cs b/Imagekit.UnitTests/ImageKitTestCasesNonAsync.cs index 7df6226f..1dd226cd 100644 --- a/Imagekit.UnitTests/ImageKitTestCasesNonAsync.cs +++ b/Imagekit.UnitTests/ImageKitTestCasesNonAsync.cs @@ -8,6 +8,7 @@ using System.Net.Http; using Xunit; using Newtonsoft.Json.Linq; +using Imagekit.Helper; namespace Imagekit.UnitTests.FileVersion { @@ -751,8 +752,4 @@ public void AuthenticationParamNullCheck() Assert.IsType(authParams.signature); } } -} - - - - +} \ No newline at end of file diff --git a/Imagekit/Helper/ImagekitBase.cs b/Imagekit/Helper/ImagekitBase.cs index ef673522..a3714be8 100644 --- a/Imagekit/Helper/ImagekitBase.cs +++ b/Imagekit/Helper/ImagekitBase.cs @@ -8,6 +8,7 @@ namespace Imagekit using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; + using global::Imagekit.Helper; [ExcludeFromCodeCoverage] public abstract partial class BaseImagekit @@ -80,4 +81,4 @@ public Imagekit( { } } -} +} \ No newline at end of file diff --git a/Imagekit/Helper/ImagekitParams.cs b/Imagekit/Helper/ImagekitParams.cs index 38b6f937..5065ec10 100644 --- a/Imagekit/Helper/ImagekitParams.cs +++ b/Imagekit/Helper/ImagekitParams.cs @@ -4,6 +4,8 @@ namespace Imagekit { + using global::Imagekit.Helper; + public partial class BaseImagekit { public T Path(string value) @@ -70,4 +72,4 @@ public T Add(string key, object value) return (T)this; } } -} +} \ No newline at end of file diff --git a/Imagekit/Helper/Transformation.cs b/Imagekit/Helper/Transformation.cs index cc5fa234..6d86c649 100644 --- a/Imagekit/Helper/Transformation.cs +++ b/Imagekit/Helper/Transformation.cs @@ -7,275 +7,278 @@ using System.Globalization; using System.Linq; -public partial class Transformation +namespace Imagekit.Helper { - public const string KeyNameRegex = "^\\$[a-zA-Z][a-zA-Z0-9]*$"; - public const string ChainTransformDelimiter = ":"; - public const string TransformDelimiter = ","; - public const string TransformKeyValueDelimiter = "-"; - - /// - /// Initializes a new instance of the class. - /// - /// - /// - public Transformation(Dictionary transformParams) + public partial class Transformation { - foreach (var key in transformParams.Keys) + public const string KeyNameRegex = "^\\$[a-zA-Z][a-zA-Z0-9]*$"; + public const string ChainTransformDelimiter = ":"; + public const string TransformDelimiter = ","; + public const string TransformKeyValueDelimiter = "-"; + + /// + /// Initializes a new instance of the class. + /// + /// + /// + public Transformation(Dictionary transformParams) { - transformParams.Add(key, transformParams[key]); - } - } - - /// - /// Initializes a new instance of the class. - /// Creates transformation object initialized with array of transformation parameters. - /// - /// List of transformation parameters represented as pairs 'name=value'. - public Transformation(params string[] transformParams) - { - foreach (var pair in transformParams) - { - string[] splittedPair = pair.Split('='); - if (splittedPair.Length != 2) + foreach (var key in transformParams.Keys) { - throw new Exception(string.Format("Couldn't parse '{0}'!", pair)); + transformParams.Add(key, transformParams[key]); } - - this.Add(splittedPair[0], splittedPair[1]); } - } - /// - /// Add transformation parameter. - /// - /// The name. - /// The value. - public Transformation Add(string key, object value) - { - if (this.transformParams.ContainsKey(key)) + /// + /// Initializes a new instance of the class. + /// Creates transformation object initialized with array of transformation parameters. + /// + /// List of transformation parameters represented as pairs 'name=value'. + public Transformation(params string[] transformParams) { - this.transformParams[key] = value; - } - else - { - this.transformParams.Add(key, value); - } - - return this; - } - - /// - /// Initializes a new instance of the class. - /// Creates empty transformation object. - /// - public Transformation() - { - } + foreach (var pair in transformParams) + { + string[] splittedPair = pair.Split('='); + if (splittedPair.Length != 2) + { + throw new Exception(string.Format("Couldn't parse '{0}'!", pair)); + } - /// - /// A dictionary of transformation parameters. - /// - protected Dictionary transformParams = new Dictionary(); - - /// - /// A list of nested transformations. - /// - protected List nestedTransforms = new List(); - - /// - /// Initializes a new instance of the class. - /// Creates transformation object chained with other transformations. - /// - /// List of transformations to chain with. - public Transformation(List transforms) - { - if (transforms != null) - { - this.nestedTransforms = transforms; + this.Add(splittedPair[0], splittedPair[1]); + } } - } - /// - /// Initializes a new instance of the class. - /// - /// - /// - public Transformation(Dictionary[] dictionary) - { - for (int i = 0; i < dictionary.Length; i++) + /// + /// Add transformation parameter. + /// + /// The name. + /// The value. + public Transformation Add(string key, object value) { - if (i == dictionary.Length - 1) + if (this.transformParams.ContainsKey(key)) { - this.transformParams = dictionary[i]; + this.transformParams[key] = value; } else { - this.nestedTransforms.Add(new Transformation(dictionary[i])); + this.transformParams.Add(key, value); } - } - } - - /// - /// Get the transformation parameters dictionary. - /// - public Dictionary Params - { - get { return this.transformParams; } - } - /// - /// Get list of nested transformations. - /// - public List NestedTransforms - { - get { return this.nestedTransforms; } - } - - /// - /// Chain transformation. - /// - public Transformation Chain() - { - Transformation nested = this.Clone(); - nested.nestedTransforms = null; - this.nestedTransforms.Add(nested); - this.transformParams = new Dictionary(); - Transformation transform = new Transformation(this.nestedTransforms); - return transform; - } - - /// - /// Get a deep cloned copy of this transformation. - /// - /// A deep cloned copy of this transformation. - public Transformation Clone() - { - Transformation t = (Transformation)this.MemberwiseClone(); - - t.transformParams = new Dictionary(); + return this; + } - foreach (var key in this.transformParams.Keys) + /// + /// Initializes a new instance of the class. + /// Creates empty transformation object. + /// + public Transformation() { - var value = this.transformParams[key]; + } - if (value is Array) - { - t.Add(key, ((Array)value).Clone()); - } - else if (value is string || value is ValueType) - { - t.Add(key, value); - } - else if (value is Dictionary) - { - t.Add(key, new Dictionary((Dictionary)value)); - } - else + /// + /// A dictionary of transformation parameters. + /// + protected Dictionary transformParams = new Dictionary(); + + /// + /// A list of nested transformations. + /// + protected List nestedTransforms = new List(); + + /// + /// Initializes a new instance of the class. + /// Creates transformation object chained with other transformations. + /// + /// List of transformations to chain with. + public Transformation(List transforms) + { + if (transforms != null) { - throw new Exception(string.Format("Couldn't clone parameter '{0}'!", key)); + this.nestedTransforms = transforms; } } - if (this.nestedTransforms != null) + /// + /// Initializes a new instance of the class. + /// + /// + /// + public Transformation(Dictionary[] dictionary) { - t.nestedTransforms = new List(); - foreach (var nestedTransform in this.nestedTransforms) + for (int i = 0; i < dictionary.Length; i++) { - t.nestedTransforms.Add(nestedTransform.Clone()); + if (i == dictionary.Length - 1) + { + this.transformParams = dictionary[i]; + } + else + { + this.nestedTransforms.Add(new Transformation(dictionary[i])); + } } } - return t; - } + /// + /// Get the transformation parameters dictionary. + /// + public Dictionary Params + { + get { return this.transformParams; } + } - /// - /// Get this transformation represented as string. - /// - /// The transformation represented as string. - public string Generate() - { - List parts = new List(this.nestedTransforms.Select(t => t.GetTrans()).ToList()); + /// + /// Get list of nested transformations. + /// + public List NestedTransforms + { + get { return this.nestedTransforms; } + } - var thisTransform = this.GetTrans(); - if (!string.IsNullOrEmpty(thisTransform)) + /// + /// Chain transformation. + /// + public Transformation Chain() { - parts.Add(thisTransform); + Transformation nested = this.Clone(); + nested.nestedTransforms = null; + this.nestedTransforms.Add(nested); + this.transformParams = new Dictionary(); + Transformation transform = new Transformation(this.nestedTransforms); + return transform; } - return string.Join(ChainTransformDelimiter, parts.ToArray()); - } + /// + /// Get a deep cloned copy of this transformation. + /// + /// A deep cloned copy of this transformation. + public Transformation Clone() + { + Transformation t = (Transformation) this.MemberwiseClone(); - public string GetTrans() - { - List transformations = new List(); - List varParams = new List(); + t.transformParams = new Dictionary(); - foreach (var key in this.transformParams.Keys) - { - string val = this.GetString(this.transformParams, key); - if (string.IsNullOrEmpty(val)) + foreach (var key in this.transformParams.Keys) { - varParams.Add($"{key}"); + var value = this.transformParams[key]; + + if (value is Array) + { + t.Add(key, ((Array) value).Clone()); + } + else if (value is string || value is ValueType) + { + t.Add(key, value); + } + else if (value is Dictionary) + { + t.Add(key, new Dictionary((Dictionary) value)); + } + else + { + throw new Exception(string.Format("Couldn't clone parameter '{0}'!", key)); + } } - else + + if (this.nestedTransforms != null) { - if (key == "oi" || key == "di") + t.nestedTransforms = new List(); + foreach (var nestedTransform in this.nestedTransforms) { - val = val.TrimStart('/').TrimEnd('/'); - val = val.Replace("/", "@@"); + t.nestedTransforms.Add(nestedTransform.Clone()); } - - varParams.Add($"{key}-{val}"); } + + return t; } - if (varParams.Count > 0) + /// + /// Get this transformation represented as string. + /// + /// The transformation represented as string. + public string Generate() { - transformations.Add(string.Join(TransformDelimiter, varParams)); - } + List parts = new List(this.nestedTransforms.Select(t => t.GetTrans()).ToList()); - return string.Join(ChainTransformDelimiter, transformations.ToArray()); - } + var thisTransform = this.GetTrans(); + if (!string.IsNullOrEmpty(thisTransform)) + { + parts.Add(thisTransform); + } - private string GetString(Dictionary options, string key) - { - if (options.ContainsKey(key)) - { - return ToString(options[key]); + return string.Join(ChainTransformDelimiter, parts.ToArray()); } - else + + public string GetTrans() { - return null; + List transformations = new List(); + List varParams = new List(); + + foreach (var key in this.transformParams.Keys) + { + string val = this.GetString(this.transformParams, key); + if (string.IsNullOrEmpty(val)) + { + varParams.Add($"{key}"); + } + else + { + if (key == "oi" || key == "di") + { + val = val.TrimStart('/').TrimEnd('/'); + val = val.Replace("/", "@@"); + } + + varParams.Add($"{key}-{val}"); + } + } + + if (varParams.Count > 0) + { + transformations.Add(string.Join(TransformDelimiter, varParams)); + } + + return string.Join(ChainTransformDelimiter, transformations.ToArray()); } - } - private static string ToString(object obj) - { - if (obj == null) + private string GetString(Dictionary options, string key) { - return null; + if (options.ContainsKey(key)) + { + return ToString(options[key]); + } + else + { + return null; + } } - if (obj is string) + private static string ToString(object obj) { - return obj.ToString(); + if (obj == null) + { + return null; + } + + if (obj is string) + { + return obj.ToString(); + } + + if (obj is float || obj is double) + { + return string.Format(CultureInfo.InvariantCulture, "{0:0.0#}", obj); + } + + return string.Format(CultureInfo.InvariantCulture, "{0}", obj); } - if (obj is float || obj is double) + /// + /// Get this transformation represented as string. + /// + /// The transformation represented as string. + public override string ToString() { - return string.Format(CultureInfo.InvariantCulture, "{0:0.0#}", obj); + return this.Generate(); } - - return string.Format(CultureInfo.InvariantCulture, "{0}", obj); - } - - /// - /// Get this transformation represented as string. - /// - /// The transformation represented as string. - public override string ToString() - { - return this.Generate(); } -} +} \ No newline at end of file diff --git a/Imagekit/Helper/TransformationTypes.cs b/Imagekit/Helper/TransformationTypes.cs index 30d3cb95..3d7582bf 100644 --- a/Imagekit/Helper/TransformationTypes.cs +++ b/Imagekit/Helper/TransformationTypes.cs @@ -5,427 +5,430 @@ using System; using System.Diagnostics.CodeAnalysis; -[ExcludeFromCodeCoverage] -public partial class Transformation +namespace Imagekit.Helper { - /// Width of a transformed image - /// - public Transformation Width(int value) - { - return this.Add("w", value); - } - - /// Height of a transformed image - /// - public Transformation Height(int value) - { - return this.Add("h", value); - } - - /// Aspect Ratio of a transformed image - /// - public Transformation AspectRatio(string value) - { - return this.Add("ar", value); - } - - /// - /// JPG compression quality. 1 is the lowest quality and 100 is the highest. The default is the - /// original image's quality or 80% if not available. - /// - /// - public Transformation Quality(int value) - { - return this.Add("q", value); - } - - /// Crop Transformation. - /// - public Transformation Crop(string value) - { - return this.Add("c", value); - } - - /// Crop Mode - /// - public Transformation CropMode(string value) - { - return this.Add("cm", value); - } - - /// - /// - public Transformation X(int value) - { - return this.Add("x", this.ConvertCoordinateParam(value)); - } - - /// - /// - public Transformation Y(int value) - { - return this.Add("y", this.ConvertCoordinateParam(value)); - } - - /// - /// - public Transformation Focus(string value) - { - return this.Add("fo", value); - } - - /// - /// - public Transformation Format(string value) - { - return this.Add("f", value); - } - - /// - /// - public Transformation Radius(object value) - { - return this.Add("r", value); - } - - /// - /// - public Transformation Background(string value) - { - return this.Add("bg", value); - } - - /// - /// - public Transformation Border(string value) - { - return this.Add("b", value); - } - - /// - /// - public Transformation Rotation(object value) - { - return this.Add("rt", value); - } - - /// - /// - public Transformation Rotate(object value) - { - return this.Add("rt", value); - } - - /// - /// - public Transformation Blur(int value) - { - return this.Add("bl", value); - } - - /// Add named transformation. - /// named transformation. - public Transformation Named(string value) - { - return this.Add("n", value); - } - - /// - /// - public Transformation OverlayImage(string value) - { - return this.Add("oi", value); - } - - /// - /// - public Transformation OverlayX(int value) - { - return this.Add("ox", this.ConvertCoordinateParam(value)); - } - - /// - /// - public Transformation OverlayY(int value) - { - return this.Add("oy", this.ConvertCoordinateParam(value)); - } - - /// - /// - public Transformation OverlayFocus(string value) - { - return this.Add("ofo", value); - } - - /// - /// - public Transformation OverlayHeight(int value) - { - return this.Add("oh", value); - } - - /// - /// - public Transformation OverlayWidth(int value) - { - return this.Add("ow", value); - } - - /// - /// - public Transformation OverlayText(string value) - { - return this.Add("ot", value); - } - - /// - /// - public Transformation OverlayTextFontSize(int value) - { - return this.Add("ots", value); - } - - /// - /// - public Transformation OverlayTextFontFamily(string value) - { - return this.Add("otf", value); - } - - /// - /// - public Transformation OverlayTextColor(string value) - { - return this.Add("otc", value); - } - - /// - /// - public Transformation OverylayAlpha(int value) - { - return this.Add("oa", value); - } - - /// - /// - public Transformation OverlayTextTypography(string value) - { - return this.Add("ott", value); - } - - /// - /// - public Transformation OverlayTextTransparency(int value) - { - return this.Add("oa", value); - } - - /// - /// - public Transformation OverlayTextBackground(string value) - { - return this.Add("otbg", value); - } - - /// - /// - public Transformation OverlayTextEncoded(string value) - { - return this.Add("ote", value); - } - - /// - /// - public Transformation OverlayTextWidth(int value) - { - return this.Add("otw", value); - } - - /// - /// - public Transformation OverlayTextPadding(int value) - { - return this.Add("otp", value); - } - - /// - /// - public Transformation OverlayTextInnerAlignment(string value) - { - return this.Add("otia", value); - } - - /// - /// - public Transformation OverlayRadius(int value) - { - return this.Add("or", value); - } - - /// - /// - public Transformation OverlayBackground(string value) - { - return this.Add("obg", value); - } - - /// - /// - public Transformation OverlayImageTrim(bool value) - { - return this.Add("oit", value.ToString().ToLower()); - } - - /// - /// - public Transformation OverlayImageAspectRatio(string value) - { - return this.Add("oiar", value); - } - - /// - /// - public Transformation OverlayImageBackground(string value) - { - return this.Add("oibg", value); - } - - /// - /// - public Transformation OverlayImageBorder(string value) - { - return this.Add("oib", value); - } - - /// - /// - public Transformation OverlayImageDpr(object value) - { - return this.Add("oidpr", value); - } - - /// - /// - public Transformation OverlayImageQuality(int value) - { - return this.Add("oiq", value); - } - - /// - /// - public Transformation OverlayImageCropping(string value) - { - return this.Add("oic", value); - } - - /// - /// - public Transformation Progressive(bool value) - { - return this.Add("pr", value.ToString().ToLower()); - } - - /// - /// - public Transformation Lossless(bool value) - { - return this.Add("lo", value.ToString().ToLower()); - } - - /// - /// - public Transformation Trim(int value) - { - return this.Add("t", value); - } - - /// - /// - public Transformation Metadata(bool value) - { - return this.Add("md", value.ToString().ToLower()); - } - - /// - /// - public Transformation ColorProfile(bool value) - { - return this.Add("cp", value.ToString().ToLower()); - } - - /// - /// - public Transformation DefaultImage(string value) - { - return this.Add("di", value); - } - - /// - /// - public Transformation Dpr(object value) - { - return this.Add("dpr", value); - } - - /// - public Transformation EffectSharpen() - { - return this.Add("e-sharpen", string.Empty); - } - - /// - /// - public Transformation EffectSharpen(int value) - { - return this.Add("e-sharpen", value); - } - - /// - /// - public Transformation EffectUsm(string value) - { - return this.Add("e-usm", value); - } - - /// - public Transformation EffectContrast() - { - return this.Add("e-contrast", string.Empty); - } - - /// - /// - public Transformation EffectContrast(object value) - { - return this.Add("e-contrast", value.ToString().ToLower()); - } - - /// - public Transformation EffectGray() - { - return this.Add("e-grayscale", "true"); - } - - /// - public Transformation Original() - { - return this.Add("orig", "true"); - } - - /// - /// Pass an raw transformation string (including chained transformations) - /// - /// A raw transformation string. - public Transformation Raw(string value) - { - return this.Add(value, string.Empty); - } - - private string ConvertCoordinateParam(int paramValue) - { - return paramValue < 0 - ? $"N{Math.Abs(paramValue)}" - : $"{paramValue}"; - } -} + [ExcludeFromCodeCoverage] + public partial class Transformation + { + /// Width of a transformed image + /// + public Transformation Width(int value) + { + return this.Add("w", value); + } + + /// Height of a transformed image + /// + public Transformation Height(int value) + { + return this.Add("h", value); + } + + /// Aspect Ratio of a transformed image + /// + public Transformation AspectRatio(string value) + { + return this.Add("ar", value); + } + + /// + /// JPG compression quality. 1 is the lowest quality and 100 is the highest. The default is the + /// original image's quality or 80% if not available. + /// + /// + public Transformation Quality(int value) + { + return this.Add("q", value); + } + + /// Crop Transformation. + /// + public Transformation Crop(string value) + { + return this.Add("c", value); + } + + /// Crop Mode + /// + public Transformation CropMode(string value) + { + return this.Add("cm", value); + } + + /// + /// + public Transformation X(int value) + { + return this.Add("x", this.ConvertCoordinateParam(value)); + } + + /// + /// + public Transformation Y(int value) + { + return this.Add("y", this.ConvertCoordinateParam(value)); + } + + /// + /// + public Transformation Focus(string value) + { + return this.Add("fo", value); + } + + /// + /// + public Transformation Format(string value) + { + return this.Add("f", value); + } + + /// + /// + public Transformation Radius(object value) + { + return this.Add("r", value); + } + + /// + /// + public Transformation Background(string value) + { + return this.Add("bg", value); + } + + /// + /// + public Transformation Border(string value) + { + return this.Add("b", value); + } + + /// + /// + public Transformation Rotation(object value) + { + return this.Add("rt", value); + } + + /// + /// + public Transformation Rotate(object value) + { + return this.Add("rt", value); + } + + /// + /// + public Transformation Blur(int value) + { + return this.Add("bl", value); + } + + /// Add named transformation. + /// named transformation. + public Transformation Named(string value) + { + return this.Add("n", value); + } + + /// + /// + public Transformation OverlayImage(string value) + { + return this.Add("oi", value); + } + + /// + /// + public Transformation OverlayX(int value) + { + return this.Add("ox", this.ConvertCoordinateParam(value)); + } + + /// + /// + public Transformation OverlayY(int value) + { + return this.Add("oy", this.ConvertCoordinateParam(value)); + } + + /// + /// + public Transformation OverlayFocus(string value) + { + return this.Add("ofo", value); + } + + /// + /// + public Transformation OverlayHeight(int value) + { + return this.Add("oh", value); + } + + /// + /// + public Transformation OverlayWidth(int value) + { + return this.Add("ow", value); + } + + /// + /// + public Transformation OverlayText(string value) + { + return this.Add("ot", value); + } + + /// + /// + public Transformation OverlayTextFontSize(int value) + { + return this.Add("ots", value); + } + + /// + /// + public Transformation OverlayTextFontFamily(string value) + { + return this.Add("otf", value); + } + + /// + /// + public Transformation OverlayTextColor(string value) + { + return this.Add("otc", value); + } + + /// + /// + public Transformation OverylayAlpha(int value) + { + return this.Add("oa", value); + } + + /// + /// + public Transformation OverlayTextTypography(string value) + { + return this.Add("ott", value); + } + + /// + /// + public Transformation OverlayTextTransparency(int value) + { + return this.Add("oa", value); + } + + /// + /// + public Transformation OverlayTextBackground(string value) + { + return this.Add("otbg", value); + } + + /// + /// + public Transformation OverlayTextEncoded(string value) + { + return this.Add("ote", value); + } + + /// + /// + public Transformation OverlayTextWidth(int value) + { + return this.Add("otw", value); + } + + /// + /// + public Transformation OverlayTextPadding(int value) + { + return this.Add("otp", value); + } + + /// + /// + public Transformation OverlayTextInnerAlignment(string value) + { + return this.Add("otia", value); + } + + /// + /// + public Transformation OverlayRadius(int value) + { + return this.Add("or", value); + } + + /// + /// + public Transformation OverlayBackground(string value) + { + return this.Add("obg", value); + } + + /// + /// + public Transformation OverlayImageTrim(bool value) + { + return this.Add("oit", value.ToString().ToLower()); + } + + /// + /// + public Transformation OverlayImageAspectRatio(string value) + { + return this.Add("oiar", value); + } + + /// + /// + public Transformation OverlayImageBackground(string value) + { + return this.Add("oibg", value); + } + + /// + /// + public Transformation OverlayImageBorder(string value) + { + return this.Add("oib", value); + } + + /// + /// + public Transformation OverlayImageDpr(object value) + { + return this.Add("oidpr", value); + } + + /// + /// + public Transformation OverlayImageQuality(int value) + { + return this.Add("oiq", value); + } + + /// + /// + public Transformation OverlayImageCropping(string value) + { + return this.Add("oic", value); + } + + /// + /// + public Transformation Progressive(bool value) + { + return this.Add("pr", value.ToString().ToLower()); + } + + /// + /// + public Transformation Lossless(bool value) + { + return this.Add("lo", value.ToString().ToLower()); + } + + /// + /// + public Transformation Trim(int value) + { + return this.Add("t", value); + } + + /// + /// + public Transformation Metadata(bool value) + { + return this.Add("md", value.ToString().ToLower()); + } + + /// + /// + public Transformation ColorProfile(bool value) + { + return this.Add("cp", value.ToString().ToLower()); + } + + /// + /// + public Transformation DefaultImage(string value) + { + return this.Add("di", value); + } + + /// + /// + public Transformation Dpr(object value) + { + return this.Add("dpr", value); + } + + /// + public Transformation EffectSharpen() + { + return this.Add("e-sharpen", string.Empty); + } + + /// + /// + public Transformation EffectSharpen(int value) + { + return this.Add("e-sharpen", value); + } + + /// + /// + public Transformation EffectUsm(string value) + { + return this.Add("e-usm", value); + } + + /// + public Transformation EffectContrast() + { + return this.Add("e-contrast", string.Empty); + } + + /// + /// + public Transformation EffectContrast(object value) + { + return this.Add("e-contrast", value.ToString().ToLower()); + } + + /// + public Transformation EffectGray() + { + return this.Add("e-grayscale", "true"); + } + + /// + public Transformation Original() + { + return this.Add("orig", "true"); + } + + /// + /// Pass an raw transformation string (including chained transformations) + /// + /// A raw transformation string. + public Transformation Raw(string value) + { + return this.Add(value, string.Empty); + } + + private string ConvertCoordinateParam(int paramValue) + { + return paramValue < 0 + ? $"N{Math.Abs(paramValue)}" + : $"{paramValue}"; + } + } +} \ No newline at end of file diff --git a/Imagekit/Helper/Url.cs b/Imagekit/Helper/Url.cs index fbf7b154..8228c13b 100644 --- a/Imagekit/Helper/Url.cs +++ b/Imagekit/Helper/Url.cs @@ -9,176 +9,184 @@ using System.Text.RegularExpressions; using global::Imagekit.Util; -[ExcludeFromCodeCoverage] -public class Url +namespace Imagekit.Helper { - public Dictionary options = new Dictionary(); - - private bool isSrcParameterUsedForUrl; - private Uri parsedUrl; - private Uri parsedHost; - - public Url(Dictionary client) + [ExcludeFromCodeCoverage] + public class Url { - this.options = client; - } + public Dictionary options = new Dictionary(); - public string UrlBuilder(string transformationString) - { - Dictionary urlObject = new Dictionary(); + private bool isSrcParameterUsedForUrl; + private Uri parsedUrl; + private Uri parsedHost; - if (this.options.ContainsKey("path") && this.options.ContainsKey("src")) + public Url(Dictionary client) { - throw new Exception("Either path or src is required."); + this.options = client; } - else if (this.options.ContainsKey("path")) - { - string path = this.AddLeadingSlash((string)this.options["path"]); - this.parsedUrl = new Uri((string)this.options["urlEndpoint"] + path); - this.parsedHost = new Uri((string)this.options["urlEndpoint"]); - urlObject.Add("protocol", this.parsedHost.Scheme); - urlObject.Add("host", this.options["urlEndpoint"].ToString().Replace(this.parsedHost.Scheme + "://", string.Empty)); - urlObject.Add("pathname", path.Split('?')[0]); - } - else if (this.options.ContainsKey("src")) - { - this.parsedUrl = new Uri((string)this.options["src"]); - this.isSrcParameterUsedForUrl = true; - urlObject.Add("protocol", this.parsedUrl.Scheme); - urlObject.Add("host", this.parsedUrl.Host); - urlObject.Add("pathname", this.parsedUrl.AbsolutePath); - } - else + + public string UrlBuilder(string transformationString) { - throw new Exception("Either path or src is required."); - } + Dictionary urlObject = new Dictionary(); + + if (this.options.ContainsKey("path") && this.options.ContainsKey("src")) + { + throw new Exception("Either path or src is required."); + } + else if (this.options.ContainsKey("path")) + { + string path = this.AddLeadingSlash((string) this.options["path"]); + this.parsedUrl = new Uri((string) this.options["urlEndpoint"] + path); + this.parsedHost = new Uri((string) this.options["urlEndpoint"]); + urlObject.Add("protocol", this.parsedHost.Scheme); + urlObject.Add("host", + this.options["urlEndpoint"].ToString().Replace(this.parsedHost.Scheme + "://", string.Empty)); + urlObject.Add("pathname", path.Split('?')[0]); + } + else if (this.options.ContainsKey("src")) + { + this.parsedUrl = new Uri((string) this.options["src"]); + this.isSrcParameterUsedForUrl = true; + urlObject.Add("protocol", this.parsedUrl.Scheme); + urlObject.Add("host", this.parsedUrl.Host); + urlObject.Add("pathname", this.parsedUrl.AbsolutePath); + } + else + { + throw new Exception("Either path or src is required."); + } - // Create correct query parameters - List queryParameters = new List(); + // Create correct query parameters + List queryParameters = new List(); - // Parse query params which are part of the URL - if (this.parsedUrl.Query != null && this.parsedUrl.Query.Length > 1) - { - string[] queryList = this.parsedUrl.Query.Split(new char[] { '&', '?' }); - foreach (var param in queryList) + // Parse query params which are part of the URL + if (this.parsedUrl.Query != null && this.parsedUrl.Query.Length > 1) { - if (string.IsNullOrEmpty(param)) + string[] queryList = this.parsedUrl.Query.Split(new char[] {'&', '?'}); + foreach (var param in queryList) { - continue; + if (string.IsNullOrEmpty(param)) + { + continue; + } + + int index = param.IndexOf('='); + if (index < 0) + { + continue; + } + + queryParameters.Add(this.GetParam(param.Substring(0, index), param.Substring(index + 1))); } + } - int index = param.IndexOf('='); - if (index < 0) + // parse param passed as queryParameters list + if (this.options.ContainsKey("queryParameters")) + { + foreach (var param in (string[]) this.options["queryParameters"]) { - continue; + if (string.IsNullOrEmpty(param)) + { + continue; + } + + int index = param.IndexOf('='); + if (index < 0) + { + throw new Exception(string.Format("Couldn't parse '{0}'!", param)); + } + + queryParameters.Add(this.GetParam(param.Substring(0, index), param.Substring(index + 1))); } - - queryParameters.Add(this.GetParam(param.Substring(0, index), param.Substring(index + 1))); } - } - // parse param passed as queryParameters list - if (this.options.ContainsKey("queryParameters")) - { - foreach (var param in (string[])this.options["queryParameters"]) + if (!string.IsNullOrEmpty(transformationString)) { - if (string.IsNullOrEmpty(param)) + if (this.isSrcParameterUsedForUrl || this.AddAsQueryParameter()) { - continue; + queryParameters.Add(this.GetParam(Constants.TransformationParameter, transformationString)); } - - int index = param.IndexOf('='); - if (index < 0) + else { - throw new Exception(string.Format("Couldn't parse '{0}'!", param)); + urlObject["pathname"] = "/tr:" + transformationString + urlObject["pathname"]; } - - queryParameters.Add(this.GetParam(param.Substring(0, index), param.Substring(index + 1))); } - } - if (!string.IsNullOrEmpty(transformationString)) - { - if (this.isSrcParameterUsedForUrl || this.AddAsQueryParameter()) + urlObject["host"] = this.RemoveTrailingSlash(urlObject["host"]); + + if (queryParameters.Count > 0) { - queryParameters.Add(this.GetParam(Constants.TransformationParameter, transformationString)); + urlObject["query"] = string.Join("&", queryParameters); } - else + + if (this.options.ContainsKey("signed") && this.options["signed"].Equals(true)) { - urlObject["pathname"] = "/tr:" + transformationString + urlObject["pathname"]; - } - } + string expiryTimestamp = this.options.ContainsKey("expireSeconds") + ? Utils.GetSignatureTimestamp((int) this.options["expireSeconds"]) + : Constants.DefaultTimestamp; + if (expiryTimestamp != Constants.DefaultTimestamp) + { + queryParameters.Add(this.GetParam(Constants.TimestampParameter, expiryTimestamp)); + } - urlObject["host"] = this.RemoveTrailingSlash(urlObject["host"]); + string intermediateUrl = this.GenrateUrl(urlObject); + queryParameters.Add(this.GetParam(Constants.SignatureParameter, + this.GetSignature(intermediateUrl, expiryTimestamp))); - if (queryParameters.Count > 0) - { - urlObject["query"] = string.Join("&", queryParameters); + if (queryParameters.Count > 0) + { + urlObject["query"] = string.Join("&", queryParameters); + } + } + + return this.GenrateUrl(urlObject); } - if (this.options.ContainsKey("signed") && this.options["signed"].Equals(true)) + public string GenrateUrl(Dictionary urlObject) { - string expiryTimestamp = this.options.ContainsKey("expireSeconds") ? Utils.GetSignatureTimestamp((int)this.options["expireSeconds"]) : Constants.DefaultTimestamp; - if (expiryTimestamp != Constants.DefaultTimestamp) + if (urlObject.ContainsKey("query")) { - queryParameters.Add(this.GetParam(Constants.TimestampParameter, expiryTimestamp)); + return urlObject["protocol"] + "://" + urlObject["host"] + urlObject["pathname"] + "?" + + urlObject["query"]; } - - string intermediateUrl = this.GenrateUrl(urlObject); - queryParameters.Add(this.GetParam(Constants.SignatureParameter, this.GetSignature(intermediateUrl, expiryTimestamp))); - - if (queryParameters.Count > 0) + else { - urlObject["query"] = string.Join("&", queryParameters); + return urlObject["protocol"] + "://" + urlObject["host"] + urlObject["pathname"]; } } - return this.GenrateUrl(urlObject); - } - - public string GenrateUrl(Dictionary urlObject) - { - if (urlObject.ContainsKey("query")) + public string RemoveTrailingSlash(string str) { - return urlObject["protocol"] + "://" + urlObject["host"] + urlObject["pathname"] + "?" + urlObject["query"]; + return str.TrimEnd(new[] {'/'}); } - else + + public string AddLeadingSlash(string str) { - return urlObject["protocol"] + "://" + urlObject["host"] + urlObject["pathname"]; + str = str.TrimStart('/'); + str = "/" + str; + return str; } - } - public string RemoveTrailingSlash(string str) - { - return str.TrimEnd(new[] { '/' }); - } + public string GetParam(string key, string param) + { + return $"{Uri.EscapeDataString(key)}={Uri.EscapeDataString(param)}"; + } - public string AddLeadingSlash(string str) - { - str = str.TrimStart('/'); - str = "/" + str; - return str; - } + public bool AddAsQueryParameter() + { + if (this.options["transformationPosition"].ToString() == "query") + { + return true; + } - public string GetParam(string key, string param) - { - return $"{Uri.EscapeDataString(key)}={Uri.EscapeDataString(param)}"; - } + return false; + } - public bool AddAsQueryParameter() - { - if (this.options["transformationPosition"].ToString() == "query") + public string GetSignature(string url, string expiryTimestamp) { - return true; + var endPoint = this.RemoveTrailingSlash((string) this.options["urlEndpoint"]); + string str = Regex.Replace(url, endPoint + "/", string.Empty) + expiryTimestamp; + return Utils.CalculateSignature(str, Encoding.ASCII.GetBytes((string) this.options["privateKey"])); } - - return false; - } - - public string GetSignature(string url, string expiryTimestamp) - { - var endPoint = this.RemoveTrailingSlash((string)this.options["urlEndpoint"]); - string str = Regex.Replace(url, endPoint + "/", string.Empty) + expiryTimestamp; - return Utils.CalculateSignature(str, Encoding.ASCII.GetBytes((string)this.options["privateKey"])); } -} +} \ No newline at end of file