fixes #226 - We're holding locks on images

pull/702/head
Luke Pulverenti 12 years ago
parent 84714deccc
commit beafdb27f9

@ -1,4 +1,6 @@
using MediaBrowser.Model.Logging; using System.Threading.Tasks;
using MediaBrowser.Common.IO;
using MediaBrowser.Model.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
@ -39,7 +41,7 @@ namespace MediaBrowser.Controller.Drawing
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
/// <returns>The dimensions of the specified image.</returns> /// <returns>The dimensions of the specified image.</returns>
/// <exception cref="ArgumentException">The image was of an unrecognised format.</exception> /// <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 try
{ {
@ -54,11 +56,17 @@ namespace MediaBrowser.Controller.Drawing
catch catch
{ {
logger.Info("Failed to read image header for {0}. Doing it the slow way.", path); 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 // 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; return b.Size;
} }

@ -172,21 +172,26 @@ namespace MediaBrowser.Controller.Drawing
try 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)) using (var originalImage = Image.FromStream(fileStream, true, false))
{ {
var newWidth = Convert.ToInt32(newSize.Width); var newWidth = Convert.ToInt32(newSize.Width);
var newHeight = Convert.ToInt32(newSize.Height); var newHeight = Convert.ToInt32(newSize.Height);
// Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here // 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 // Preserve the original resolution
thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution); thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
var thumbnailGraph = Graphics.FromImage(thumbnail); using (var thumbnailGraph = Graphics.FromImage(thumbnail))
{
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality; thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality; thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
@ -197,12 +202,12 @@ namespace MediaBrowser.Controller.Drawing
var outputFormat = originalImage.RawFormat; var outputFormat = originalImage.RawFormat;
using (var memoryStream = new MemoryStream()) using (var outputMemoryStream = new MemoryStream())
{ {
// Save to the memory stream // 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); var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
@ -211,9 +216,10 @@ namespace MediaBrowser.Controller.Drawing
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 // Cache file doesn't exist no biggie
} }
_logger.Debug("Getting image size for {0}", imagePath); var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false);
var size = ImageHeader.GetDimensions(imagePath, _logger);
// Update the file system cache // Update the file system cache
File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture)); File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture));
@ -468,9 +472,14 @@ namespace MediaBrowser.Controller.Drawing
try 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())
{ {
using (var originalImage = (Bitmap)Image.FromStream(fileStream, true, false)) await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var originalImage = (Bitmap)Image.FromStream(memoryStream, true, false))
{ {
var outputFormat = originalImage.RawFormat; var outputFormat = originalImage.RawFormat;
@ -484,6 +493,7 @@ namespace MediaBrowser.Controller.Drawing
} }
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
// We have to have a catch-all here because some of the .net image methods throw a plain old Exception // 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 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 //Pass the image through registered enhancers
using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false)) using (var newImage = await ExecuteImageEnhancers(supportedEnhancers, originalImage, item, imageType, imageIndex).ConfigureAwait(false))
@ -563,6 +578,7 @@ namespace MediaBrowser.Controller.Drawing
} }
} }
} }
}
finally finally
{ {
semaphore.Release(); semaphore.Release();
@ -650,8 +666,6 @@ namespace MediaBrowser.Controller.Drawing
{ {
var typeName = enhancer.GetType().Name; var typeName = enhancer.GetType().Name;
_logger.Debug("Running {0} for {1}", typeName, item.Path ?? item.Name ?? "--Unknown--");
try try
{ {
result = await enhancer.EnhanceImageAsync(item, result, imageType, imageIndex).ConfigureAwait(false); result = await enhancer.EnhanceImageAsync(item, result, imageType, imageIndex).ConfigureAwait(false);

Loading…
Cancel
Save