Browse Source

Fixing instance thread safety

Former-commit-id: 2121efd8578141a0edc316c471a7fc3a7834fc53
af/merge-core
James South 12 years ago
parent
commit
f2247c5d06
  1. 7
      src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs
  2. 5
      src/ImageProcessor.Web/NET45/ImageFactoryExtensions.cs
  3. 24
      src/ImageProcessor.Web/NET45/Processors/Format.cs
  4. 1
      src/ImageProcessor/Core/Common/Exceptions/ImageFormatException.cs
  5. 45
      src/ImageProcessor/ImageFactory.cs
  6. 5
      src/ImageProcessor/Imaging/Formats/FormatUtilities.cs
  7. 8
      src/ImageProcessor/Imaging/Formats/JpegFormat.cs
  8. 4
      src/ImageProcessor/Processors/Format.cs

7
src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs

@ -412,8 +412,7 @@ namespace ImageProcessor.Web.HttpModules
// Process the Image
imageFactory.Load(memoryStream)
.AddQueryString(queryString)
.AutoProcess()
.AutoProcess(queryString)
.Save(cachedPath);
// Store the response type in the context for later retrieval.
@ -451,7 +450,9 @@ namespace ImageProcessor.Web.HttpModules
semaphore.Wait();
// Process the Image
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);
imageFactory.Load(requestPath)
.AutoProcess(queryString)
.Save(cachedPath);
// Store the response type in the context for later retrieval.
context.Items[CachedResponseTypeKey] = imageFactory.CurrentImageFormat.MimeType;

5
src/ImageProcessor.Web/NET45/ImageFactoryExtensions.cs

@ -34,10 +34,11 @@ namespace ImageProcessor.Web
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class
/// that this method extends.
/// </param>
/// <param name="queryString">The collection of querystring parameters to process.</param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public static ImageFactory AutoProcess(this ImageFactory factory)
public static ImageFactory AutoProcess(this ImageFactory factory, string queryString)
{
if (factory.ShouldProcess)
{
@ -47,7 +48,7 @@ namespace ImageProcessor.Web
// Get a list of all graphics processors that have parsed and matched the query string.
List<IWebGraphicsProcessor> graphicsProcessors =
ImageProcessorConfiguration.Instance.GraphicsProcessors
.Where(x => x.MatchRegexIndex(factory.QueryString) != int.MaxValue)
.Where(x => x.MatchRegexIndex(queryString) != int.MaxValue)
.OrderBy(y => y.SortOrder)
.ToList();

24
src/ImageProcessor.Web/NET45/Processors/Format.cs

@ -134,24 +134,30 @@ namespace ImageProcessor.Web.Processors
{
identifier = identifier.ToLowerInvariant();
string finalIdentifier = identifier.Equals("png8") ? "png" : identifier;
ISupportedImageFormat newFormat = null;
ISupportedImageFormat format = ImageProcessorBootstrapper.Instance.SupportedImageFormats
.FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(finalIdentifier, StringComparison.InvariantCultureIgnoreCase)));
if (format != null)
{
// I wish this wasn't hard-coded but there's no way I can
// find to preserve the palette.
if (identifier.Equals("png8"))
// Return a new instance as we want to use instance properties.
newFormat = Activator.CreateInstance(format.GetType()) as ISupportedImageFormat;
if (newFormat != null)
{
format.IsIndexed = true;
}
else if (identifier.Equals("png"))
{
format.IsIndexed = false;
// I wish this wasn't hard-coded but there's no way I can
// find to preserve the palette.
if (identifier.Equals("png8"))
{
newFormat.IsIndexed = true;
}
else if (identifier.Equals("png"))
{
newFormat.IsIndexed = false;
}
}
}
return format;
return newFormat;
}
}
}

1
src/ImageProcessor/Core/Common/Exceptions/ImageFormatException.cs

@ -11,6 +11,7 @@
namespace ImageProcessor.Core.Common.Exceptions
{
using System;
using System.Runtime.Serialization;
/// <summary>
/// The exception that is thrown when loading the supported image format types has failed.

45
src/ImageProcessor/ImageFactory.cs

@ -94,11 +94,6 @@ namespace ImageProcessor
/// </summary>
public string ImagePath { get; private set; }
/// <summary>
/// Gets the query-string parameters for web image manipulation.
/// </summary>
public string QueryString { get; private set; }
/// <summary>
/// Gets a value indicating whether the image factory should process the file.
/// </summary>
@ -182,23 +177,13 @@ namespace ImageProcessor
/// </returns>
public ImageFactory Load(string imagePath)
{
// Remove any querystring parameters passed by web requests.
string[] paths = imagePath.Split('?');
string path = paths[0];
string query = string.Empty;
if (paths.Length > 1)
FileInfo fileInfo = new FileInfo(imagePath);
if (fileInfo.Exists)
{
query = paths[1];
}
if (File.Exists(path))
{
this.ImagePath = path;
this.QueryString = query;
this.ImagePath = imagePath;
// Open a file stream to prevent the need for lock.
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
ISupportedImageFormat format = FormatUtilities.GetFormat(fileStream);
@ -237,6 +222,10 @@ namespace ImageProcessor
this.ShouldProcess = true;
}
}
else
{
throw new FileNotFoundException(imagePath);
}
return this;
}
@ -267,24 +256,6 @@ namespace ImageProcessor
}
#region Manipulation
/// <summary>
/// Adds a query-string to the image factory to allow auto-processing of remote files.
/// </summary>
/// <param name="query">The query-string parameter to process.</param>
/// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns>
public ImageFactory AddQueryString(string query)
{
// TODO: Remove this.
if (this.ShouldProcess)
{
this.QueryString = query;
}
return this;
}
/// <summary>
/// Changes the opacity of the current image.
/// </summary>

5
src/ImageProcessor/Imaging/Formats/FormatUtilities.cs

@ -10,6 +10,7 @@
namespace ImageProcessor.Imaging.Formats
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
@ -53,7 +54,9 @@ namespace ImageProcessor.Imaging.Formats
if (header.SequenceEqual(buffer.Take(header.Length)))
{
stream.Position = 0;
return supportedImageFormat;
// Return a new instance as we want to use instance properties.
return Activator.CreateInstance(supportedImageFormat.GetType()) as ISupportedImageFormat;
}
}
}

8
src/ImageProcessor/Imaging/Formats/JpegFormat.cs

@ -22,6 +22,14 @@ namespace ImageProcessor.Imaging.Formats
/// </summary>
public sealed class JpegFormat : FormatBase
{
/// <summary>
/// Initializes a new instance of the <see cref="JpegFormat"/> class.
/// </summary>
public JpegFormat()
{
this.Quality = 90;
}
/// <summary>
/// Gets the file headers.
/// </summary>

4
src/ImageProcessor/Processors/Format.cs

@ -13,11 +13,7 @@ namespace ImageProcessor.Processors
#region Using
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using ImageProcessor.Imaging.Formats;
#endregion
/// <summary>

Loading…
Cancel
Save