Skip to content

Commit

Permalink
Bypass .NET encoders when not processing.
Browse files Browse the repository at this point in the history
Images with no processing requirements no longer go through the
ImageFactory pipeline. They will still however be cached and optionally
postprocessed. #208
  • Loading branch information
JimBobSquarePants committed Aug 26, 2015
1 parent acdde7a commit 6e807cf
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
Expand Up @@ -21,6 +21,7 @@ namespace ImageProcessor.Web.HttpModules
using System.Web;
using System.Web.Hosting;

using ImageProcessor.Imaging.Formats;
using ImageProcessor.Web.Caching;
using ImageProcessor.Web.Configuration;
using ImageProcessor.Web.Extensions;
Expand Down Expand Up @@ -484,13 +485,6 @@ private async Task ProcessImageAsync(HttpContext context)
// Execute the handler which can change the querystring
queryString = this.CheckQuerystringHandler(queryString, request.Unvalidated.RawUrl);

// 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))
{
return;
}

if (string.IsNullOrWhiteSpace(requestPath))
{
return;
Expand Down Expand Up @@ -536,13 +530,23 @@ private async Task ProcessImageAsync(HttpContext context)
using (ImageFactory imageFactory = new ImageFactory(preserveExifMetaData != null && preserveExifMetaData.Value))
{
byte[] imageBuffer = await currentService.GetImage(resourcePath);
string mimeType;

using (MemoryStream inStream = new MemoryStream(imageBuffer))
{
// Process the Image
MemoryStream outStream = new MemoryStream();

imageFactory.Load(inStream).AutoProcess(queryString).Save(outStream);
if (!string.IsNullOrWhiteSpace(queryString))
{
imageFactory.Load(inStream).AutoProcess(queryString).Save(outStream);
mimeType = imageFactory.CurrentImageFormat.MimeType;
}
else
{
await inStream.CopyToAsync(outStream);
mimeType = FormatUtilities.GetFormat(outStream).MimeType;
}

// Post process the image.
if (ImageProcessorConfiguration.Instance.PostProcess)
Expand All @@ -552,15 +556,15 @@ private async Task ProcessImageAsync(HttpContext context)
}

// Add to the cache.
await this.imageCache.AddImageToCacheAsync(outStream, imageFactory.CurrentImageFormat.MimeType);
await this.imageCache.AddImageToCacheAsync(outStream, mimeType);

// Cleanup
outStream.Dispose();
}

// Store the cached path, response type, and cache dependency in the context for later retrieval.
context.Items[CachedPathKey] = cachedPath;
context.Items[CachedResponseTypeKey] = imageFactory.CurrentImageFormat.MimeType;
context.Items[CachedResponseTypeKey] = mimeType;
bool isFileCached = new Uri(cachedPath).IsFile;

if (isFileLocal)
Expand Down

0 comments on commit 6e807cf

Please sign in to comment.