diff --git a/src/ImageProcessor.Web/Helpers/ProcessQueryStringEventArgs.cs b/src/ImageProcessor.Web/Helpers/ProcessQueryStringEventArgs.cs new file mode 100644 index 000000000..3bdda5c72 --- /dev/null +++ b/src/ImageProcessor.Web/Helpers/ProcessQueryStringEventArgs.cs @@ -0,0 +1,25 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// The process querystring event arguments. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.Helpers +{ + using System; + + /// + /// The process querystring event arguments. + /// + public class ProcessQueryStringEventArgs : EventArgs + { + /// + /// Gets or sets the querystring. + /// + public string Querystring { get; set; } + } +} diff --git a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs index de1a64382..2b06b2b7c 100644 --- a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs +++ b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs @@ -107,11 +107,28 @@ namespace ImageProcessor.Web.HttpModules } #endregion + /// + /// The process querystring event handler. + /// + /// + /// The sender. + /// + /// + /// The . + /// + /// Returns the processed querystring. + public delegate string ProcessQuerystringEventHandler(object sender, ProcessQueryStringEventArgs e); + /// /// The event that is called when a new image is processed. /// public static event EventHandler OnPostProcessing; + /// + /// The event that is called when a querystring is processed. + /// + public static event ProcessQuerystringEventHandler OnProcessQuerystring; + #region IHttpModule Members /// /// Initializes a module and prepares it to handle requests. @@ -327,6 +344,9 @@ namespace ImageProcessor.Web.HttpModules } } + // Replace any presets in the querystring with the actual value. + queryString = this.ReplacePresetsInQueryString(queryString); + // If the current service doesn't require a prefix, don't fetch it. // Let the static file handler take over. if (string.IsNullOrWhiteSpace(currentService.Prefix) && string.IsNullOrWhiteSpace(queryString)) @@ -334,9 +354,6 @@ namespace ImageProcessor.Web.HttpModules return; } - // Replace any presets in the querystring with the actual value. - queryString = this.ReplacePresetsInQueryString(queryString); - string parts = !string.IsNullOrWhiteSpace(urlParameters) ? "?" + urlParameters : string.Empty; string fullPath = string.Format("{0}{1}?{2}", requestPath, parts, queryString); object resourcePath; @@ -526,18 +543,29 @@ namespace ImageProcessor.Web.HttpModules /// private string ReplacePresetsInQueryString(string queryString) { - foreach (Match match in PresetRegex.Matches(queryString)) + if (!string.IsNullOrWhiteSpace(queryString)) { - if (match.Success) + foreach (Match match in PresetRegex.Matches(queryString)) { - string preset = match.Value.Split('=')[1]; + if (match.Success) + { + string preset = match.Value.Split('=')[1]; - // We use the processor config system to store the preset values. - string replacements = ImageProcessorConfiguration.Instance.GetPresetSettings(preset); - queryString = Regex.Replace(queryString, preset, replacements ?? string.Empty); + // We use the processor config system to store the preset values. + string replacements = ImageProcessorConfiguration.Instance.GetPresetSettings(preset); + queryString = Regex.Replace(queryString, preset, replacements ?? string.Empty); + } } } + // Fire the process querystring event. + ProcessQuerystringEventHandler handler = OnProcessQuerystring; + if (handler != null) + { + ProcessQueryStringEventArgs args = new ProcessQueryStringEventArgs { Querystring = queryString ?? string.Empty }; + queryString = handler(this, args); + } + return queryString; } diff --git a/src/ImageProcessor.Web/ImageProcessor.Web.csproj b/src/ImageProcessor.Web/ImageProcessor.Web.csproj index 1c7e29c40..9ad80addb 100644 --- a/src/ImageProcessor.Web/ImageProcessor.Web.csproj +++ b/src/ImageProcessor.Web/ImageProcessor.Web.csproj @@ -46,6 +46,7 @@ + diff --git a/src/TestWebsites/MVC/Global.asax.cs b/src/TestWebsites/MVC/Global.asax.cs index 54afb402f..f72024ad9 100644 --- a/src/TestWebsites/MVC/Global.asax.cs +++ b/src/TestWebsites/MVC/Global.asax.cs @@ -10,6 +10,7 @@ namespace Test_Website_NET45 { using System.Diagnostics; using System.Threading.Tasks; + using System.Web.UI.WebControls; using ImageProcessor.Web.Helpers; using ImageProcessor.Web.HttpModules; @@ -28,7 +29,16 @@ namespace Test_Website_NET45 // Test the post processing event. ImageProcessingModule.OnPostProcessing += (sender, args) => Debug.WriteLine(args.CachedImagePath); - //ImageProcessingModule.OnPostProcessing += this.WritePath; + + ImageProcessingModule.OnProcessQuerystring += (sender, args) => + { + if (!args.Querystring.Contains("watermark")) + { + return args.Querystring += "watermark=protected&color=fff&fontsize=36&fontopacity=70textshadow=true&fontfamily=arial"; + } + + return args.Querystring; + }; } private async void WritePath(object sender, PostProcessingEventArgs e)