diff options
| author | Luke <luke.pulverenti@gmail.com> | 2015-10-26 18:50:19 -0400 |
|---|---|---|
| committer | Luke <luke.pulverenti@gmail.com> | 2015-10-26 18:50:19 -0400 |
| commit | 35778ebc02e5931142a1fe31a256b7488a07c5c2 (patch) | |
| tree | ced0290be8820f5e507b51ca4c5165212b1879d1 /Emby.Drawing/ImageProcessor.cs | |
| parent | c0dc8d055bfd4d2f58591083beb9e9128357aad6 (diff) | |
| parent | 8d77308593c3b16b733b0109323770d9dfe7e166 (diff) | |
Merge pull request #1222 from MediaBrowser/dev
3.0.5768.7
Diffstat (limited to 'Emby.Drawing/ImageProcessor.cs')
| -rw-r--r-- | Emby.Drawing/ImageProcessor.cs | 115 |
1 files changed, 69 insertions, 46 deletions
diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index 231bfa2b4..05d89406b 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -1,5 +1,4 @@ -using Emby.Drawing.Common; -using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Controller; using MediaBrowser.Controller.Drawing; @@ -17,6 +16,9 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using CommonIO; +using Emby.Drawing.Common; +using MediaBrowser.Controller.Library; namespace Emby.Drawing { @@ -52,18 +54,20 @@ namespace Emby.Drawing private readonly IServerApplicationPaths _appPaths; private readonly IImageEncoder _imageEncoder; private readonly SemaphoreSlim _imageProcessingSemaphore; + private readonly Func<ILibraryManager> _libraryManager; public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IImageEncoder imageEncoder, - int maxConcurrentImageProcesses) + int maxConcurrentImageProcesses, Func<ILibraryManager> libraryManager) { _logger = logger; _fileSystem = fileSystem; _jsonSerializer = jsonSerializer; _imageEncoder = imageEncoder; + _libraryManager = libraryManager; _appPaths = appPaths; ImageEnhancers = new List<IImageEnhancer>(); @@ -106,6 +110,15 @@ namespace Emby.Drawing } } + + public bool SupportsImageCollageCreation + { + get + { + return _imageEncoder.SupportsImageCollageCreation; + } + } + private string ResizedImageCachePath { get @@ -157,7 +170,19 @@ namespace Emby.Drawing throw new ArgumentNullException("options"); } - var originalImagePath = options.Image.Path; + var originalImage = options.Image; + + if (!originalImage.IsLocalFile) + { + originalImage = await _libraryManager().ConvertImageToLocal(options.Item, originalImage, options.ImageIndex).ConfigureAwait(false); + } + + var originalImagePath = originalImage.Path; + + if (!_imageEncoder.SupportsImageEncoding) + { + return originalImagePath; + } if (options.HasDefaultOptions(originalImagePath) && options.Enhancers.Count == 0 && !options.CropWhiteSpace) { @@ -165,9 +190,9 @@ namespace Emby.Drawing return originalImagePath; } - var dateModified = options.Image.DateModified; + var dateModified = originalImage.DateModified; - if (options.CropWhiteSpace) + if (options.CropWhiteSpace && _imageEncoder.SupportsImageEncoding) { var tuple = await GetWhitespaceCroppedImage(originalImagePath, dateModified).ConfigureAwait(false); @@ -180,7 +205,7 @@ namespace Emby.Drawing var tuple = await GetEnhancedImage(new ItemImageInfo { DateModified = dateModified, - Type = options.Image.Type, + Type = originalImage.Type, Path = originalImagePath }, options.Item, options.ImageIndex, options.Enhancers).ConfigureAwait(false); @@ -215,21 +240,18 @@ namespace Emby.Drawing { CheckDisposed(); - if (!File.Exists(cacheFilePath)) + if (!_fileSystem.FileExists(cacheFilePath)) { var newWidth = Convert.ToInt32(newSize.Width); var newHeight = Convert.ToInt32(newSize.Height); - Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); + _fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); imageProcessingLockTaken = true; _imageEncoder.EncodeImage(originalImagePath, cacheFilePath, newWidth, newHeight, quality, options); - - // ImageMagick doesn't seem to always release it right away - await Task.Delay(100).ConfigureAwait(false); } } finally @@ -270,7 +292,7 @@ namespace Emby.Drawing await semaphore.WaitAsync().ConfigureAwait(false); // Check again in case of contention - if (File.Exists(croppedImagePath)) + if (_fileSystem.FileExists(croppedImagePath)) { semaphore.Release(); return GetResult(croppedImagePath); @@ -280,13 +302,18 @@ namespace Emby.Drawing try { - Directory.CreateDirectory(Path.GetDirectoryName(croppedImagePath)); + _fileSystem.CreateDirectory(Path.GetDirectoryName(croppedImagePath)); await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); imageProcessingLockTaken = true; _imageEncoder.CropWhiteSpace(originalImagePath, croppedImagePath); } + catch (NotImplementedException) + { + // No need to spam the log with an error message + return new Tuple<string, DateTime>(originalImagePath, dateModified); + } catch (Exception ex) { // We have to have a catch-all here because some of the .net image methods throw a plain old Exception @@ -359,19 +386,14 @@ namespace Emby.Drawing return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower()); } - /// <summary> - /// Gets the size of the image. - /// </summary> - /// <param name="path">The path.</param> - /// <returns>ImageSize.</returns> - public ImageSize GetImageSize(string path) + public ImageSize GetImageSize(ItemImageInfo info) { - return GetImageSize(path, File.GetLastWriteTimeUtc(path), false); + return GetImageSize(info.Path, info.DateModified, false); } - public ImageSize GetImageSize(ItemImageInfo info) + public ImageSize GetImageSize(string path) { - return GetImageSize(info.Path, info.DateModified, false); + return GetImageSize(path, _fileSystem.GetLastWriteTimeUtc(path), false); } /// <summary> @@ -399,7 +421,11 @@ namespace Emby.Drawing { size = GetImageSizeInternal(path, allowSlowMethod); - _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size); + if (size.Width > 0 && size.Height > 0) + { + StartSaveImageSizeTimer(); + _cachedImagedSizes.AddOrUpdate(cacheHash, size, (keyName, oldValue) => size); + } } return size; @@ -413,28 +439,26 @@ namespace Emby.Drawing /// <returns>ImageSize.</returns> private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod) { - ImageSize size; - try { - size = ImageHeader.GetDimensions(path, _logger, _fileSystem); - } - catch - { - if (!allowSlowMethod) + using (var file = TagLib.File.Create(path)) { - throw; - } - //_logger.Info("Failed to read image header for {0}. Doing it the slow way.", path); + var image = file as TagLib.Image.File; - CheckDisposed(); + var properties = image.Properties; - size = _imageEncoder.GetImageSize(path); + return new ImageSize + { + Height = properties.PhotoHeight, + Width = properties.PhotoWidth + }; + } + } + catch + { } - StartSaveImageSizeTimer(); - - return size; + return ImageHeader.GetDimensions(path, _logger, _fileSystem); } private readonly Timer _saveImageSizeTimer; @@ -452,7 +476,7 @@ namespace Emby.Drawing try { var path = ImageSizeFile; - Directory.CreateDirectory(Path.GetDirectoryName(path)); + _fileSystem.CreateDirectory(Path.GetDirectoryName(path)); _jsonSerializer.SerializeToFile(_cachedImagedSizes, path); } catch (Exception ex) @@ -624,7 +648,7 @@ namespace Emby.Drawing await semaphore.WaitAsync().ConfigureAwait(false); // Check again in case of contention - if (File.Exists(enhancedImagePath)) + if (_fileSystem.FileExists(enhancedImagePath)) { semaphore.Release(); return enhancedImagePath; @@ -634,7 +658,7 @@ namespace Emby.Drawing try { - Directory.CreateDirectory(Path.GetDirectoryName(enhancedImagePath)); + _fileSystem.CreateDirectory(Path.GetDirectoryName(enhancedImagePath)); await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); @@ -773,11 +797,11 @@ namespace Emby.Drawing try { - _logger.Debug("Creating image collage and saving to {0}", options.OutputPath); + _logger.Info("Creating image collage and saving to {0}", options.OutputPath); _imageEncoder.CreateImageCollage(options); - _logger.Debug("Completed creation of image collage and saved to {0}", options.OutputPath); + _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath); } finally { @@ -799,7 +823,6 @@ namespace Emby.Drawing return false; } - }); } @@ -819,4 +842,4 @@ namespace Emby.Drawing } } } -} +}
\ No newline at end of file |
