diff options
| author | Luke <luke.pulverenti@gmail.com> | 2017-05-31 15:40:34 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-31 15:40:34 -0400 |
| commit | 91176d9ccc1dde8155c10411c70e62a9f4b059d5 (patch) | |
| tree | 21365f5a8dd09534a53d9f88d2a7a3116f3f3f98 /Emby.Drawing/ImageProcessor.cs | |
| parent | c37c9a75073b1b9caa3af2c3bc62abd837bd630e (diff) | |
| parent | 4e10daf646e0788409f2bc52ef70effa2616e3f3 (diff) | |
Merge pull request #2677 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Drawing/ImageProcessor.cs')
| -rw-r--r-- | Emby.Drawing/ImageProcessor.cs | 109 |
1 files changed, 85 insertions, 24 deletions
diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index 3fa6f6450..a1543382f 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -17,7 +17,7 @@ using System.Threading; using System.Threading.Tasks; using MediaBrowser.Model.IO; using Emby.Drawing.Common; -using MediaBrowser.Common.IO; + using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Net; @@ -75,6 +75,7 @@ namespace Emby.Drawing ImageEnhancers = new List<IImageEnhancer>(); _saveImageSizeTimer = timerFactory.Create(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite); + ImageHelper.ImageProcessor = this; Dictionary<Guid, ImageSize> sizeDictionary; @@ -178,10 +179,15 @@ namespace Emby.Drawing } var originalImage = options.Image; + IHasImages item = options.Item; if (!originalImage.IsLocalFile) { - originalImage = await _libraryManager().ConvertImageToLocal(options.Item, originalImage, options.ImageIndex).ConfigureAwait(false); + if (item == null) + { + item = _libraryManager().GetItemById(options.ItemId); + } + originalImage = await _libraryManager().ConvertImageToLocal(item, originalImage, options.ImageIndex).ConfigureAwait(false); } var originalImagePath = originalImage.Path; @@ -194,13 +200,18 @@ namespace Emby.Drawing if (options.Enhancers.Count > 0) { + if (item == null) + { + item = _libraryManager().GetItemById(options.ItemId); + } + var tuple = await GetEnhancedImage(new ItemImageInfo { DateModified = dateModified, Type = originalImage.Type, Path = originalImagePath - }, options.Item, options.ImageIndex, options.Enhancers).ConfigureAwait(false); + }, item, options.ImageIndex, options.Enhancers).ConfigureAwait(false); originalImagePath = tuple.Item1; dateModified = tuple.Item2; @@ -212,19 +223,12 @@ namespace Emby.Drawing return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); } - ImageSize? originalImageSize = null; - try + ImageSize? originalImageSize = GetSavedImageSize(originalImagePath, dateModified); + if (originalImageSize.HasValue && options.HasDefaultOptions(originalImagePath, originalImageSize.Value)) { - originalImageSize = GetImageSize(originalImagePath, dateModified, true); - if (options.HasDefaultOptions(originalImagePath, originalImageSize.Value)) - { - // Just spit out the original file if all the options are default - return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); - } - } - catch - { - originalImageSize = null; + // Just spit out the original file if all the options are default + _logger.Info("Returning original image {0}", originalImagePath); + return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); } var newSize = ImageHelper.GetNewImageSize(options, originalImageSize); @@ -243,7 +247,18 @@ namespace Emby.Drawing var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(cacheFilePath)); _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath)); - _imageEncoder.EncodeImage(originalImagePath, originalImageSize, tmpPath, AutoOrient(options.Item), quality, options, outputFormat); + if (item == null && string.Equals(options.ItemType, typeof(Photo).Name, StringComparison.OrdinalIgnoreCase)) + { + item = _libraryManager().GetItemById(options.ItemId); + } + + var resultPath =_imageEncoder.EncodeImage(originalImagePath, dateModified, tmpPath, AutoOrient(item), quality, options, outputFormat); + + if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase)) + { + return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); + } + CopyFile(tmpPath, cacheFilePath); return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath)); @@ -422,24 +437,70 @@ namespace Emby.Drawing throw new ArgumentNullException("path"); } - var name = path + "datemodified=" + imageDateModified.Ticks; - ImageSize size; - var cacheHash = name.GetMD5(); + var cacheHash = GetImageSizeKey(path, imageDateModified); if (!_cachedImagedSizes.TryGetValue(cacheHash, out size)) { size = GetImageSizeInternal(path, allowSlowMethod); - if (size.Width > 0 && size.Height > 0) + SaveImageSize(size, cacheHash, false); + } + + return size; + } + + public void SaveImageSize(string path, DateTime imageDateModified, ImageSize size) + { + var cacheHash = GetImageSizeKey(path, imageDateModified); + SaveImageSize(size, cacheHash, true); + } + + private void SaveImageSize(ImageSize size, Guid cacheHash, bool checkExists) + { + if (size.Width <= 0 || size.Height <= 0) + { + return; + } + + if (checkExists && _cachedImagedSizes.ContainsKey(cacheHash)) + { + return; + } + + if (checkExists) + { + if (_cachedImagedSizes.TryAdd(cacheHash, size)) { StartSaveImageSizeTimer(); - _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size); } } + else + { + StartSaveImageSizeTimer(); + _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size); + } + } - return size; + private Guid GetImageSizeKey(string path, DateTime imageDateModified) + { + var name = path + "datemodified=" + imageDateModified.Ticks; + return name.GetMD5(); + } + + public ImageSize? GetSavedImageSize(string path, DateTime imageDateModified) + { + ImageSize size; + + var cacheHash = GetImageSizeKey(path, imageDateModified); + + if (_cachedImagedSizes.TryGetValue(cacheHash, out size)) + { + return size; + } + + return null; } /// <summary> @@ -624,7 +685,7 @@ namespace Emby.Drawing var ehnancedImagePath = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false); // If the path changed update dateModified - if (!ehnancedImagePath.Equals(originalImagePath, StringComparison.OrdinalIgnoreCase)) + if (!string.Equals(ehnancedImagePath, originalImagePath, StringComparison.OrdinalIgnoreCase)) { return GetResult(ehnancedImagePath); } @@ -783,7 +844,7 @@ namespace Emby.Drawing return Path.Combine(path, filename); } - public async Task CreateImageCollage(ImageCollageOptions options) + public void CreateImageCollage(ImageCollageOptions options) { _logger.Info("Creating image collage and saving to {0}", options.OutputPath); |
