From bae89ee8243fea06ba1e17c1f1ed6da890e9f08c Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sat, 16 Mar 2013 12:41:49 -0400 Subject: [PATCH] fix duplicate connections on the dashboard --- .../HttpClientManager/HttpClientManager.cs | 64 +++---------------- .../Updates/PackageManager.cs | 3 +- MediaBrowser.Common/Net/HttpRequestOptions.cs | 6 -- .../Drawing/ImageExtensions.cs | 33 ++++++++++ .../Drawing/ImageManager.cs | 2 +- .../Library/IUserManager.cs | 2 +- .../Library/UserManager.cs | 39 ++++++----- .../Updates/InstallationManager.cs | 4 +- .../Api/DashboardService.cs | 2 +- MediaBrowser.WebDashboard/ApiClient.js | 2 +- Nuget/MediaBrowser.Common.Internal.nuspec | 4 +- Nuget/MediaBrowser.Common.nuspec | 2 +- Nuget/MediaBrowser.Server.Core.nuspec | 4 +- 13 files changed, 79 insertions(+), 88 deletions(-) diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 56b4efd2f0..cba2688131 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -252,30 +252,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager _logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", options.Url, tempFile); - FileStream tempFileStream; - - if (resumeCount > 0 && File.Exists(tempFile)) - { - tempFileStream = new FileStream(tempFile, FileMode.Open, FileAccess.Write, FileShare.Read, - StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous); - - var startPosition = tempFileStream.Length; - tempFileStream.Seek(startPosition, SeekOrigin.Current); - - message.Headers.Range = new RangeHeaderValue(startPosition, null); - - _logger.Info("Resuming from position {1} for {0}", options.Url, startPosition); - } - else - { - tempFileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, - StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous); - } - - var serverSupportsRangeRequests = false; - - Exception downloadException = null; - try { options.CancellationToken.ThrowIfCancellationRequested(); @@ -286,15 +262,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager options.CancellationToken.ThrowIfCancellationRequested(); - var rangeValue = string.Join(" ", response.Headers.AcceptRanges.ToArray()); - serverSupportsRangeRequests = rangeValue.IndexOf("bytes", StringComparison.OrdinalIgnoreCase) != -1 || rangeValue.IndexOf("*", StringComparison.OrdinalIgnoreCase) != -1; - - if (!serverSupportsRangeRequests && resumeCount > 0) - { - _logger.Info("Server does not support range requests for {0}", options.Url); - tempFileStream.Position = 0; - } - IEnumerable lengthValues; if (!response.Headers.TryGetValues("content-length", out lengthValues) && @@ -303,7 +270,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager // We're not able to track progress using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { - await stream.CopyToAsync(tempFileStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + { + await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + } } } else @@ -312,7 +282,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, length)) { - await stream.CopyToAsync(tempFileStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) + { + await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false); + } } } @@ -323,23 +296,16 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } catch (Exception ex) { - downloadException = ex; + HandleTempFileException(ex, options, tempFile); } finally { - tempFileStream.Dispose(); - if (options.ResourcePool != null) { options.ResourcePool.Release(); } } - if (downloadException != null) - { - await HandleTempFileException(downloadException, options, tempFile, serverSupportsRangeRequests, resumeCount).ConfigureAwait(false); - } - return tempFile; } @@ -349,11 +315,9 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager /// The ex. /// The options. /// The temp file. - /// if set to true [server supports range requests]. - /// The resume count. /// Task. /// - private Task HandleTempFileException(Exception ex, HttpRequestOptions options, string tempFile, bool serverSupportsRangeRequests, int resumeCount) + private void HandleTempFileException(Exception ex, HttpRequestOptions options, string tempFile) { var operationCanceledException = ex as OperationCanceledException; @@ -375,14 +339,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager // Cleanup if (File.Exists(tempFile)) { - // Try to resume - if (httpRequestException != null && serverSupportsRangeRequests && resumeCount < options.MaxResumeCount && new FileInfo(tempFile).Length > 0) - { - _logger.Info("Attempting to resume download from {0}", options.Url); - - return GetTempFile(options, tempFile, resumeCount + 1); - } - File.Delete(tempFile); } diff --git a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs index 594027bcbd..f3ab27293b 100644 --- a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs +++ b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs @@ -75,8 +75,7 @@ namespace MediaBrowser.Common.Implementations.Updates { Url = package.sourceUrl, CancellationToken = cancellationToken, - Progress = progress, - MaxResumeCount = 3 + Progress = progress }).ConfigureAwait(false); diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs index a45acb206e..98d02a9f99 100644 --- a/MediaBrowser.Common/Net/HttpRequestOptions.cs +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -32,12 +32,6 @@ namespace MediaBrowser.Common.Net /// The user agent. public string UserAgent { get; set; } - /// - /// Gets or sets the max resume count. - /// - /// The max resume count. - public int MaxResumeCount { get; set; } - /// /// Gets or sets the progress. /// diff --git a/MediaBrowser.Controller/Drawing/ImageExtensions.cs b/MediaBrowser.Controller/Drawing/ImageExtensions.cs index cecbfe74aa..32268011fc 100644 --- a/MediaBrowser.Controller/Drawing/ImageExtensions.cs +++ b/MediaBrowser.Controller/Drawing/ImageExtensions.cs @@ -64,6 +64,39 @@ namespace MediaBrowser.Controller.Drawing return encoders.FirstOrDefault(i => i.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase)) ?? encoders.FirstOrDefault(); } + /// + /// Determines whether [is pixel format supported by graphics object] [the specified format]. + /// + /// The format. + /// true if [is pixel format supported by graphics object] [the specified format]; otherwise, false. + public static bool IsPixelFormatSupportedByGraphicsObject(PixelFormat format) + { + // http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx + + if (format.HasFlag(PixelFormat.Indexed)) + { + return false; + } + if (format.HasFlag(PixelFormat.Undefined)) + { + return false; + } + if (format.HasFlag(PixelFormat.DontCare)) + { + return false; + } + if (format.HasFlag(PixelFormat.Format16bppArgb1555)) + { + return false; + } + if (format.HasFlag(PixelFormat.Format16bppGrayScale)) + { + return false; + } + + return true; + } + /// /// Crops an image by removing whitespace and transparency from the edges /// diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index 3c6f845e57..6bae3510ab 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -179,7 +179,7 @@ namespace MediaBrowser.Controller.Drawing var newHeight = Convert.ToInt32(newSize.Height); // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here - var thumbnail = originalImage.PixelFormat.HasFlag(PixelFormat.Indexed) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat); + var thumbnail = !ImageExtensions.IsPixelFormatSupportedByGraphicsObject(originalImage.PixelFormat) ? new Bitmap(originalImage, newWidth, newHeight) : new Bitmap(newWidth, newHeight, originalImage.PixelFormat); // Preserve the original resolution thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution); diff --git a/MediaBrowser.Controller/Library/IUserManager.cs b/MediaBrowser.Controller/Library/IUserManager.cs index f1cef1d660..910ba13b28 100644 --- a/MediaBrowser.Controller/Library/IUserManager.cs +++ b/MediaBrowser.Controller/Library/IUserManager.cs @@ -20,7 +20,7 @@ namespace MediaBrowser.Controller.Library /// Gets the active connections. /// /// The active connections. - IEnumerable ConnectedUsers { get; } + IEnumerable RecentConnections { get; } /// /// Occurs when [playback start]. diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 01a745e95c..1ebf99b6a2 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -25,8 +25,8 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The _active connections /// - private readonly ConcurrentBag _activeConnections = - new ConcurrentBag(); + private readonly List _activeConnections = + new List(); /// /// The _users @@ -67,7 +67,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Gets all connections. /// /// All connections. - private IEnumerable AllConnections + public IEnumerable AllConnections { get { return _activeConnections.Where(c => GetUserById(c.UserId) != null).OrderByDescending(c => c.LastActivityDate); } } @@ -76,7 +76,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Gets the active connections. /// /// The active connections. - public IEnumerable ConnectedUsers + public IEnumerable RecentConnections { get { return AllConnections.Where(c => (DateTime.UtcNow - c.LastActivityDate).TotalMinutes <= 10); } } @@ -303,22 +303,29 @@ namespace MediaBrowser.Server.Implementations.Library /// ClientConnectionInfo. private ClientConnectionInfo GetConnection(Guid userId, ClientType clientType, string deviceId, string deviceName) { - var conn = _activeConnections.FirstOrDefault(c => c.UserId == userId && c.ClientType == clientType && string.Equals(deviceId, c.DeviceId)); - - if (conn == null) + lock (_activeConnections) { - conn = new ClientConnectionInfo + var conn = _activeConnections.FirstOrDefault(c => c.ClientType == clientType && string.Equals(deviceId, c.DeviceId)); + + if (conn == null) { - UserId = userId, - ClientType = clientType, - DeviceName = deviceName, - DeviceId = deviceId - }; + conn = new ClientConnectionInfo + { + UserId = userId, + ClientType = clientType, + DeviceName = deviceName, + DeviceId = deviceId + }; - _activeConnections.Add(conn); - } + _activeConnections.Add(conn); + } + else + { + conn.UserId = userId; + } - return conn; + return conn; + } } /// diff --git a/MediaBrowser.Server.Implementations/Updates/InstallationManager.cs b/MediaBrowser.Server.Implementations/Updates/InstallationManager.cs index bdbf3896ff..271d3d8775 100644 --- a/MediaBrowser.Server.Implementations/Updates/InstallationManager.cs +++ b/MediaBrowser.Server.Implementations/Updates/InstallationManager.cs @@ -375,8 +375,10 @@ namespace MediaBrowser.Server.Implementations.Updates throw; } - catch + catch (Exception ex) { + _logger.ErrorException("Package installation failed", ex); + lock (CurrentInstallations) { CurrentInstallations.Remove(tuple); diff --git a/MediaBrowser.WebDashboard/Api/DashboardService.cs b/MediaBrowser.WebDashboard/Api/DashboardService.cs index 3f4c84a879..7428b43897 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardService.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs @@ -125,7 +125,7 @@ namespace MediaBrowser.WebDashboard.Api /// DashboardInfo. public static async Task GetDashboardInfo(IServerApplicationHost appHost, ILogger logger, ITaskManager taskManager, IUserManager userManager, ILibraryManager libraryManager) { - var connections = userManager.ConnectedUsers.ToArray(); + var connections = userManager.RecentConnections.ToArray(); var dtoBuilder = new DtoBuilder(logger, libraryManager); diff --git a/MediaBrowser.WebDashboard/ApiClient.js b/MediaBrowser.WebDashboard/ApiClient.js index e7025c8901..af26e46146 100644 --- a/MediaBrowser.WebDashboard/ApiClient.js +++ b/MediaBrowser.WebDashboard/ApiClient.js @@ -1387,7 +1387,7 @@ $(document).ajaxSend(function (event, jqXHR) { if (ApiClient.currentUserId) { - var auth = 'MediaBrowser UserId="' + ApiClient.currentUserId + '", Client="Dashboard", Device="' + ApiClient.getDeviceName() + '", DeviceId="' + ApiClient.getDeviceName() + '"'; + var auth = 'MediaBrowser UserId="' + ApiClient.currentUserId + '", Client="Dashboard", Device="' + ApiClient.getDeviceName() + '", DeviceId="' + ApiClient.getDeviceId() + '"'; jqXHR.setRequestHeader("Authorization", auth); } }); \ No newline at end of file diff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspec index a2a4f88717..7be51fb1a1 100644 --- a/Nuget/MediaBrowser.Common.Internal.nuspec +++ b/Nuget/MediaBrowser.Common.Internal.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common.Internal - 3.0.48 + 3.0.49 MediaBrowser.Common.Internal Luke ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains common components shared by Media Browser Theatre and Media Browser Server. Not intended for plugin developer consumption. Copyright © Media Browser 2013 - + diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspec index c3ef3537ba..26737dc73d 100644 --- a/Nuget/MediaBrowser.Common.nuspec +++ b/Nuget/MediaBrowser.Common.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Common - 3.0.48 + 3.0.49 MediaBrowser.Common Media Browser Team ebr,Luke,scottisafool diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspec index 7e59d6dab3..a5738419e0 100644 --- a/Nuget/MediaBrowser.Server.Core.nuspec +++ b/Nuget/MediaBrowser.Server.Core.nuspec @@ -2,7 +2,7 @@ MediaBrowser.Server.Core - 3.0.48 + 3.0.49 Media Browser.Server.Core Media Browser Team ebr,Luke,scottisafool @@ -12,7 +12,7 @@ Contains core components required to build plugins for Media Browser Server. Copyright © Media Browser 2013 - +