fixes #226 - We're holding locks on images

This commit is contained in:
Luke Pulverenti 2013-05-08 14:05:14 -04:00
parent 84714deccc
commit beafdb27f9
2 changed files with 77 additions and 55 deletions

View File

@ -1,4 +1,6 @@
using MediaBrowser.Model.Logging;
using System.Threading.Tasks;
using MediaBrowser.Common.IO;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Drawing;
@ -39,7 +41,7 @@ namespace MediaBrowser.Controller.Drawing
/// <param name="logger">The logger.</param>
/// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognised format.</exception>
public static Size GetDimensions(string path, ILogger logger)
public static async Task<Size> GetDimensions(string path, ILogger logger)
{
try
{
@ -54,11 +56,17 @@ namespace MediaBrowser.Controller.Drawing
catch
{
logger.Info("Failed to read image header for {0}. Doing it the slow way.", path);
}
using (var fs = File.OpenRead(path))
// Buffer to memory stream to avoid image locking file
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
using (var memoryStream = new MemoryStream())
{
await fs.CopyToAsync(memoryStream).ConfigureAwait(false);
// Co it the old fashioned way
using (var b = Image.FromStream(fs, true, false))
using (var b = Image.FromStream(memoryStream, true, false))
{
return b.Size;
}

View File

@ -172,48 +172,54 @@ namespace MediaBrowser.Controller.Drawing
try
{
using (var fileStream = File.OpenRead(originalImagePath))
using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
using (var originalImage = Image.FromStream(fileStream, true, false))
// Copy to memory stream to avoid Image locking file
using (var memoryStream = new MemoryStream())
{
var newWidth = Convert.ToInt32(newSize.Width);
var newHeight = Convert.ToInt32(newSize.Height);
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat);
// Preserve the original resolution
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
var thumbnailGraph = Graphics.FromImage(thumbnail);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
var outputFormat = originalImage.RawFormat;
using (var memoryStream = new MemoryStream())
using (var originalImage = Image.FromStream(fileStream, true, false))
{
// Save to the memory stream
thumbnail.Save(outputFormat, memoryStream, quality.Value);
var newWidth = Convert.ToInt32(newSize.Width);
var newHeight = Convert.ToInt32(newSize.Height);
var bytes = memoryStream.ToArray();
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
using (var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat))
{
// Preserve the original resolution
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
using (var thumbnailGraph = Graphics.FromImage(thumbnail))
{
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
thumbnailGraph.CompositingMode = CompositingMode.SourceOver;
// kick off a task to cache the result
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);
var outputFormat = originalImage.RawFormat;
using (var outputMemoryStream = new MemoryStream())
{
// Save to the memory stream
thumbnail.Save(outputFormat, outputMemoryStream, quality.Value);
var bytes = outputMemoryStream.ToArray();
var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
// kick off a task to cache the result
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
await outputTask.ConfigureAwait(false);
}
}
}
await outputTask.ConfigureAwait(false);
}
thumbnailGraph.Dispose();
thumbnail.Dispose();
}
}
}
@ -320,9 +326,7 @@ namespace MediaBrowser.Controller.Drawing
// Cache file doesn't exist no biggie
}
_logger.Debug("Getting image size for {0}", imagePath);
var size = ImageHeader.GetDimensions(imagePath, _logger);
var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false);
// Update the file system cache
File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture));
@ -468,17 +472,23 @@ namespace MediaBrowser.Controller.Drawing
try
{
using (var fileStream = File.OpenRead(originalImagePath))
using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
using (var originalImage = (Bitmap)Image.FromStream(fileStream, true, false))
// Copy to memory stream to avoid Image locking file
using (var memoryStream = new MemoryStream())
{
var outputFormat = originalImage.RawFormat;
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var croppedImage = originalImage.CropWhitespace())
using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
{
using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
var outputFormat = originalImage.RawFormat;
using (var croppedImage = originalImage.CropWhitespace())
{
croppedImage.Save(outputFormat, outputStream, 100);
using (var outputStream = new FileStream(croppedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
croppedImage.Save(outputFormat, outputStream, 100);
}
}
}
}
@ -547,17 +557,23 @@ namespace MediaBrowser.Controller.Drawing
try
{
using (var fileStream = File.OpenRead(originalImagePath))
using (var fileStream = new FileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
{
using (var originalImage = Image.FromStream(fileStream, true, false))
// Copy to memory stream to avoid Image locking file
using (var memoryStream = new MemoryStream())
{
//Pass the image through registered enhancers
using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var originalImage = Image.FromStream(memoryStream, true, false))
{
//And then save it in the cache
using (var outputStream = new FileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
//Pass the image through registered enhancers
using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
{
newImage.Save(ImageFormat.Png, outputStream, 100);
//And then save it in the cache
using (var outputStream = new FileStream(enhancedImagePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
newImage.Save(ImageFormat.Png, outputStream, 100);
}
}
}
}
@ -650,8 +666,6 @@ namespace MediaBrowser.Controller.Drawing
{
var typeName = enhancer.GetType().Name;
_logger.Debug("Running {0} for {1}", typeName, item.Path ?? item.Name ?? "--Unknown--");
try
{
result = await enhancer.EnhanceImageAsync(item, result, imageType, imageIndex).ConfigureAwait(false);