From 7aa9e108214356aa26e9c6b798c9a41744d539b6 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 17 Apr 2013 18:30:20 -0400 Subject: [PATCH] =?UTF-8?q?fixes=20#152=20-=20Gallery=20Images=20=E2=80=93?= =?UTF-8?q?=20some=20show,=20others=20don=E2=80=99t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MediaBrowser.Api/UserService.cs | 8 +- .../Drawing/ImageManager.cs | 74 +++++++++---------- MediaBrowser.Controller/Dto/DtoBuilder.cs | 14 ++-- MediaBrowser.Controller/Dto/UserDtoBuilder.cs | 5 +- .../EntryPoints/WebSocketEvents.cs | 4 +- .../Api/DashboardInfoWebSocketListener.cs | 2 +- .../Api/DashboardService.cs | 10 ++- 7 files changed, 62 insertions(+), 55 deletions(-) diff --git a/MediaBrowser.Api/UserService.cs b/MediaBrowser.Api/UserService.cs index 3f3af743d..44c11e790 100644 --- a/MediaBrowser.Api/UserService.cs +++ b/MediaBrowser.Api/UserService.cs @@ -168,7 +168,9 @@ namespace MediaBrowser.Api { var dtoBuilder = new UserDtoBuilder(Logger); - var users = _userManager.Users.OrderBy(u => u.Name).Select(dtoBuilder.GetUserDto).ToArray(); + var tasks = _userManager.Users.OrderBy(u => u.Name).Select(dtoBuilder.GetUserDto); + + var users = tasks.Select(i => i.Result).ToList(); return ToOptimizedResult(users); } @@ -189,7 +191,7 @@ namespace MediaBrowser.Api var dtoBuilder = new UserDtoBuilder(Logger); - var result = dtoBuilder.GetUserDto(user); + var result = dtoBuilder.GetUserDto(user).Result; return ToOptimizedResult(result); } @@ -305,7 +307,7 @@ namespace MediaBrowser.Api var dtoBuilder = new UserDtoBuilder(Logger); - var result = dtoBuilder.GetUserDto(newUser); + var result = dtoBuilder.GetUserDto(newUser).Result; return ToOptimizedResult(result); } diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index 32ff940b2..d901d4a94 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -137,7 +137,7 @@ namespace MediaBrowser.Controller.Drawing _logger.Error("Error enhancing image", ex); } - var originalImageSize = GetImageSize(originalImagePath, dateModified); + var originalImageSize = await GetImageSize(originalImagePath, dateModified).ConfigureAwait(false); // Determine the output size based on incoming parameters var newSize = DrawingUtils.Resize(originalImageSize, width, height, maxWidth, maxHeight); @@ -149,16 +149,6 @@ namespace MediaBrowser.Controller.Drawing var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality.Value, dateModified); - // Grab the cache file if it already exists - if (File.Exists(cacheFilePath)) - { - using (var fileStream = new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) - { - await fileStream.CopyToAsync(toStream).ConfigureAwait(false); - return; - } - } - var semaphore = GetLock(cacheFilePath); await semaphore.WaitAsync().ConfigureAwait(false); @@ -279,7 +269,7 @@ namespace MediaBrowser.Controller.Drawing /// The date modified. /// Task{ImageSize}. /// imagePath - public ImageSize GetImageSize(string imagePath, DateTime dateModified) + public async Task GetImageSize(string imagePath, DateTime dateModified) { if (string.IsNullOrEmpty(imagePath)) { @@ -288,7 +278,16 @@ namespace MediaBrowser.Controller.Drawing var name = imagePath + "datemodified=" + dateModified.Ticks; - return _cachedImagedSizes.GetOrAdd(name, keyName => GetImageSize(keyName, imagePath)); + ImageSize size; + + if (!_cachedImagedSizes.TryGetValue(name, out size)) + { + size = await GetImageSize(name, imagePath).ConfigureAwait(false); + + _cachedImagedSizes.AddOrUpdate(name, size, (keyName, oldValue) => size); + } + + return size; } protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); @@ -299,30 +298,41 @@ namespace MediaBrowser.Controller.Drawing /// Name of the key. /// The image path. /// ImageSize. - private ImageSize GetImageSize(string keyName, string imagePath) + private async Task GetImageSize(string keyName, string imagePath) { // Now check the file system cache var fullCachePath = ImageSizeCache.GetResourcePath(keyName, ".txt"); + var semaphore = GetLock(fullCachePath); + + await semaphore.WaitAsync().ConfigureAwait(false); + try { - var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); + try + { + var result = File.ReadAllText(fullCachePath).Split('|').Select(i => double.Parse(i, UsCulture)).ToArray(); - return new ImageSize { Width = result[0], Height = result[1] }; + return new ImageSize { Width = result[0], Height = result[1] }; + } + catch (FileNotFoundException) + { + // Cache file doesn't exist no biggie + } + + _logger.Debug("Getting image size for {0}", imagePath); + + var size = ImageHeader.GetDimensions(imagePath, _logger); + + // Update the file system cache + File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture)); + + return new ImageSize { Width = size.Width, Height = size.Height }; } - catch (FileNotFoundException) + finally { - // Cache file doesn't exist no biggie + semaphore.Release(); } - - _logger.Debug("Getting image size for {0}", imagePath); - - var size = ImageHeader.GetDimensions(imagePath, _logger); - - // Update the file system cache - File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture)); - - return new ImageSize { Width = size.Width, Height = size.Height }; } /// @@ -445,11 +455,6 @@ namespace MediaBrowser.Controller.Drawing var croppedImagePath = CroppedImageCache.GetResourcePath(name, Path.GetExtension(originalImagePath)); - if (CroppedImageCache.ContainsFilePath(croppedImagePath)) - { - return croppedImagePath; - } - var semaphore = GetLock(croppedImagePath); await semaphore.WaitAsync().ConfigureAwait(false); @@ -529,11 +534,6 @@ namespace MediaBrowser.Controller.Drawing // All enhanced images are saved as png to allow transparency var enhancedImagePath = EnhancedImageCache.GetResourcePath(cacheGuid + ".png"); - if (EnhancedImageCache.ContainsFilePath(enhancedImagePath)) - { - return enhancedImagePath; - } - var semaphore = GetLock(enhancedImagePath); await semaphore.WaitAsync().ConfigureAwait(false); diff --git a/MediaBrowser.Controller/Dto/DtoBuilder.cs b/MediaBrowser.Controller/Dto/DtoBuilder.cs index 6e33f52e6..504b3d92a 100644 --- a/MediaBrowser.Controller/Dto/DtoBuilder.cs +++ b/MediaBrowser.Controller/Dto/DtoBuilder.cs @@ -75,7 +75,7 @@ namespace MediaBrowser.Controller.Dto { try { - AttachPrimaryImageAspectRatio(dto, item, _logger); + await AttachPrimaryImageAspectRatio(dto, item, _logger).ConfigureAwait(false); } catch (Exception ex) { @@ -138,7 +138,7 @@ namespace MediaBrowser.Controller.Dto { try { - AttachPrimaryImageAspectRatio(dto, item, _logger); + await AttachPrimaryImageAspectRatio(dto, item, _logger).ConfigureAwait(false); } catch (Exception ex) { @@ -198,9 +198,9 @@ namespace MediaBrowser.Controller.Dto /// /// The dto. /// The item. - /// The _logger. + /// The _logger. /// Task. - internal static void AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item, ILogger _logger) + internal static async Task AttachPrimaryImageAspectRatio(IItemDto dto, BaseItem item, ILogger logger) { var path = item.PrimaryImagePath; @@ -218,16 +218,16 @@ namespace MediaBrowser.Controller.Dto try { - size = Kernel.Instance.ImageManager.GetImageSize(path, dateModified); + size = await Kernel.Instance.ImageManager.GetImageSize(path, dateModified).ConfigureAwait(false); } catch (FileNotFoundException) { - _logger.Error("Image file does not exist: {0}", path); + logger.Error("Image file does not exist: {0}", path); return; } catch (Exception ex) { - _logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path); + logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path); return; } diff --git a/MediaBrowser.Controller/Dto/UserDtoBuilder.cs b/MediaBrowser.Controller/Dto/UserDtoBuilder.cs index 5c717529a..ad90a392c 100644 --- a/MediaBrowser.Controller/Dto/UserDtoBuilder.cs +++ b/MediaBrowser.Controller/Dto/UserDtoBuilder.cs @@ -3,6 +3,7 @@ using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using System; +using System.Threading.Tasks; namespace MediaBrowser.Controller.Dto { @@ -31,7 +32,7 @@ namespace MediaBrowser.Controller.Dto /// The user. /// DtoUser. /// user - public UserDto GetUserDto(User user) + public async Task GetUserDto(User user) { if (user == null) { @@ -56,7 +57,7 @@ namespace MediaBrowser.Controller.Dto try { - DtoBuilder.AttachPrimaryImageAspectRatio(dto, user, _logger); + await DtoBuilder.AttachPrimaryImageAspectRatio(dto, user, _logger).ConfigureAwait(false); } catch (Exception ex) { diff --git a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs index a8eb8f4c0..64aa78ec4 100644 --- a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs +++ b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs @@ -241,9 +241,9 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// /// The sender. /// The e. - void userManager_UserUpdated(object sender, GenericEventArgs e) + async void userManager_UserUpdated(object sender, GenericEventArgs e) { - var dto = new UserDtoBuilder(_logger).GetUserDto(e.Argument); + var dto = await new UserDtoBuilder(_logger).GetUserDto(e.Argument).ConfigureAwait(false); _serverManager.SendWebSocketMessage("UserUpdated", dto); } diff --git a/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs b/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs index debe78599..1c4037179 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs @@ -61,7 +61,7 @@ namespace MediaBrowser.WebDashboard.Api /// Task{IEnumerable{TaskInfo}}. protected override Task GetDataToSend(object state) { - return Task.FromResult(DashboardService.GetDashboardInfo(_appHost, Logger, _taskManager, _userManager, _libraryManager)); + return DashboardService.GetDashboardInfo(_appHost, Logger, _taskManager, _userManager, _libraryManager); } } } diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index 04586fbe4..66219e72d 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -177,7 +177,9 @@ namespace MediaBrowser.WebDashboard.Api /// System.Object. public object Get(GetDashboardInfo request) { - return GetDashboardInfo(_appHost, Logger, _taskManager, _userManager, _libraryManager); + var result = GetDashboardInfo(_appHost, Logger, _taskManager, _userManager, _libraryManager).Result; + + return ResultFactory.GetOptimizedResult(RequestContext, result); } /// @@ -189,13 +191,15 @@ namespace MediaBrowser.WebDashboard.Api /// The user manager. /// The library manager. /// DashboardInfo. - public static DashboardInfo GetDashboardInfo(IServerApplicationHost appHost, ILogger logger, ITaskManager taskManager, IUserManager userManager, ILibraryManager libraryManager) + public static async Task GetDashboardInfo(IServerApplicationHost appHost, ILogger logger, ITaskManager taskManager, IUserManager userManager, ILibraryManager libraryManager) { var connections = userManager.RecentConnections.ToArray(); var dtoBuilder = new UserDtoBuilder(logger); - var users = userManager.Users.Where(u => connections.Any(c => new Guid(c.UserId) == u.Id)).Select(dtoBuilder.GetUserDto); + var tasks = userManager.Users.Where(u => connections.Any(c => new Guid(c.UserId) == u.Id)).Select(dtoBuilder.GetUserDto); + + var users = await Task.WhenAll(tasks).ConfigureAwait(false); return new DashboardInfo {