Clean up DeviceManager and don't store capabilities on disk

This commit is contained in:
Patrick Barron 2020-11-19 09:38:54 -05:00
parent a8524be6be
commit 5790db05ba
2 changed files with 12 additions and 83 deletions

View File

@ -1,61 +1,38 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using Jellyfin.Data.Entities; using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums; using Jellyfin.Data.Enums;
using Jellyfin.Data.Events; using Jellyfin.Data.Events;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Security; using MediaBrowser.Controller.Security;
using MediaBrowser.Model.Devices; using MediaBrowser.Model.Devices;
using MediaBrowser.Model.Querying; using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Session; using MediaBrowser.Model.Session;
using Microsoft.Extensions.Caching.Memory;
namespace Emby.Server.Implementations.Devices namespace Emby.Server.Implementations.Devices
{ {
public class DeviceManager : IDeviceManager public class DeviceManager : IDeviceManager
{ {
private readonly IMemoryCache _memoryCache;
private readonly IJsonSerializer _json;
private readonly IUserManager _userManager; private readonly IUserManager _userManager;
private readonly IServerConfigurationManager _config;
private readonly IAuthenticationRepository _authRepo; private readonly IAuthenticationRepository _authRepo;
private readonly object _capabilitiesSyncLock = new object(); private readonly ConcurrentDictionary<string, ClientCapabilities> _capabilitiesMap = new ();
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated; public DeviceManager(IAuthenticationRepository authRepo, IUserManager userManager)
public DeviceManager(
IAuthenticationRepository authRepo,
IJsonSerializer json,
IUserManager userManager,
IServerConfigurationManager config,
IMemoryCache memoryCache)
{ {
_json = json;
_userManager = userManager; _userManager = userManager;
_config = config;
_memoryCache = memoryCache;
_authRepo = authRepo; _authRepo = authRepo;
} }
public event EventHandler<GenericEventArgs<Tuple<string, DeviceOptions>>> DeviceOptionsUpdated;
public void SaveCapabilities(string deviceId, ClientCapabilities capabilities) public void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
{ {
var path = Path.Combine(GetDevicePath(deviceId), "capabilities.json"); _capabilitiesMap[deviceId] = capabilities;
Directory.CreateDirectory(Path.GetDirectoryName(path));
lock (_capabilitiesSyncLock)
{
_memoryCache.Set(deviceId, capabilities);
_json.SerializeToFile(capabilities, path);
}
} }
public void UpdateDeviceOptions(string deviceId, DeviceOptions options) public void UpdateDeviceOptions(string deviceId, DeviceOptions options)
@ -72,32 +49,12 @@ namespace Emby.Server.Implementations.Devices
public ClientCapabilities GetCapabilities(string id) public ClientCapabilities GetCapabilities(string id)
{ {
if (_memoryCache.TryGetValue(id, out ClientCapabilities result)) return _capabilitiesMap.TryGetValue(id, out ClientCapabilities result)
{ ? result
return result; : new ClientCapabilities();
}
lock (_capabilitiesSyncLock)
{
var path = Path.Combine(GetDevicePath(id), "capabilities.json");
try
{
return _json.DeserializeFromFile<ClientCapabilities>(path) ?? new ClientCapabilities();
}
catch
{
}
}
return new ClientCapabilities();
} }
public DeviceInfo GetDevice(string id) public DeviceInfo GetDevice(string id)
{
return GetDevice(id, true);
}
private DeviceInfo GetDevice(string id, bool includeCapabilities)
{ {
var session = _authRepo.Get(new AuthenticationInfoQuery var session = _authRepo.Get(new AuthenticationInfoQuery
{ {
@ -154,16 +111,6 @@ namespace Emby.Server.Implementations.Devices
}; };
} }
private string GetDevicesPath()
{
return Path.Combine(_config.ApplicationPaths.DataPath, "devices");
}
private string GetDevicePath(string id)
{
return Path.Combine(GetDevicesPath(), id.GetMD5().ToString("N", CultureInfo.InvariantCulture));
}
public bool CanAccessDevice(User user, string deviceId) public bool CanAccessDevice(User user, string deviceId)
{ {
if (user == null) if (user == null)

View File

@ -58,8 +58,7 @@ namespace Emby.Server.Implementations.Session
/// <summary> /// <summary>
/// The active connections. /// The active connections.
/// </summary> /// </summary>
private readonly ConcurrentDictionary<string, SessionInfo> _activeConnections = private readonly ConcurrentDictionary<string, SessionInfo> _activeConnections = new (StringComparer.OrdinalIgnoreCase);
new ConcurrentDictionary<string, SessionInfo>(StringComparer.OrdinalIgnoreCase);
private Timer _idleTimer; private Timer _idleTimer;
@ -196,7 +195,7 @@ namespace Emby.Server.Implementations.Session
{ {
if (!string.IsNullOrEmpty(info.DeviceId)) if (!string.IsNullOrEmpty(info.DeviceId))
{ {
var capabilities = GetSavedCapabilities(info.DeviceId); var capabilities = _deviceManager.GetCapabilities(info.DeviceId);
if (capabilities != null) if (capabilities != null)
{ {
@ -1677,27 +1676,10 @@ namespace Emby.Server.Implementations.Session
SessionInfo = session SessionInfo = session
}); });
try _deviceManager.SaveCapabilities(session.DeviceId, capabilities);
{
SaveCapabilities(session.DeviceId, capabilities);
}
catch (Exception ex)
{
_logger.LogError("Error saving device capabilities", ex);
}
} }
} }
private ClientCapabilities GetSavedCapabilities(string deviceId)
{
return _deviceManager.GetCapabilities(deviceId);
}
private void SaveCapabilities(string deviceId, ClientCapabilities capabilities)
{
_deviceManager.SaveCapabilities(deviceId, capabilities);
}
/// <summary> /// <summary>
/// Converts a BaseItem to a BaseItemInfo. /// Converts a BaseItem to a BaseItemInfo.
/// </summary> /// </summary>