diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs | 98 |
1 files changed, 21 insertions, 77 deletions
diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs index 86bd6b731..09071cbf9 100644 --- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs +++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs @@ -118,14 +118,19 @@ namespace MediaBrowser.Server.Implementations.Drawing public async Task ProcessImage(ImageProcessingOptions options, Stream toStream) { - if (options == null) + var file = await ProcessImage(options).ConfigureAwait(false); + + using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true)) { - throw new ArgumentNullException("options"); + await fileStream.CopyToAsync(toStream).ConfigureAwait(false); } + } - if (toStream == null) + public async Task<string> ProcessImage(ImageProcessingOptions options) + { + if (options == null) { - throw new ArgumentNullException("toStream"); + throw new ArgumentNullException("options"); } var originalImagePath = options.Image.Path; @@ -133,11 +138,7 @@ namespace MediaBrowser.Server.Implementations.Drawing if (options.HasDefaultOptions() && options.Enhancers.Count == 0 && !options.CropWhiteSpace) { // Just spit out the original file if all the options are default - using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true)) - { - await fileStream.CopyToAsync(toStream).ConfigureAwait(false); - return; - } + return originalImagePath; } var dateModified = options.Image.DateModified; @@ -166,30 +167,13 @@ namespace MediaBrowser.Server.Implementations.Drawing if (options.HasDefaultOptionsWithoutSize() && newSize.Equals(originalImageSize) && options.Enhancers.Count == 0) { // Just spit out the original file if the new size equals the old - using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true)) - { - await fileStream.CopyToAsync(toStream).ConfigureAwait(false); - return; - } + return originalImagePath; } var quality = options.Quality ?? 90; var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor); - try - { - using (var fileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, true)) - { - await fileStream.CopyToAsync(toStream).ConfigureAwait(false); - return; - } - } - catch (IOException) - { - // Cache file doesn't exist or is currently being written to - } - var semaphore = GetLock(cacheFilePath); await semaphore.WaitAsync().ConfigureAwait(false); @@ -197,17 +181,12 @@ namespace MediaBrowser.Server.Implementations.Drawing // Check again in case of lock contention try { - using (var fileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, true)) + if (File.Exists(cacheFilePath)) { - await fileStream.CopyToAsync(toStream).ConfigureAwait(false); semaphore.Release(); - return; + return cacheFilePath; } } - catch (IOException) - { - // Cache file doesn't exist or is currently being written to - } catch { semaphore.Release(); @@ -218,37 +197,6 @@ namespace MediaBrowser.Server.Implementations.Drawing { var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed.HasValue; - //if (!hasPostProcessing) - //{ - // using (var outputStream = await _mediaEncoder.EncodeImage(new ImageEncodingOptions - // { - // InputPath = originalImagePath, - // MaxHeight = options.MaxHeight, - // MaxWidth = options.MaxWidth, - // Height = options.Height, - // Width = options.Width, - // Quality = options.Quality, - // Format = options.OutputFormat == ImageOutputFormat.Original ? Path.GetExtension(originalImagePath).TrimStart('.') : options.OutputFormat.ToString().ToLower() - - // }, CancellationToken.None).ConfigureAwait(false)) - // { - // using (var outputMemoryStream = new MemoryStream()) - // { - // // Save to the memory stream - // await outputStream.CopyToAsync(outputMemoryStream).ConfigureAwait(false); - - // var bytes = outputMemoryStream.ToArray(); - - // await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); - - // // kick off a task to cache the result - // await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false); - // } - - // return; - // } - //} - using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true)) { // Copy to memory stream to avoid Image locking file @@ -289,18 +237,16 @@ namespace MediaBrowser.Server.Implementations.Drawing var outputFormat = GetOutputFormat(originalImage, options.OutputFormat); - using (var outputMemoryStream = new MemoryStream()) + Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); + + // Save to the cache location + using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false)) { // Save to the memory stream - thumbnail.Save(outputFormat, outputMemoryStream, quality); - - var bytes = outputMemoryStream.ToArray(); - - await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); - - // kick off a task to cache the result - await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false); + thumbnail.Save(outputFormat, cacheFileStream, quality); } + + return cacheFilePath; } } @@ -324,9 +270,7 @@ namespace MediaBrowser.Server.Implementations.Drawing { try { - var parentPath = Path.GetDirectoryName(cacheFilePath); - - Directory.CreateDirectory(parentPath); + Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); // Save to the cache location using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true)) |
