image delivery optimizations

pull/702/head
Luke Pulverenti 12 years ago
parent e031c7bcaa
commit 7d33a08cb9

@ -120,10 +120,28 @@ namespace MediaBrowser.Controller.Drawing
originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false); originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
} }
var supportedEnhancers = _kernel.ImageEnhancers.Where(i =>
{
try
{
return i.Supports(entity, imageType);
}
catch (Exception ex)
{
_logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
return false;
}
}).ToList();
// No enhancement - don't cache
if (supportedEnhancers.Count > 0)
{
try try
{ {
// Enhance if we have enhancers // Enhance if we have enhancers
var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex).ConfigureAwait(false); var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex, supportedEnhancers).ConfigureAwait(false);
// If the path changed update dateModified // If the path changed update dateModified
if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase)) if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase))
@ -136,6 +154,7 @@ namespace MediaBrowser.Controller.Drawing
{ {
_logger.Error("Error enhancing image", ex); _logger.Error("Error enhancing image", ex);
} }
}
var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false); var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false);
@ -179,7 +198,7 @@ namespace MediaBrowser.Controller.Drawing
{ {
await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false); await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false);
using (var originalImage = Image.FromStream(fileStream, true, false)) using (var originalImage = Image.FromStream(memoryStream, 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);
@ -212,9 +231,9 @@ namespace MediaBrowser.Controller.Drawing
var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length); var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
// kick off a task to cache the result // kick off a task to cache the result
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false); var cacheTask = CacheResizedImage(cacheFilePath, bytes);
await outputTask.ConfigureAwait(false); await Task.WhenAll(outputTask, cacheTask).ConfigureAwait(false);
} }
} }
} }
@ -517,9 +536,10 @@ namespace MediaBrowser.Controller.Drawing
/// <param name="item">The item.</param> /// <param name="item">The item.</param>
/// <param name="imageType">Type of the image.</param> /// <param name="imageType">Type of the image.</param>
/// <param name="imageIndex">Index of the image.</param> /// <param name="imageIndex">Index of the image.</param>
/// <param name="supportedEnhancers">The supported enhancers.</param>
/// <returns>System.String.</returns> /// <returns>System.String.</returns>
/// <exception cref="System.ArgumentNullException">originalImagePath</exception> /// <exception cref="System.ArgumentNullException">originalImagePath</exception>
public async Task<string> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex) public async Task<string> GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex, IEnumerable<IImageEnhancer> supportedEnhancers)
{ {
if (string.IsNullOrEmpty(originalImagePath)) if (string.IsNullOrEmpty(originalImagePath))
{ {
@ -531,27 +551,6 @@ namespace MediaBrowser.Controller.Drawing
throw new ArgumentNullException("item"); throw new ArgumentNullException("item");
} }
var supportedEnhancers = _kernel.ImageEnhancers.Where(i =>
{
try
{
return i.Supports(item, imageType);
}
catch (Exception ex)
{
_logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
return false;
}
}).ToList();
// No enhancement - don't cache
if (supportedEnhancers.Count == 0)
{
return originalImagePath;
}
var cacheGuid = GetImageCacheTag(originalImagePath, dateModified, supportedEnhancers, item, imageType); var cacheGuid = GetImageCacheTag(originalImagePath, dateModified, supportedEnhancers, item, imageType);
// All enhanced images are saved as png to allow transparency // All enhanced images are saved as png to allow transparency

@ -205,4 +205,7 @@ Global
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal EndGlobal

Loading…
Cancel
Save