Browse Source

Fixing instance thread safety

Former-commit-id: d1478f5ae95ddbd161073204df65799a903fb20f
af/merge-core
James South 12 years ago
parent
commit
d63151e644
  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 // Process the Image
imageFactory.Load(memoryStream) imageFactory.Load(memoryStream)
.AddQueryString(queryString) .AutoProcess(queryString)
.AutoProcess()
.Save(cachedPath); .Save(cachedPath);
// Store the response type in the context for later retrieval. // Store the response type in the context for later retrieval.
@ -451,7 +450,9 @@ namespace ImageProcessor.Web.HttpModules
semaphore.Wait(); semaphore.Wait();
// Process the Image // 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. // Store the response type in the context for later retrieval.
context.Items[CachedResponseTypeKey] = imageFactory.CurrentImageFormat.MimeType; 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 /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class
/// that this method extends. /// that this method extends.
/// </param> /// </param>
/// <param name="queryString">The collection of querystring parameters to process.</param>
/// <returns> /// <returns>
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class. /// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
/// </returns> /// </returns>
public static ImageFactory AutoProcess(this ImageFactory factory) public static ImageFactory AutoProcess(this ImageFactory factory, string queryString)
{ {
if (factory.ShouldProcess) 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. // Get a list of all graphics processors that have parsed and matched the query string.
List<IWebGraphicsProcessor> graphicsProcessors = List<IWebGraphicsProcessor> graphicsProcessors =
ImageProcessorConfiguration.Instance.GraphicsProcessors ImageProcessorConfiguration.Instance.GraphicsProcessors
.Where(x => x.MatchRegexIndex(factory.QueryString) != int.MaxValue) .Where(x => x.MatchRegexIndex(queryString) != int.MaxValue)
.OrderBy(y => y.SortOrder) .OrderBy(y => y.SortOrder)
.ToList(); .ToList();

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

@ -134,24 +134,30 @@ namespace ImageProcessor.Web.Processors
{ {
identifier = identifier.ToLowerInvariant(); identifier = identifier.ToLowerInvariant();
string finalIdentifier = identifier.Equals("png8") ? "png" : identifier; string finalIdentifier = identifier.Equals("png8") ? "png" : identifier;
ISupportedImageFormat newFormat = null;
ISupportedImageFormat format = ImageProcessorBootstrapper.Instance.SupportedImageFormats ISupportedImageFormat format = ImageProcessorBootstrapper.Instance.SupportedImageFormats
.FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(finalIdentifier, StringComparison.InvariantCultureIgnoreCase))); .FirstOrDefault(f => f.FileExtensions.Any(e => e.Equals(finalIdentifier, StringComparison.InvariantCultureIgnoreCase)));
if (format != null) if (format != null)
{ {
// I wish this wasn't hard-coded but there's no way I can // Return a new instance as we want to use instance properties.
// find to preserve the palette. newFormat = Activator.CreateInstance(format.GetType()) as ISupportedImageFormat;
if (identifier.Equals("png8")) if (newFormat != null)
{ {
format.IsIndexed = true; // I wish this wasn't hard-coded but there's no way I can
} // find to preserve the palette.
else if (identifier.Equals("png")) if (identifier.Equals("png8"))
{ {
format.IsIndexed = false; 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 namespace ImageProcessor.Core.Common.Exceptions
{ {
using System; using System;
using System.Runtime.Serialization;
/// <summary> /// <summary>
/// The exception that is thrown when loading the supported image format types has failed. /// 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> /// </summary>
public string ImagePath { get; private set; } public string ImagePath { get; private set; }
/// <summary>
/// Gets the query-string parameters for web image manipulation.
/// </summary>
public string QueryString { get; private set; }
/// <summary> /// <summary>
/// Gets a value indicating whether the image factory should process the file. /// Gets a value indicating whether the image factory should process the file.
/// </summary> /// </summary>
@ -182,23 +177,13 @@ namespace ImageProcessor
/// </returns> /// </returns>
public ImageFactory Load(string imagePath) public ImageFactory Load(string imagePath)
{ {
// Remove any querystring parameters passed by web requests. FileInfo fileInfo = new FileInfo(imagePath);
string[] paths = imagePath.Split('?'); if (fileInfo.Exists)
string path = paths[0];
string query = string.Empty;
if (paths.Length > 1)
{ {
query = paths[1]; this.ImagePath = imagePath;
}
if (File.Exists(path))
{
this.ImagePath = path;
this.QueryString = query;
// Open a file stream to prevent the need for lock. // 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); ISupportedImageFormat format = FormatUtilities.GetFormat(fileStream);
@ -237,6 +222,10 @@ namespace ImageProcessor
this.ShouldProcess = true; this.ShouldProcess = true;
} }
} }
else
{
throw new FileNotFoundException(imagePath);
}
return this; return this;
} }
@ -267,24 +256,6 @@ namespace ImageProcessor
} }
#region Manipulation #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> /// <summary>
/// Changes the opacity of the current image. /// Changes the opacity of the current image.
/// </summary> /// </summary>

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

@ -10,6 +10,7 @@
namespace ImageProcessor.Imaging.Formats namespace ImageProcessor.Imaging.Formats
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
@ -53,7 +54,9 @@ namespace ImageProcessor.Imaging.Formats
if (header.SequenceEqual(buffer.Take(header.Length))) if (header.SequenceEqual(buffer.Take(header.Length)))
{ {
stream.Position = 0; 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> /// </summary>
public sealed class JpegFormat : FormatBase public sealed class JpegFormat : FormatBase
{ {
/// <summary>
/// Initializes a new instance of the <see cref="JpegFormat"/> class.
/// </summary>
public JpegFormat()
{
this.Quality = 90;
}
/// <summary> /// <summary>
/// Gets the file headers. /// Gets the file headers.
/// </summary> /// </summary>

4
src/ImageProcessor/Processors/Format.cs

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

Loading…
Cancel
Save