@ -36,13 +36,6 @@ namespace ImageProcessor.Web.Configuration
private static readonly Lazy < ImageProcessorConfiguration > Lazy =
new Lazy < ImageProcessorConfiguration > ( ( ) = > new ImageProcessorConfiguration ( ) ) ;
/// <summary>
/// A collection of the <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> elements
/// for available plugins.
/// </summary>
private static readonly ConcurrentDictionary < string , Dictionary < string , string > > PluginSettings =
new ConcurrentDictionary < string , Dictionary < string , string > > ( ) ;
/// <summary>
/// A collection of the processing presets defined in the configuration.
/// for available plugins.
@ -134,74 +127,6 @@ namespace ImageProcessor.Web.Configuration
}
}
#endregion
#region Security
/// <summary>
/// Gets a list of white listed url[s] that images can be downloaded from.
/// </summary>
public Uri [ ] RemoteFileWhiteList
{
get
{
return GetImageSecuritySection ( ) . ImageServices . Cast < ImageSecuritySection . SafeUrl > ( ) . Select ( x = > x . Url ) . ToArray ( ) ;
}
}
/// <summary>
/// Gets a list of image extensions for url[s] with no extension.
/// </summary>
public ImageSecuritySection . SafeUrl [ ] RemoteFileWhiteListExtensions
{
get
{
return GetImageSecuritySection ( ) . ImageServices . Cast < ImageSecuritySection . SafeUrl > ( ) . ToArray ( ) ;
}
}
/// <summary>
/// Gets a value indicating whether the current application is allowed to download remote files.
/// </summary>
public bool AllowRemoteDownloads
{
get
{
return GetImageSecuritySection ( ) . AllowRemoteDownloads ;
}
}
/// <summary>
/// Gets the maximum length to wait in milliseconds before throwing an error requesting a remote file.
/// </summary>
public int Timeout
{
get
{
return GetImageSecuritySection ( ) . Timeout ;
}
}
/// <summary>
/// Gets the maximum allowable size in bytes of e remote file to process.
/// </summary>
public int MaxBytes
{
get
{
return GetImageSecuritySection ( ) . MaxBytes ;
}
}
/// <summary>
/// Gets the remote prefix for external files for the application.
/// </summary>
public string RemotePrefix
{
get
{
return GetImageSecuritySection ( ) . RemotePrefix ;
}
}
#endregion
#endregion
#region Methods
@ -228,80 +153,6 @@ namespace ImageProcessor.Web.Configuration
} ) ;
}
/// <summary>
/// Returns the <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </summary>
/// <param name="name">
/// The name of the plugin to get the settings for.
/// </param>
/// <returns>
/// The <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </returns>
public Dictionary < string , string > GetPluginSettings ( string name )
{
return PluginSettings . GetOrAdd (
name ,
n = >
{
ImageProcessingSection . PluginElement pluginElement = GetImageProcessingSection ( )
. Plugins
. Cast < ImageProcessingSection . PluginElement > ( )
. FirstOrDefault ( x = > x . Name = = n ) ;
Dictionary < string , string > settings ;
if ( pluginElement ! = null )
{
settings = pluginElement . Settings
. Cast < ImageProcessingSection . SettingElement > ( )
. ToDictionary ( setting = > setting . Key , setting = > setting . Value ) ;
}
else
{
settings = new Dictionary < string , string > ( ) ;
}
return settings ;
} ) ;
}
/// <summary>
/// Returns the <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </summary>
/// <param name="name">
/// The name of the plugin to get the settings for.
/// </param>
/// <returns>
/// The <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </returns>
public Dictionary < string , string > GetServiceSettings ( string name )
{
return PluginSettings . GetOrAdd (
name ,
n = >
{
ImageSecuritySection . ServiceElement pluginElement = GetImageSecuritySection ( )
. ImageServices
. Cast < ImageSecuritySection . ServiceElement > ( )
. FirstOrDefault ( x = > x . Name = = n ) ;
Dictionary < string , string > settings ;
if ( pluginElement ! = null )
{
settings = pluginElement . Settings
. Cast < ImageProcessingSection . SettingElement > ( )
. ToDictionary ( setting = > setting . Key , setting = > setting . Value ) ;
}
else
{
settings = new Dictionary < string , string > ( ) ;
}
return settings ;
} ) ;
}
/// <summary>
/// Retrieves the processing configuration section from the current application configuration.
/// </summary>
@ -329,6 +180,7 @@ namespace ImageProcessor.Web.Configuration
return imageSecuritySection ? ? ( imageSecuritySection = ImageSecuritySection . GetConfiguration ( ) ) ;
}
#region GraphicesProcessors
/// <summary>
/// Gets the list of available GraphicsProcessors.
/// </summary>
@ -369,6 +221,69 @@ namespace ImageProcessor.Web.Configuration
}
}
/// <summary>
/// Loads graphics processors from configuration.
/// </summary>
/// <exception cref="TypeLoadException">
/// Thrown when an <see cref="IWebGraphicsProcessor"/> cannot be loaded.
/// </exception>
private void LoadGraphicsProcessorsFromConfiguration ( )
{
ImageProcessingSection . PluginElementCollection pluginConfigs = imageProcessingSection . Plugins ;
this . GraphicsProcessors = new List < IWebGraphicsProcessor > ( ) ;
foreach ( ImageProcessingSection . PluginElement pluginConfig in pluginConfigs )
{
Type type = Type . GetType ( pluginConfig . Type ) ;
if ( type = = null )
{
throw new TypeLoadException ( "Couldn't load IWebGraphicsProcessor: " + pluginConfig . Type ) ;
}
this . GraphicsProcessors . Add ( Activator . CreateInstance ( type ) as IWebGraphicsProcessor ) ;
}
// Add the available settings.
foreach ( IWebGraphicsProcessor webProcessor in this . GraphicsProcessors )
{
webProcessor . Processor . Settings = this . GetPluginSettings ( webProcessor . GetType ( ) . Name ) ;
}
}
/// <summary>
/// Returns the <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </summary>
/// <param name="name">
/// The name of the plugin to get the settings for.
/// </param>
/// <returns>
/// The <see cref="T:ImageProcessor.Web.Config.ImageProcessingSection.SettingElementCollection"/> for the given plugin.
/// </returns>
private Dictionary < string , string > GetPluginSettings ( string name )
{
ImageProcessingSection . PluginElement pluginElement = GetImageProcessingSection ( )
. Plugins
. Cast < ImageProcessingSection . PluginElement > ( )
. FirstOrDefault ( x = > x . Name = = name ) ;
Dictionary < string , string > settings ;
if ( pluginElement ! = null )
{
settings = pluginElement . Settings
. Cast < ImageProcessingSection . SettingElement > ( )
. ToDictionary ( setting = > setting . Key , setting = > setting . Value ) ;
}
else
{
settings = new Dictionary < string , string > ( ) ;
}
return settings ;
}
#endregion
#region ImageServices
/// <summary>
/// Gets the list of available ImageServices.
/// </summary>
@ -392,9 +307,11 @@ namespace ImageProcessor.Web.Configuration
this . ImageServices = availableTypes . Select ( x = > ( Activator . CreateInstance ( x ) as IImageService ) ) . ToList ( ) ;
// Add the available settings.
foreach ( IImageService imageS ervice in this . ImageServices )
foreach ( IImageService s ervice in this . ImageServices )
{
imageService . Settings = this . GetServiceSettings ( imageService . GetType ( ) . Name ) ;
string name = service . GetType ( ) . Name ;
service . Settings = this . GetServiceSettings ( name ) ;
service . WhiteList = this . GetServiceWhitelist ( name ) ;
}
}
catch ( ReflectionTypeLoadException )
@ -410,62 +327,94 @@ namespace ImageProcessor.Web.Configuration
}
/// <summary>
/// Loads graphics processor s from configuration.
/// Loads image service s from configuration.
/// </summary>
/// <exception cref="TypeLoadException">
/// Thrown when an <see cref="IWeb GraphicsProcessor"/> cannot be loaded.
/// Thrown when an <see cref="IGraphicsProcessor"/> cannot be loaded.
/// </exception>
private void LoadGraphicsProcessor sFromConfiguration ( )
private void LoadImageService sFromConfiguration ( )
{
ImageProcessingSection . PluginElementCollection pluginConfigs = imageProcessingSection . Plugin s ;
this . GraphicsProcessor s = new List < IWebGraphicsProcessor > ( ) ;
foreach ( ImageProcessingSection . PluginElement pluginConfig in pluginConfig s )
ImageSecuritySection . ServiceElementCollection services = imageSecuritySection . ImageService s ;
this . ImageService s = new List < IImageService > ( ) ;
foreach ( ImageSecuritySection . ServiceElement config in service s )
{
Type type = Type . GetType ( pluginC onfig. Type ) ;
Type type = Type . GetType ( c onfig. Type ) ;
if ( type = = null )
{
throw new TypeLoadException ( "Couldn't load IWebGraphicsProcessor: " + pluginC onfig . Type ) ;
throw new TypeLoadException ( "Couldn't load IImageService: " + c onfig . Type ) ;
}
this . GraphicsProcessors . Add ( Activator . CreateInstance ( type ) as IWebGraphicsProcessor ) ;
}
// Add the available settings.
foreach ( IWebGraphicsProcessor webProcessor in this . GraphicsProcessor s)
foreach ( IImageService service in this . ImageService s)
{
webProcessor . Processor . Settings = this . GetPluginSettings ( webProcessor . GetType ( ) . Name ) ;
string name = service . GetType ( ) . Name ;
service . Settings = this . GetServiceSettings ( name ) ;
service . WhiteList = this . GetServiceWhitelist ( name ) ;
}
}
/// <summary>
/// Loads image services from configuratio n.
/// Returns the <see cref="T:ImageProcessor.Web.Config.ImageSecuritySection.SettingElementCollection"/> for the given plugi n.
/// </summary>
/// <exception cref="TypeLoadException">
/// Thrown when an <see cref="IGraphicsProcessor"/> cannot be loaded.
/// </exception>
private void LoadImageServicesFromConfiguration ( )
/// <param name="name">
/// The name of the plugin to get the settings for.
/// </param>
/// <returns>
/// The <see cref="T:ImageProcessor.Web.Config.ImageSecuritySection.SettingElementCollection"/> for the given plugin.
/// </returns>
private Dictionary < string , string > GetServiceSettings ( string name )
{
ImageSecuritySection . ServiceElementCollection services = imageSecuritySection . ImageServices ;
this . ImageServices = new List < IImageService > ( ) ;
foreach ( ImageSecuritySection . ServiceElement config in services )
{
Type type = Type . GetType ( config . Type ) ;
ImageSecuritySection . ServiceElement serviceElement = GetImageSecuritySection ( )
. ImageServices
. Cast < ImageSecuritySection . ServiceElement > ( )
. FirstOrDefault ( x = > x . Name = = name ) ;
if ( type = = null )
{
throw new TypeLoadException ( "Couldn't load IImageService: " + config . Type ) ;
}
Dictionary < string , string > settings ;
this . GraphicsProcessors . Add ( Activator . CreateInstance ( type ) as IWebGraphicsProcessor ) ;
if ( serviceElement ! = null )
{
settings = serviceElement . Settings
. Cast < ImageSecuritySection . SettingElement > ( )
. ToDictionary ( setting = > setting . Key , setting = > setting . Value ) ;
}
else
{
settings = new Dictionary < string , string > ( ) ;
}
// Add the available settings.
foreach ( IImageService service in this . ImageServices )
return settings ;
}
/// <summary>
/// Gets the whitelist of <see cref="System.Uri"/> for the given service.
/// </summary>
/// <param name="name">
/// The name of the service to return the whitelist for.
/// </param>
/// <returns>
/// The <see cref="System.Uri"/> array containing the whitelist.
/// </returns>
private Uri [ ] GetServiceWhitelist ( string name )
{
ImageSecuritySection . ServiceElement serviceElement = GetImageSecuritySection ( )
. ImageServices
. Cast < ImageSecuritySection . ServiceElement > ( )
. FirstOrDefault ( x = > x . Name = = name ) ;
Uri [ ] whitelist = { } ;
if ( serviceElement ! = null )
{
service . Settings = this . GetServiceSettings ( service . GetType ( ) . Name ) ;
whitelist = serviceElement . WhiteList . Cast < ImageSecuritySection . SafeUrl > ( )
. Select ( s = > s . Url ) . ToArray ( ) ;
}
return whitelist ;
}
#endregion
#endregion
}
}