Browse Source

Adding ProcessQuerystringEvent

Allows augmentation of the querystring to allow watermarking.


Former-commit-id: 0b8db9f2f54858725e29826d98fbb4de8af9cbe6
Former-commit-id: 45a24b35a38f477fdaf711abd7d1fb2046afc687
af/merge-core
James South 11 years ago
parent
commit
076990dd7a
  1. 25
      src/ImageProcessor.Web/Helpers/ProcessQueryStringEventArgs.cs
  2. 46
      src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
  3. 1
      src/ImageProcessor.Web/ImageProcessor.Web.csproj
  4. 12
      src/TestWebsites/MVC/Global.asax.cs

25
src/ImageProcessor.Web/Helpers/ProcessQueryStringEventArgs.cs

@ -0,0 +1,25 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProcessQueryStringEventArgs.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// The process querystring event arguments.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Helpers
{
using System;
/// <summary>
/// The process querystring event arguments.
/// </summary>
public class ProcessQueryStringEventArgs : EventArgs
{
/// <summary>
/// Gets or sets the querystring.
/// </summary>
public string Querystring { get; set; }
}
}

46
src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

@ -107,11 +107,28 @@ namespace ImageProcessor.Web.HttpModules
}
#endregion
/// <summary>
/// The process querystring event handler.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The <see cref="ProcessQueryStringEventArgs"/>.
/// </param>
/// <returns>Returns the processed querystring.</returns>
public delegate string ProcessQuerystringEventHandler(object sender, ProcessQueryStringEventArgs e);
/// <summary>
/// The event that is called when a new image is processed.
/// </summary>
public static event EventHandler<PostProcessingEventArgs> OnPostProcessing;
/// <summary>
/// The event that is called when a querystring is processed.
/// </summary>
public static event ProcessQuerystringEventHandler OnProcessQuerystring;
#region IHttpModule Members
/// <summary>
/// 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
/// </returns>
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;
}

1
src/ImageProcessor.Web/ImageProcessor.Web.csproj

@ -46,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Caching\CachedImage.cs" />
<Compile Include="Helpers\ProcessQueryStringEventArgs.cs" />
<Compile Include="Processors\DetectEdges.cs" />
<Compile Include="Processors\EntropyCrop.cs" />
<Compile Include="Processors\Hue.cs" />

12
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)

Loading…
Cancel
Save