Browse Source

Fixed bug where file extension is not changed

The file extension was not getting changed when the Format() command
function was being excecuted.


Former-commit-id: 1c486f11e48ca99552470d3963a7f32507e09068
pull/17/head
JimBobSquarePants 14 years ago
parent
commit
012eb2f1c7
  1. 54
      src/ImageProcessor.Web/Caching/DiskCache.cs
  2. 2
      src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
  3. 7
      src/ImageProcessor/ImageFactory.cs
  4. 27
      src/ImageProcessor/Imaging/ImageUtils.cs

54
src/ImageProcessor.Web/Caching/DiskCache.cs

@ -11,6 +11,7 @@ namespace ImageProcessor.Web.Caching
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
@ -42,6 +43,20 @@ namespace ImageProcessor.Web.Caching
/// </summary>
internal static readonly int MaxFileCachedDuration = ImageProcessorConfig.Instance.MaxCacheDays;
/// <summary>
/// The maximum number of files allowed in the directory.
/// </summary>
/// <remarks>
/// NTFS Folder can handle up to 8000 files in a directory.
/// This buffer will help us to ensure that we rarely hit anywhere near that limit.
/// </remarks>
private const int MaxFilesCount = 6000;
/// <summary>
/// The regular expression to search strings for extension changes.
/// </summary>
private static readonly Regex FormatRegex = new Regex(@"format=(jpeg|png|bmp|gif)", RegexOptions.Compiled);
/// <summary>
/// The object to lock against.
/// </summary>
@ -51,15 +66,6 @@ namespace ImageProcessor.Web.Caching
/// The default paths for Cached folders on the server.
/// </summary>
private static readonly string CachePath = ImageProcessorConfig.Instance.VirtualCachePath;
/// <summary>
/// The maximum number of files allowed in the directory.
/// </summary>
/// <remarks>
/// NTFS Folder can handle up to 8000 files in a directory.
/// This buffer will help us to ensure that we rarely hit anywhere near that limit.
/// </remarks>
private const int MaxFilesCount = 6000;
#endregion
#region Methods
@ -85,7 +91,13 @@ namespace ImageProcessor.Web.Caching
Directory.CreateDirectory(absoluteCachePath);
}
string cachedFileName = string.Format("{0}{1}", imagePath.ToMD5Fingerprint(), imageName.Substring(imageName.LastIndexOf(".", StringComparison.Ordinal)));
string parsedExtension = ParseExtension(imagePath);
string fallbackExtension = imageName.Substring(imageName.LastIndexOf(".", StringComparison.Ordinal));
string cachedFileName = string.Format(
"{0}{1}",
imagePath.ToMD5Fingerprint(),
!string.IsNullOrWhiteSpace(parsedExtension) ? parsedExtension : fallbackExtension);
cachedPath = Path.Combine(absoluteCachePath, cachedFileName);
}
@ -208,6 +220,28 @@ namespace ImageProcessor.Web.Caching
}
}
}
/// <summary>
/// Returns the correct file extension for the given string input
/// </summary>
/// <param name="input">
/// The string to parse.
/// </param>
/// <returns>
/// The correct file extension for the given string input if it can find one; otherwise an empty string.
/// </returns>
private static string ParseExtension(string input)
{
foreach (Match match in FormatRegex.Matches(input))
{
if (match.Success)
{
return "." + match.Value.Split('=')[1];
}
}
return string.Empty;
}
#endregion
}
}

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

@ -83,7 +83,7 @@ namespace ImageProcessor.Web.HttpModules
if (isRemote)
{
// We need to split the querystring to get the actual values we want.
string[] paths = HttpUtility.UrlDecode(context.Request.QueryString.ToString()).Replace("%7c", "|").Split('?');
string[] paths = HttpUtility.UrlDecode(context.Request.QueryString.ToString()).Split('?');
path = paths[0];

7
src/ImageProcessor/ImageFactory.cs

@ -97,7 +97,6 @@ namespace ImageProcessor
#endregion
#region Methods
/// <summary>
/// Loads the image to process. Always call this method first.
/// </summary>
@ -358,6 +357,12 @@ namespace ImageProcessor
{
if (this.ShouldProcess)
{
// We need to check here if the path has an extension and remove it if so.
// This is so we can add the correct image format.
int length = filePath.LastIndexOf(".", StringComparison.Ordinal);
string extension = ImageUtils.GetExtensionFromImageFormat(this.ImageFormat);
filePath = length == -1 ? filePath + extension : filePath.Substring(0, length) + extension;
// Fix the colour palette of gif images.
this.FixGifs();

27
src/ImageProcessor/Imaging/ImageUtils.cs

@ -74,10 +74,35 @@ namespace ImageProcessor.Imaging
return ImageFormat.Jpeg;
}
}
// TODO: SHow custom exception??
// TODO: Show custom exception??
return null;
}
/// <summary>
/// Returns the correct file extension for the given <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
/// </summary>
/// <param name="imageFormat">
/// The <see cref="T:System.Drawing.Imaging.ImageFormat"/> to return the extension for.
/// </param>
/// <returns>
/// The correct file extension for the given <see cref="T:System.Drawing.Imaging.ImageFormat"/>.
/// </returns>
public static string GetExtensionFromImageFormat(ImageFormat imageFormat)
{
switch (imageFormat.ToString())
{
case "Gif":
return ".gif";
case "Bmp":
return ".bmp";
case "Png":
return ".png";
default:
return ".jpg";
}
}
/// <summary>
/// Returns the correct image format based on the given response type.
/// </summary>

Loading…
Cancel
Save