From 012eb2f1c767bb4ba3127d8efa06dc3cefe655ba Mon Sep 17 00:00:00 2001 From: JimBobSquarePants Date: Mon, 2 Jul 2012 17:04:13 +0100 Subject: [PATCH] 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 --- src/ImageProcessor.Web/Caching/DiskCache.cs | 54 +++++++++++++++---- .../HttpModules/ImageProcessingModule.cs | 2 +- src/ImageProcessor/ImageFactory.cs | 7 ++- src/ImageProcessor/Imaging/ImageUtils.cs | 27 +++++++++- 4 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/ImageProcessor.Web/Caching/DiskCache.cs b/src/ImageProcessor.Web/Caching/DiskCache.cs index 4ce6021fb..7930623ee 100644 --- a/src/ImageProcessor.Web/Caching/DiskCache.cs +++ b/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 /// internal static readonly int MaxFileCachedDuration = ImageProcessorConfig.Instance.MaxCacheDays; + /// + /// The maximum number of files allowed in the directory. + /// + /// + /// 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. + /// + private const int MaxFilesCount = 6000; + + /// + /// The regular expression to search strings for extension changes. + /// + private static readonly Regex FormatRegex = new Regex(@"format=(jpeg|png|bmp|gif)", RegexOptions.Compiled); + /// /// The object to lock against. /// @@ -51,15 +66,6 @@ namespace ImageProcessor.Web.Caching /// The default paths for Cached folders on the server. /// private static readonly string CachePath = ImageProcessorConfig.Instance.VirtualCachePath; - - /// - /// The maximum number of files allowed in the directory. - /// - /// - /// 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. - /// - 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 } } } + + /// + /// Returns the correct file extension for the given string input + /// + /// + /// The string to parse. + /// + /// + /// The correct file extension for the given string input if it can find one; otherwise an empty string. + /// + 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 } } diff --git a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs index a3e23610f..335df1cb5 100644 --- a/src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs +++ b/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]; diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index 9acd8e135..7eded8bd4 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -97,7 +97,6 @@ namespace ImageProcessor #endregion #region Methods - /// /// Loads the image to process. Always call this method first. /// @@ -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(); diff --git a/src/ImageProcessor/Imaging/ImageUtils.cs b/src/ImageProcessor/Imaging/ImageUtils.cs index e5525e44c..ee6ec75cc 100644 --- a/src/ImageProcessor/Imaging/ImageUtils.cs +++ b/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; } + /// + /// Returns the correct file extension for the given . + /// + /// + /// The to return the extension for. + /// + /// + /// The correct file extension for the given . + /// + public static string GetExtensionFromImageFormat(ImageFormat imageFormat) + { + switch (imageFormat.ToString()) + { + case "Gif": + return ".gif"; + case "Bmp": + return ".bmp"; + case "Png": + return ".png"; + default: + return ".jpg"; + } + } + /// /// Returns the correct image format based on the given response type. ///