From 7d33a08cb9b24746259c65406a2c40ce0cb89197 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 2 Jun 2013 12:45:32 -0400 Subject: [PATCH] image delivery optimizations --- .../Drawing/ImageManager.cs | 71 +++++++++---------- MediaBrowser.sln | 3 + 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index f2d6657d54..807c2d1e0c 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -120,21 +120,40 @@ namespace MediaBrowser.Controller.Drawing originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false); } - try + var supportedEnhancers = _kernel.ImageEnhancers.Where(i => { - // Enhance if we have enhancers - var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex).ConfigureAwait(false); - - // If the path changed update dateModified - if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase)) + try { - dateModified = File.GetLastWriteTimeUtc(ehnancedImagePath); - originalImagePath = ehnancedImagePath; + return i.Supports(entity, imageType); } - } - catch (Exception ex) + 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) { - _logger.Error("Error enhancing image", ex); + try + { + // Enhance if we have enhancers + var ehnancedImagePath = await GetEnhancedImage(originalImagePath, dateModified, entity, imageType, imageIndex, supportedEnhancers).ConfigureAwait(false); + + // If the path changed update dateModified + if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase)) + { + dateModified = File.GetLastWriteTimeUtc(ehnancedImagePath); + originalImagePath = ehnancedImagePath; + } + } + catch (Exception ex) + { + _logger.Error("Error enhancing image", ex); + } } var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false); @@ -179,7 +198,7 @@ namespace MediaBrowser.Controller.Drawing { 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 newHeight = Convert.ToInt32(newSize.Height); @@ -212,9 +231,9 @@ namespace MediaBrowser.Controller.Drawing var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length); // 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 /// The item. /// Type of the image. /// Index of the image. + /// The supported enhancers. /// System.String. /// originalImagePath - public async Task GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex) + public async Task GetEnhancedImage(string originalImagePath, DateTime dateModified, BaseItem item, ImageType imageType, int imageIndex, IEnumerable supportedEnhancers) { if (string.IsNullOrEmpty(originalImagePath)) { @@ -531,27 +551,6 @@ namespace MediaBrowser.Controller.Drawing 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); // All enhanced images are saved as png to allow transparency diff --git a/MediaBrowser.sln b/MediaBrowser.sln index ea09b3fae4..86624423eb 100644 --- a/MediaBrowser.sln +++ b/MediaBrowser.sln @@ -205,4 +205,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal