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,21 +172,26 @@ 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))
{
// Copy to memory stream to avoid Image locking file
using (var memoryStream = new MemoryStream())
{
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var originalImage = Image.FromStream(fileStream, true, false))
{
var newWidth = Convert.ToInt32(newSize.Width);
var newHeight = Convert.ToInt32(newSize.Height);
// 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);
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 thumbnailGraph = Graphics.FromImage(thumbnail);
using (var thumbnailGraph = Graphics.FromImage(thumbnail))
{
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
@ -197,12 +202,12 @@ namespace MediaBrowser.Controller.Drawing
var outputFormat = originalImage.RawFormat;
using (var memoryStream = new MemoryStream())
using (var outputMemoryStream = new MemoryStream())
{
// Save to the memory stream
thumbnail.Save(outputFormat, memoryStream, quality.Value);
thumbnail.Save(outputFormat, outputMemoryStream, quality.Value);
var bytes = memoryStream.ToArray();
var bytes = outputMemoryStream.ToArray();
var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
@ -211,9 +216,10 @@ namespace MediaBrowser.Controller.Drawing
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,9 +472,14 @@ 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())
{
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
{
var outputFormat = originalImage.RawFormat;
@ -484,6 +493,7 @@ namespace MediaBrowser.Controller.Drawing
}
}
}
}
catch (Exception ex)
{
// We have to have a catch-all here because some of the .net image methods throw a plain old Exception
@ -547,9 +557,14 @@ 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())
{
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var originalImage = Image.FromStream(memoryStream, true, false))
{
//Pass the image through registered enhancers
using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
@ -563,6 +578,7 @@ namespace MediaBrowser.Controller.Drawing
}
}
}
}
finally
{
semaphore.Release();
@ -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);