From 85d7eb917f1cd7530e28fb0565564c79ddb910ed Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 16 Sep 2013 22:44:06 -0400 Subject: made dtoservice synchronous --- MediaBrowser.Controller/Drawing/ImageHeader.cs | 10 ++- MediaBrowser.Controller/Drawing/ImageManager.cs | 84 +++++++++++++------------ 2 files changed, 49 insertions(+), 45 deletions(-) (limited to 'MediaBrowser.Controller/Drawing') diff --git a/MediaBrowser.Controller/Drawing/ImageHeader.cs b/MediaBrowser.Controller/Drawing/ImageHeader.cs index bca897a4cd..11072ff0ca 100644 --- a/MediaBrowser.Controller/Drawing/ImageHeader.cs +++ b/MediaBrowser.Controller/Drawing/ImageHeader.cs @@ -1,6 +1,4 @@ -using System.Threading.Tasks; -using MediaBrowser.Common.IO; -using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; using System.Drawing; @@ -41,7 +39,7 @@ namespace MediaBrowser.Controller.Drawing /// The logger. /// The dimensions of the specified image. /// The image was of an unrecognised format. - public static async Task GetDimensions(string path, ILogger logger) + public static Size GetDimensions(string path, ILogger logger) { try { @@ -59,11 +57,11 @@ namespace MediaBrowser.Controller.Drawing } // Buffer to memory stream to avoid image locking file - using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true)) + using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var memoryStream = new MemoryStream()) { - await fs.CopyToAsync(memoryStream).ConfigureAwait(false); + fs.CopyTo(memoryStream); // Co it the old fashioned way using (var b = Image.FromStream(memoryStream, true, false)) diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index 51ae6b7ca7..05f45a4574 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -68,11 +68,6 @@ namespace MediaBrowser.Controller.Drawing private readonly IItemRepository _itemRepo; - /// - /// The _locks - /// - private readonly ConcurrentDictionary _locks = new ConcurrentDictionary(); - /// /// Initializes a new instance of the class. /// @@ -146,7 +141,7 @@ namespace MediaBrowser.Controller.Drawing } } - var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false); + var originalImageSize = GetImageSize(originalImagePath, dateModified); // Determine the output size based on incoming parameters var newSize = DrawingUtils.Resize(originalImageSize, width, height, maxWidth, maxHeight); @@ -304,7 +299,7 @@ namespace MediaBrowser.Controller.Drawing /// The date modified. /// Task{ImageSize}. /// imagePath - public async Task GetImageSize(string imagePath, DateTime dateModified) + public ImageSize GetImageSize(string imagePath, DateTime dateModified) { if (string.IsNullOrEmpty(imagePath)) { @@ -317,7 +312,7 @@ namespace MediaBrowser.Controller.Drawing if (!_cachedImagedSizes.TryGetValue(name, out size)) { - size = await GetImageSize(name, imagePath).ConfigureAwait(false); + size = GetImageSize(name, imagePath); _cachedImagedSizes.AddOrUpdate(name, size, (keyName, oldValue) => size); } @@ -333,7 +328,7 @@ namespace MediaBrowser.Controller.Drawing /// Name of the key. /// The image path. /// ImageSize. - private async Task GetImageSize(string keyName, string imagePath) + private ImageSize GetImageSize(string keyName, string imagePath) { // Now check the file system cache var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".txt"); @@ -349,34 +344,29 @@ namespace MediaBrowser.Controller.Drawing // Cache file doesn't exist or is currently being written to } - var semaphore = GetLock(fullCachePath); - - await semaphore.WaitAsync().ConfigureAwait(false); - - try - { - var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); + var syncLock = GetObjectLock(fullCachePath); - return new ImageSize { Width = result[0], Height = result[1] }; - } - catch (FileNotFoundException) + lock (syncLock) { - // Cache file doesn't exist no biggie - } - catch (DirectoryNotFoundException) - { - // Cache file doesn't exist no biggie - } - catch - { - semaphore.Release(); + try + { + var result = File.ReadAllText(fullCachePath) + .Split('|') + .Select(i => double.Parse(i, UsCulture)) + .ToArray(); - throw; - } + return new ImageSize { Width = result[0], Height = result[1] }; + } + catch (FileNotFoundException) + { + // Cache file doesn't exist no biggie + } + catch (DirectoryNotFoundException) + { + // Cache file doesn't exist no biggie + } - try - { - var size = await ImageHeader.GetDimensions(imagePath, _logger).ConfigureAwait(false); + var size = ImageHeader.GetDimensions(imagePath, _logger); var parentPath = Path.GetDirectoryName(fullCachePath); @@ -390,10 +380,6 @@ namespace MediaBrowser.Controller.Drawing return new ImageSize { Width = size.Width, Height = size.Height }; } - finally - { - semaphore.Release(); - } } /// @@ -600,7 +586,7 @@ namespace MediaBrowser.Controller.Drawing return GetEnhancedImage(originalImagePath, dateModified, item, imageType, imageIndex, supportedImageEnhancers); } - + /// /// Runs an image through the image enhancers, caches the result, and returns the cached path /// @@ -786,6 +772,11 @@ namespace MediaBrowser.Controller.Drawing return result; } + /// + /// The _semaphoreLocks + /// + private readonly ConcurrentDictionary _semaphoreLocks = new ConcurrentDictionary(); + /// /// Gets the lock. /// @@ -793,7 +784,22 @@ namespace MediaBrowser.Controller.Drawing /// System.Object. private SemaphoreSlim GetLock(string filename) { - return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1)); + } + + /// + /// The _semaphoreLocks + /// + private readonly ConcurrentDictionary _locks = new ConcurrentDictionary(); + + /// + /// Gets the lock. + /// + /// The filename. + /// System.Object. + private object GetObjectLock(string filename) + { + return _locks.GetOrAdd(filename, key => new object()); } } } -- cgit v1.2.3