The Free Software Media System - Server Backend & API
fix duplicate connections on the dashboard
LukePulverentibae89ee8243f
diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.csindex 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.HttpClientManageroptions.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<string> lengthValues;if (!response.Headers.TryGetValues("content-length", out lengthValues) &&@@ -303,7 +270,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager// We're not able to track progressusing (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.HttpClientManagerusing (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/// <param name="ex">The ex.</param>/// <param name="options">The options.</param>/// <param name="tempFile">The temp file.</param>- /// <param name="serverSupportsRangeRequests">if set to <c>true</c> [server supports range requests].</param>- /// <param name="resumeCount">The resume count.</param>/// <returns>Task.</returns>/// <exception cref="HttpException"></exception>- 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// Cleanupif (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.csindex 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.csindex a45acb206e..98d02a9f99 100644--- a/MediaBrowser.Common/Net/HttpRequestOptions.cs+++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs@@ -32,12 +32,6 @@ namespace MediaBrowser.Common.Net/// <value>The user agent.</value>public string UserAgent { get; set; }- /// <summary>- /// Gets or sets the max resume count.- /// </summary>- /// <value>The max resume count.</value>- public int MaxResumeCount { get; set; }-/// <summary>/// Gets or sets the progress./// </summary>diff --git a/MediaBrowser.Controller/Drawing/ImageExtensions.cs b/MediaBrowser.Controller/Drawing/ImageExtensions.csindex cecbfe74aa..32268011fc 100644--- a/MediaBrowser.Controller/Drawing/ImageExtensions.cs+++ b/MediaBrowser.Controller/Drawing/ImageExtensions.cs@@ -64,6 +64,39 @@ namespace MediaBrowser.Controller.Drawingreturn encoders.FirstOrDefault(i => i.MimeType.Equals(mimeType, StringComparison.OrdinalIgnoreCase)) ?? encoders.FirstOrDefault();}+ /// <summary>+ /// Determines whether [is pixel format supported by graphics object] [the specified format].+ /// </summary>+ /// <param name="format">The format.</param>+ /// <returns><c>true</c> if [is pixel format supported by graphics object] [the specified format]; otherwise, <c>false</c>.</returns>+ 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;+ }+/// <summary>/// Crops an image by removing whitespace and transparency from the edges/// </summary>diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.csindex 3c6f845e57..6bae3510ab 100644--- a/MediaBrowser.Controller/Drawing/ImageManager.cs+++ b/MediaBrowser.Controller/Drawing/ImageManager.cs@@ -179,7 +179,7 @@ namespace MediaBrowser.Controller.Drawingvar 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 resolutionthumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);diff --git a/MediaBrowser.Controller/Library/IUserManager.cs b/MediaBrowser.Controller/Library/IUserManager.csindex 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./// </summary>/// <value>The active connections.</value>- IEnumerable<ClientConnectionInfo> ConnectedUsers { get; }+ IEnumerable<ClientConnectionInfo> RecentConnections { get; }/// <summary>/// Occurs when [playback start].diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.csindex 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/// <summary>/// The _active connections/// </summary>- private readonly ConcurrentBag<ClientConnectionInfo> _activeConnections =- new ConcurrentBag<ClientConnectionInfo>();+ private readonly List<ClientConnectionInfo> _activeConnections =+ new List<ClientConnectionInfo>();/// <summary>/// The _users@@ -67,7 +67,7 @@ namespace MediaBrowser.Server.Implementations.Library/// Gets all connections./// </summary>/// <value>All connections.</value>- private IEnumerable<ClientConnectionInfo> AllConnections+ public IEnumerable<ClientConnectionInfo> 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./// </summary>/// <value>The active connections.</value>- public IEnumerable<ClientConnectionInfo> ConnectedUsers+ public IEnumerable<ClientConnectionInfo> RecentConnections{get { return AllConnections.Where(c => (DateTime.UtcNow - c.LastActivityDate).TotalMinutes <= 10); }}@@ -303,22 +303,29 @@ namespace MediaBrowser.Server.Implementations.Library/// <returns>ClientConnectionInfo.</returns>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;+ }}/// <summary>diff --git a/MediaBrowser.Server.Implementations/Updates/InstallationManager.cs b/MediaBrowser.Server.Implementations/Updates/InstallationManager.csindex 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.Updatesthrow;}- 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.csindex 3f4c84a879..7428b43897 100644--- a/MediaBrowser.WebDashboard/Api/DashboardService.cs+++ b/MediaBrowser.WebDashboard/Api/DashboardService.cs@@ -125,7 +125,7 @@ namespace MediaBrowser.WebDashboard.Api/// <returns>DashboardInfo.</returns>public static async Task<DashboardInfo> 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.jsindex 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 filediff --git a/Nuget/MediaBrowser.Common.Internal.nuspec b/Nuget/MediaBrowser.Common.Internal.nuspecindex a2a4f88717..7be51fb1a1 100644--- a/Nuget/MediaBrowser.Common.Internal.nuspec+++ b/Nuget/MediaBrowser.Common.Internal.nuspec@@ -2,7 +2,7 @@<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"><metadata><id>MediaBrowser.Common.Internal</id>- <version>3.0.48</version>+ <version>3.0.49</version><title>MediaBrowser.Common.Internal</title><authors>Luke</authors><owners>ebr,Luke,scottisafool</owners>@@ -12,7 +12,7 @@<description>Contains common components shared by Media Browser Theatre and Media Browser Server. Not intended for plugin developer consumption.</description><copyright>Copyright © Media Browser 2013</copyright><dependencies>- <dependency id="MediaBrowser.Common" version="3.0.48" />+ <dependency id="MediaBrowser.Common" version="3.0.49" /><dependency id="NLog" version="2.0.0.2000" /><dependency id="ServiceStack.Text" version="3.9.38" /><dependency id="protobuf-net" version="2.0.0.621" />diff --git a/Nuget/MediaBrowser.Common.nuspec b/Nuget/MediaBrowser.Common.nuspecindex c3ef3537ba..26737dc73d 100644--- a/Nuget/MediaBrowser.Common.nuspec+++ b/Nuget/MediaBrowser.Common.nuspec@@ -2,7 +2,7 @@<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"><metadata><id>MediaBrowser.Common</id>- <version>3.0.48</version>+ <version>3.0.49</version><title>MediaBrowser.Common</title><authors>Media Browser Team</authors><owners>ebr,Luke,scottisafool</owners>diff --git a/Nuget/MediaBrowser.Server.Core.nuspec b/Nuget/MediaBrowser.Server.Core.nuspecindex 7e59d6dab3..a5738419e0 100644--- a/Nuget/MediaBrowser.Server.Core.nuspec+++ b/Nuget/MediaBrowser.Server.Core.nuspec@@ -2,7 +2,7 @@<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"><metadata><id>MediaBrowser.Server.Core</id>- <version>3.0.48</version>+ <version>3.0.49</version><title>Media Browser.Server.Core</title><authors>Media Browser Team</authors><owners>ebr,Luke,scottisafool</owners>@@ -12,7 +12,7 @@<description>Contains core components required to build plugins for Media Browser Server.</description><copyright>Copyright © Media Browser 2013</copyright><dependencies>- <dependency id="MediaBrowser.Common" version="3.0.48" />+ <dependency id="MediaBrowser.Common" version="3.0.49" /></dependencies></metadata><files>