diff --git a/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs b/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs index c2940e3e2..1b0ed5952 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs @@ -1,9 +1,13 @@ -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- // -// Copyright (c) James South. -// Licensed under the Apache License, Version 2.0. +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. // -// ----------------------------------------------------------------------- +// +// Represents an image processing section within a configuration file. +// Nested syntax adapted from +// +// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Config { @@ -19,6 +23,21 @@ namespace ImageProcessor.Web.Config public sealed class ImageProcessingSection : ConfigurationSection { #region Properties + /// + /// Gets the . + /// + /// + /// The . + /// + [ConfigurationProperty("presets", IsRequired = true)] + public PresetElementCollection Presets + { + get + { + return this["presets"] as PresetElementCollection; + } + } + /// /// Gets the . /// @@ -54,6 +73,115 @@ namespace ImageProcessor.Web.Config } #endregion + /// + /// Represents a PresetElement configuration element within the configuration. + /// + public class PresetElement : ConfigurationElement + { + /// + /// Gets or sets the name of the preset. + /// + /// The name of the plugin. + [ConfigurationProperty("name", DefaultValue = "", IsRequired = true)] + public string Name + { + get { return (string)this["name"]; } + + set { this["name"] = value; } + } + + /// + /// Gets or sets the value of the preset. + /// + /// The full Type definition of the plugin + [ConfigurationProperty("value", DefaultValue = "", IsRequired = true)] + public string Value + { + get { return (string)this["value"]; } + + set { this["value"] = value; } + } + } + + /// + /// Represents a PresetElementCollection collection configuration element within the configuration. + /// + public class PresetElementCollection : ConfigurationElementCollection + { + /// + /// Gets the type of the . + /// + /// + /// The of this collection. + /// + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMap; } + } + + /// + /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. + /// + /// + /// The name of the collection; otherwise, an empty string. The default is an empty string. + /// + protected override string ElementName + { + get { return "preset"; } + } + + /// + /// Gets or sets the + /// at the specified index within the collection. + /// + /// The index at which to get the specified object. + /// + /// The the + /// at the specified index within the collection. + /// + public PresetElement this[int index] + { + get + { + return (PresetElement)BaseGet(index); + } + + set + { + if (this.BaseGet(index) != null) + { + this.BaseRemoveAt(index); + } + + this.BaseAdd(index, value); + } + } + + /// + /// Creates a new Preset configuration element. + /// + /// + /// A new PluginConfig configuration element. + /// + protected override ConfigurationElement CreateNewElement() + { + return new PresetElement(); + } + + /// + /// Gets the element key for a specified PluginElement configuration element. + /// + /// + /// The ConfigurationElement + /// to return the key for. + /// + /// The element key for a specified PluginElement configuration element. + protected override object GetElementKey(ConfigurationElement element) + { + return ((PresetElement)element).Name; + } + } + /// /// Represents a PluginElement configuration element within the configuration. /// @@ -104,6 +232,19 @@ namespace ImageProcessor.Web.Config /// public class PluginElementCollection : ConfigurationElementCollection { + /// + /// Gets or sets a value indicating whether to auto load all plugins. + /// Defaults to True. + /// + /// If True plugins are auto discovered and loaded from all assemblies otherwise they must be defined in the configuration file + [ConfigurationProperty("autoLoadPlugins", DefaultValue = true, IsRequired = false)] + public bool AutoLoadPlugins + { + get { return (bool)this["autoLoadPlugins"]; } + + set { this["autoLoadPlugins"] = value; } + } + /// /// Gets the type of the . /// @@ -126,18 +267,6 @@ namespace ImageProcessor.Web.Config get { return "plugin"; } } - /// - /// Gets or sets the autoLoadPlugins of the plugin file. - /// - /// If True plugins are auto discovered and loaded from all assemblies otherwise they must be defined in the configuration file - [ConfigurationProperty("autoLoadPlugins", DefaultValue = true, IsRequired = false)] - public bool AutoLoadPlugins - { - get { return (bool)this["autoLoadPlugins"]; } - - set { this["autoLoadPlugins"] = value; } - } - /// /// Gets or sets the /// at the specified index within the collection. @@ -161,15 +290,15 @@ namespace ImageProcessor.Web.Config this.BaseRemoveAt(index); } - this.BaseAdd(index, value); + this.BaseAdd(index, value); } } /// - /// Creates a new PluginConfig configuration element. + /// Creates a new Plugin configuration element. /// /// - /// A new PluginConfig configuration element. + /// A new Plugin configuration element. /// protected override ConfigurationElement CreateNewElement() { @@ -279,10 +408,10 @@ namespace ImageProcessor.Web.Config { if (this.BaseGet(index) != null) { - this.BaseRemoveAt(index); + this.BaseRemoveAt(index); } - this.BaseAdd(index, value); + this.BaseAdd(index, value); } } diff --git a/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs b/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs index baa524e4e..dcaf4f428 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs @@ -8,9 +8,6 @@ // // // -------------------------------------------------------------------------------------------------------------------- - -using System.Collections; - namespace ImageProcessor.Web.Config { #region Using @@ -45,6 +42,12 @@ namespace ImageProcessor.Web.Config private static readonly Dictionary> PluginSettings = new Dictionary>(); + /// + /// A collection of the processing presets defined in the configuration. + /// for available plugins. + /// + private static readonly Dictionary PresetSettings = new Dictionary(); + /// /// The processing configuration section from the current application configuration. /// @@ -183,6 +186,36 @@ namespace ImageProcessor.Web.Config #endregion #region Methods + /// + /// Returns the collection of the processing presets defined in the configuration. + /// + /// + /// The name of the plugin to get the settings for. + /// + /// + /// The containing the processing presets defined in the configuration. + /// + public string GetPresetSettings(string name) + { + if (!PresetSettings.ContainsKey(name)) + { + var presetElement = + GetImageProcessingSection().Presets + .Cast() + .FirstOrDefault(x => x.Name == name); + + if (presetElement != null) + { + PresetSettings.Add(presetElement.Name, presetElement.Value); + } + } + + string preset; + PresetSettings.TryGetValue(name, out preset); + + return preset; + } + /// /// Returns the for the given plugin. /// @@ -255,12 +288,12 @@ namespace ImageProcessor.Web.Config { if (this.GraphicsProcessors == null) { - if (GetImageProcessingSection().Plugins.AutoLoadPlugins) + try { - try + if (GetImageProcessingSection().Plugins.AutoLoadPlugins) { // Build a list of native IGraphicsProcessor instances. - Type type = typeof (IGraphicsProcessor); + Type type = typeof(IGraphicsProcessor); IEnumerable types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) @@ -277,51 +310,52 @@ namespace ImageProcessor.Web.Config processor.Settings = this.GetPluginSettings(processor.GetType().Name); } } - catch (ReflectionTypeLoadException ex) + else { - StringBuilder sb = new StringBuilder(); - foreach (Exception exception in ex.LoaderExceptions) + ImageProcessingSection.PluginElementCollection pluginConfigs = imageProcessingSection.Plugins; + this.GraphicsProcessors = new List(); + foreach (ImageProcessingSection.PluginElement pluginConfig in pluginConfigs) { - sb.AppendLine(exception.Message); - if (exception is FileNotFoundException) + Type type = Type.GetType(pluginConfig.Type); + + if (type == null) { - FileNotFoundException fileNotFoundException = exception as FileNotFoundException; - if (!string.IsNullOrEmpty(fileNotFoundException.FusionLog)) - { - sb.AppendLine("Fusion Log:"); - sb.AppendLine(fileNotFoundException.FusionLog); - } + throw new ArgumentException("Couldn't load IGraphicsProcessor: " + pluginConfig.Type); } - sb.AppendLine(); + this.GraphicsProcessors.Add(Activator.CreateInstance(type) as IGraphicsProcessor); } - string errorMessage = sb.ToString(); - - // Display or log the error based on your application. - throw new Exception(errorMessage); + // Add the available settings. + foreach (IGraphicsProcessor processor in this.GraphicsProcessors) + { + processor.Settings = this.GetPluginSettings(processor.GetType().Name); + } } } - else + catch (ReflectionTypeLoadException ex) { - var pluginConfigs = imageProcessingSection.Plugins; - this.GraphicsProcessors = new List(); - foreach (ImageProcessingSection.PluginElement pluginConfig in pluginConfigs) + StringBuilder stringBuilder = new StringBuilder(); + foreach (Exception exception in ex.LoaderExceptions) { - var type = Type.GetType(pluginConfig.Type); - - if (type == null) + stringBuilder.AppendLine(exception.Message); + if (exception is FileNotFoundException) { - throw new ArgumentException("Couldn't load IGraphicsProcessor: " + pluginConfig.Type); + FileNotFoundException fileNotFoundException = exception as FileNotFoundException; + if (!string.IsNullOrEmpty(fileNotFoundException.FusionLog)) + { + stringBuilder.AppendLine("Fusion Log:"); + stringBuilder.AppendLine(fileNotFoundException.FusionLog); + } } - this.GraphicsProcessors.Add((Activator.CreateInstance(type) as IGraphicsProcessor)); - } - // Add the available settings. - foreach (IGraphicsProcessor processor in this.GraphicsProcessors) - { - processor.Settings = this.GetPluginSettings(processor.GetType().Name); + stringBuilder.AppendLine(); } + + string errorMessage = stringBuilder.ToString(); + + // Display or log the error based on your application. + throw new Exception(errorMessage); } } } diff --git a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs index d98447302..ae4700ba2 100644 --- a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs +++ b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs @@ -542,17 +542,14 @@ namespace ImageProcessor.Web.HttpModules /// private string ReplacePresetsInQueryString(string queryString) { - // We use the processor config system to store the preset values. - Dictionary presets = ImageProcessorConfig.Instance.GetPluginSettings("Preset"); - foreach (Match match in PresetRegex.Matches(queryString)) { if (match.Success) { - // Set the index on the first instance only. - string preset = match.Value; - string replacements; - presets.TryGetValue(preset.Split('=')[1], out replacements); + string preset = match.Value.Split('=')[1]; + + // We use the processor config system to store the preset values. + string replacements = ImageProcessorConfig.Instance.GetPresetSettings(preset); queryString = Regex.Replace(queryString, preset, replacements ?? string.Empty); } } diff --git a/src/TestWebsites/NET4/config/imageprocessor/processing.config b/src/TestWebsites/NET4/config/imageprocessor/processing.config index 787d0214c..0479642b2 100644 --- a/src/TestWebsites/NET4/config/imageprocessor/processing.config +++ b/src/TestWebsites/NET4/config/imageprocessor/processing.config @@ -1,17 +1,42 @@  - - + + + + + + + + + + + + - - + + + - + - + + + + + + + + + + + + + + + diff --git a/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config b/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config index 9b0aa321f..0479642b2 100644 --- a/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config +++ b/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config @@ -1,5 +1,8 @@  + + + @@ -34,11 +37,6 @@ - - - - - diff --git a/src/TestWebsites/NET45/Test_Website_Webforms_NET45/config/imageprocessor/processing.config b/src/TestWebsites/NET45/Test_Website_Webforms_NET45/config/imageprocessor/processing.config index fc33547f6..0479642b2 100644 --- a/src/TestWebsites/NET45/Test_Website_Webforms_NET45/config/imageprocessor/processing.config +++ b/src/TestWebsites/NET45/Test_Website_Webforms_NET45/config/imageprocessor/processing.config @@ -1,31 +1,42 @@  - - - - - - - - + + + + + + + + + + + + - + - + + - + + + + + + +