Permalink
Browse files

Default configuration values can now be overriden in .config file

  • Loading branch information...
Dave McDermid
Dave McDermid committed Dec 23, 2011
1 parent dd5b2f5 commit 73389bbbeeee29ca2df0c07df28223be3d3232b0
Showing with 46 additions and 7 deletions.
  1. +29 −7 AdaptiveImageHandler.cs
  2. +1 −0 AdaptiveImages.csproj
  3. +16 −0 Sample.Web.config
View
@@ -7,22 +7,44 @@
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Imaging;
+using System.Configuration;
namespace AdaptiveImages
{
class AdaptiveImageHandler : IHttpHandler
{
- private static int[] resolutions = { 1382, 992, 768, 480 }; // the resolution break-points to use (screen widths, in pixels)
- private static string cache_path = "ai-cache"; // where to store the generated re-sized images. This folder must be writable.
- private static long jpg_quality = 80L; // the quality of any generated JPGs on a scale of 0 to 100
- private static bool watch_cache = true; // check that the responsive image isn't stale (ensures updated source images are re-cached)
- private static int browser_cache = 60 * 60 * 24 * 7; // How long the BROWSER cache should last (seconds, minutes, hours, days. 7days by default)
- private static bool mobile_first = true; // If there's no cookie FALSE sends the largest var resolutions version (TRUE sends smallest)
- private static string cookie_name = "resolution"; // the name of the cookie containing the resolution value
+ private int[] resolutions = { 1382, 992, 768, 480 }; // the resolution break-points to use (screen widths, in pixels)
+ private string cache_path = "ai-cache"; // where to store the generated re-sized images. This folder must be writable.
+ private long jpg_quality = 80L; // the quality of any generated JPGs on a scale of 0 to 100
+ private bool watch_cache = true; // check that the responsive image isn't stale (ensures updated source images are re-cached)
+ private int browser_cache = 60 * 60 * 24 * 7; // How long the BROWSER cache should last (seconds, minutes, hours, days. 7days by default)
+ private bool mobile_first = true; // If there's no cookie FALSE sends the largest var resolutions version (TRUE sends smallest)
+ private string cookie_name = "resolution"; // the name of the cookie containing the resolution value
private static string[] desktop_oss = { "macintosh", "x11", "windows nt" };
private static string[] image_exts = { ".png", ".gif", ".jpeg" };
+ public AdaptiveImageHandler()
+ {
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.ResolutionBreakPoints"])) {
+ int[] parsed_resolutions = ConfigurationManager.AppSettings["AdaptiveImages.ResolutionBreakPoints"].Split(',').Select(e => int.Parse(e)).ToArray();
+ if (parsed_resolutions.Length > 0)
+ resolutions = parsed_resolutions;
+ }
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.CachePath"]))
+ cache_path = ConfigurationManager.AppSettings["AdaptiveImages.CachePath"];
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.JpegQuality"]))
+ Int64.TryParse(ConfigurationManager.AppSettings["AdaptiveImages.JpegQuality"], out jpg_quality);
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.WatchCache"]))
+ watch_cache = string.Compare(ConfigurationManager.AppSettings["AdaptiveImages.WatchCache"], "true", true) == 0;
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.BrowserCache"]))
+ int.TryParse(ConfigurationManager.AppSettings["AdaptiveImages.BrowserCache"], out browser_cache);
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.MobileFirst"]))
+ mobile_first = string.Compare(ConfigurationManager.AppSettings["AdaptiveImages.MobileFirst"], "true", true) == 0;
+ if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AdaptiveImages.CookieName"]))
+ cookie_name = ConfigurationManager.AppSettings["AdaptiveImages.CookieName"];
+ }
+
public bool IsReusable
{
get { return false; }
View
@@ -32,6 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
+ <Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
View
@@ -1,5 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
+ <appSettings>
+ <!--the resolution break-points to use (screen widths, in pixels)-->
+ <add key="AdaptiveImages.ResolutionBreakpoints" value="1382, 992, 768, 480"/>
+ <!--where to store the generated re-sized images. This folder must be writable-->
+ <add key="AdaptiveImages.CachePath" value="ai-cache"/>
+ <!--the quality of any generated JPGs on a scale of 0 to 100-->
+ <add key="AdaptiveImages.JpegQuality" value="80"/>
+ <!--check that the responsive image isn't stale (ensures updated source images are re-cached)-->
+ <add key="AdaptiveImages.WatchCache" value="true"/>
+ <!--How long the BROWSER cache should last in seconds. (604800 - 7 days by default)-->
+ <add key="AdaptiveImages.BrowserCache" value="604800"/>
+ <!-- If there's no cookie false sends the largest var resolutions version (TRUE sends smallest)-->
+ <add key="AdaptiveImages.MobileFirst" value="true"/>
+ <!--the name of the cookie containing the resolution value-->
+ <add key="AdaptiveImages.CookieName" value="resolution"/>
+ </appSettings>
<system.webServer>
<handlers>
<add name="AdaptiveImageHandlerGif" path="*.gif" verb="GET" type="AdaptiveImages.AdaptiveImageHandler" />

0 comments on commit 73389bb

Please sign in to comment.