// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Represents an image cache section within a configuration file. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Configuration { using System.Configuration; using System.IO; using System.Xml; using ImageProcessor.Web.Extensions; using ImageProcessor.Web.Helpers; /// /// Represents an image cache section within a configuration file. /// public sealed class ImageCacheSection : ConfigurationSection { /// /// Gets or sets the virtual path of the cache folder. /// /// The name of the cache folder. [ConfigurationProperty("virtualPath", DefaultValue = "~/app_data/cache", IsRequired = true)] [StringValidator(MinLength = 3, MaxLength = 256)] public string VirtualPath { get { string virtualPath = (string)this["virtualPath"]; return virtualPath.IsValidVirtualPathName() ? virtualPath : "~/app_data/cache"; } set { this["virtualPath"] = value; } } /// /// Gets or sets the maximum number of days to store an image in the cache. /// /// The maximum number of days to store an image in the cache. /// Defaults to 28 if not set. [ConfigurationProperty("maxDays", DefaultValue = "365", IsRequired = false)] [IntegerValidator(ExcludeRange = false, MinValue = 0)] public int MaxDays { get { return (int)this["maxDays"]; } set { this["maxDays"] = value; } } /// /// Retrieves the cache configuration section from the current application configuration. /// /// The cache configuration section from the current application configuration. public static ImageCacheSection GetConfiguration() { ImageCacheSection imageCacheSection = ConfigurationManager.GetSection("imageProcessor/cache") as ImageCacheSection; if (imageCacheSection != null) { return imageCacheSection; } string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Configuration.Resources.cache.config"); XmlReader reader = new XmlTextReader(new StringReader(section)); imageCacheSection = new ImageCacheSection(); imageCacheSection.DeserializeSection(reader); return imageCacheSection; } } }