diff options
Diffstat (limited to 'Emby.Drawing/ImageProcessor.cs')
| -rw-r--r-- | Emby.Drawing/ImageProcessor.cs | 107 |
1 files changed, 39 insertions, 68 deletions
diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index e9f8f81f3..a15f75c9a 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -15,10 +15,14 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; -using CommonIO; +using MediaBrowser.Model.IO; using Emby.Drawing.Common; +using MediaBrowser.Common.IO; +using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Net; +using MediaBrowser.Model.Threading; +using TagLib; namespace Emby.Drawing { @@ -61,7 +65,7 @@ namespace Emby.Drawing IFileSystem fileSystem, IJsonSerializer jsonSerializer, IImageEncoder imageEncoder, - int maxConcurrentImageProcesses, Func<ILibraryManager> libraryManager) + int maxConcurrentImageProcesses, Func<ILibraryManager> libraryManager, ITimerFactory timerFactory) { _logger = logger; _fileSystem = fileSystem; @@ -71,7 +75,7 @@ namespace Emby.Drawing _appPaths = appPaths; ImageEnhancers = new List<IImageEnhancer>(); - _saveImageSizeTimer = new Timer(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite); + _saveImageSizeTimer = timerFactory.Create(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite); Dictionary<Guid, ImageSize> sizeDictionary; @@ -85,7 +89,7 @@ namespace Emby.Drawing // No biggie sizeDictionary = new Dictionary<Guid, ImageSize>(); } - catch (DirectoryNotFoundException) + catch (IOException) { // No biggie sizeDictionary = new Dictionary<Guid, ImageSize>(); @@ -152,7 +156,7 @@ namespace Emby.Drawing { var file = await ProcessImage(options).ConfigureAwait(false); - using (var fileStream = _fileSystem.GetFileStream(file.Item1, FileMode.Open, FileAccess.Read, FileShare.Read, true)) + using (var fileStream = _fileSystem.GetFileStream(file.Item1, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true)) { await fileStream.CopyToAsync(toStream).ConfigureAwait(false); } @@ -232,7 +236,7 @@ namespace Emby.Drawing var quality = options.Quality; var outputFormat = GetOutputFormat(options.SupportedOutputFormats[0]); - var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor, options.ForegroundLayer); + var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.Blur, options.BackgroundColor, options.ForegroundLayer); var imageProcessingLockTaken = false; @@ -282,7 +286,7 @@ namespace Emby.Drawing { try { - File.Copy(src, destination, true); + _fileSystem.CopyFile(src, destination, true); } catch { @@ -428,17 +432,12 @@ namespace Emby.Drawing return GetResult(croppedImagePath); } - var imageProcessingLockTaken = false; - try { _fileSystem.CreateDirectory(Path.GetDirectoryName(croppedImagePath)); var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(croppedImagePath)); _fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath)); - await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); - imageProcessingLockTaken = true; - _imageEncoder.CropWhiteSpace(originalImagePath, tmpPath); CopyFile(tmpPath, croppedImagePath); return GetResult(tmpPath); @@ -455,13 +454,6 @@ namespace Emby.Drawing return new Tuple<string, DateTime>(originalImagePath, dateModified); } - finally - { - if (imageProcessingLockTaken) - { - _imageProcessingSemaphore.Release(); - } - } } private Tuple<string, DateTime> GetResult(string path) @@ -477,7 +469,7 @@ namespace Emby.Drawing /// <summary> /// Gets the cache file path based on a set of parameters /// </summary> - private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, string backgroundColor, string foregroundLayer) + private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, int? blur, string backgroundColor, string foregroundLayer) { var filename = originalPath; @@ -506,6 +498,11 @@ namespace Emby.Drawing filename += "p=" + unwatchedCount.Value; } + if (blur.HasValue) + { + filename += "blur=" + blur.Value; + } + if (!string.IsNullOrEmpty(backgroundColor)) { filename += "b=" + backgroundColor; @@ -576,7 +573,7 @@ namespace Emby.Drawing { try { - using (var file = TagLib.File.Create(path)) + using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(path), _fileSystem.OpenRead(path), null))) { var image = file as TagLib.Image.File; @@ -596,7 +593,7 @@ namespace Emby.Drawing return ImageHeader.GetDimensions(path, _logger, _fileSystem); } - private readonly Timer _saveImageSizeTimer; + private readonly ITimer _saveImageSizeTimer; private const int SaveImageSizeTimeout = 5000; private readonly object _saveImageSizeLock = new object(); private void StartSaveImageSizeTimer() @@ -778,40 +775,38 @@ namespace Emby.Drawing // All enhanced images are saved as png to allow transparency var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + ".png"); - var semaphore = GetLock(enhancedImagePath); - - await semaphore.WaitAsync().ConfigureAwait(false); - // Check again in case of contention if (_fileSystem.FileExists(enhancedImagePath)) { - semaphore.Release(); return enhancedImagePath; } - var imageProcessingLockTaken = false; + _fileSystem.CreateDirectory(Path.GetDirectoryName(enhancedImagePath)); - try - { - _fileSystem.CreateDirectory(Path.GetDirectoryName(enhancedImagePath)); + var tmpPath = Path.Combine(_appPaths.TempDirectory, Path.ChangeExtension(Guid.NewGuid().ToString(), Path.GetExtension(enhancedImagePath))); + _fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath)); - await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); + await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); - imageProcessingLockTaken = true; + try + { + await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, tmpPath, item, imageType, imageIndex).ConfigureAwait(false); - await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, enhancedImagePath, item, imageType, imageIndex).ConfigureAwait(false); + try + { + _fileSystem.CopyFile(tmpPath, enhancedImagePath, true); + } + catch + { + + } } finally { - if (imageProcessingLockTaken) - { - _imageProcessingSemaphore.Release(); - } - - semaphore.Release(); + _imageProcessingSemaphore.Release(); } - return enhancedImagePath; + return tmpPath; } /// <summary> @@ -837,21 +832,6 @@ namespace Emby.Drawing } /// <summary> - /// The _semaphoreLocks - /// </summary> - private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>(); - - /// <summary> - /// Gets the lock. - /// </summary> - /// <param name="filename">The filename.</param> - /// <returns>System.Object.</returns> - private SemaphoreSlim GetLock(string filename) - { - return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); - } - - /// <summary> /// Gets the cache path. /// </summary> /// <param name="path">The path.</param> @@ -917,20 +897,11 @@ namespace Emby.Drawing public async Task CreateImageCollage(ImageCollageOptions options) { - await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); - - try - { - _logger.Info("Creating image collage and saving to {0}", options.OutputPath); + _logger.Info("Creating image collage and saving to {0}", options.OutputPath); - _imageEncoder.CreateImageCollage(options); + _imageEncoder.CreateImageCollage(options); - _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath); - } - finally - { - _imageProcessingSemaphore.Release(); - } + _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath); } public IEnumerable<IImageEnhancer> GetSupportedEnhancers(IHasImages item, ImageType imageType) |
