jellyfin/Emby.Server.Implementations/Devices/DeviceRepository.cs

213 lines
6.2 KiB
C#
Raw Normal View History

2016-11-10 21:25:21 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
2014-10-11 13:38:13 -07:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Devices;
using MediaBrowser.Model.Devices;
2016-11-10 21:25:21 -07:00
using MediaBrowser.Model.IO;
2014-11-04 05:41:12 -07:00
using MediaBrowser.Model.Logging;
2014-10-11 13:38:13 -07:00
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Session;
2017-08-19 12:43:35 -07:00
using MediaBrowser.Model.Extensions;
2014-10-11 13:38:13 -07:00
2016-11-18 01:39:20 -07:00
namespace Emby.Server.Implementations.Devices
2014-10-11 13:38:13 -07:00
{
public class DeviceRepository : IDeviceRepository
{
private readonly object _syncLock = new object();
private readonly IApplicationPaths _appPaths;
private readonly IJsonSerializer _json;
2014-11-05 12:28:41 -07:00
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
2014-10-11 13:38:13 -07:00
2016-06-29 11:48:26 -07:00
private Dictionary<string, DeviceInfo> _devices;
2014-10-11 13:38:13 -07:00
public DeviceRepository(IApplicationPaths appPaths, IJsonSerializer json, ILogger logger, IFileSystem fileSystem)
2014-10-11 13:38:13 -07:00
{
_appPaths = appPaths;
_json = json;
2014-11-05 12:28:41 -07:00
_logger = logger;
_fileSystem = fileSystem;
2014-10-11 13:38:13 -07:00
}
private string GetDevicesPath()
{
return Path.Combine(_appPaths.DataPath, "devices");
}
private string GetDevicePath(string id)
{
return Path.Combine(GetDevicesPath(), id.GetMD5().ToString("N"));
}
public Task SaveDevice(DeviceInfo device)
{
var path = Path.Combine(GetDevicePath(device.Id), "device.json");
2017-05-10 12:12:03 -07:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
2014-10-11 13:38:13 -07:00
lock (_syncLock)
{
_json.SerializeToFile(device, path);
2016-06-29 11:48:26 -07:00
_devices[device.Id] = device;
2014-10-11 13:38:13 -07:00
}
return Task.FromResult(true);
}
public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
{
var device = GetDevice(reportedId);
2015-01-25 12:13:04 -07:00
if (device == null)
{
throw new ArgumentException("No device has been registed with id " + reportedId);
}
2014-10-11 13:38:13 -07:00
device.Capabilities = capabilities;
SaveDevice(device);
return Task.FromResult(true);
}
public ClientCapabilities GetCapabilities(string reportedId)
{
var device = GetDevice(reportedId);
return device == null ? null : device.Capabilities;
}
public DeviceInfo GetDevice(string id)
{
2015-01-25 12:13:04 -07:00
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentNullException("id");
}
2014-11-04 05:41:12 -07:00
return GetDevices()
.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
2014-10-11 13:38:13 -07:00
}
public IEnumerable<DeviceInfo> GetDevices()
{
2016-01-26 11:18:21 -07:00
lock (_syncLock)
2014-10-11 13:38:13 -07:00
{
2016-01-26 11:18:21 -07:00
if (_devices == null)
2014-10-11 13:38:13 -07:00
{
2016-06-29 11:48:26 -07:00
_devices = new Dictionary<string, DeviceInfo>(StringComparer.OrdinalIgnoreCase);
var devices = LoadDevices().ToList();
foreach (var device in devices)
{
_devices[device.Id] = device;
}
2014-10-11 13:38:13 -07:00
}
2016-06-29 11:48:26 -07:00
return _devices.Values.ToList();
2014-10-11 13:38:13 -07:00
}
}
private IEnumerable<DeviceInfo> LoadDevices()
{
var path = GetDevicesPath();
try
{
2015-09-24 10:50:49 -07:00
return _fileSystem
.GetFilePaths(path, true)
2014-11-06 17:16:16 -07:00
.Where(i => string.Equals(Path.GetFileName(i), "device.json", StringComparison.OrdinalIgnoreCase))
.ToList()
2014-11-04 05:41:12 -07:00
.Select(i =>
{
try
{
2014-11-06 17:16:16 -07:00
return _json.DeserializeFromFile<DeviceInfo>(i);
2014-11-04 05:41:12 -07:00
}
catch (Exception ex)
{
2014-11-06 17:16:16 -07:00
_logger.ErrorException("Error reading {0}", ex, i);
2014-11-04 05:41:12 -07:00
return null;
}
})
2014-11-06 17:16:16 -07:00
.Where(i => i != null);
2014-10-11 13:38:13 -07:00
}
catch (IOException)
{
return new List<DeviceInfo>();
}
}
public Task DeleteDevice(string id)
{
var path = GetDevicePath(id);
lock (_syncLock)
{
try
{
_fileSystem.DeleteDirectory(path, true);
2014-10-11 13:38:13 -07:00
}
2016-11-18 01:39:20 -07:00
catch (IOException)
2014-10-11 13:38:13 -07:00
{
}
2016-06-29 11:48:26 -07:00
2014-10-11 13:38:13 -07:00
_devices = null;
}
return Task.FromResult(true);
}
public ContentUploadHistory GetCameraUploadHistory(string deviceId)
{
var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
lock (_syncLock)
{
try
{
return _json.DeserializeFromFile<ContentUploadHistory>(path);
}
catch (IOException)
{
return new ContentUploadHistory
{
DeviceId = deviceId
};
}
}
}
public void AddCameraUpload(string deviceId, LocalFileInfo file)
{
var path = Path.Combine(GetDevicePath(deviceId), "camerauploads.json");
2017-05-10 12:12:03 -07:00
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
2014-10-11 13:38:13 -07:00
lock (_syncLock)
{
ContentUploadHistory history;
try
{
history = _json.DeserializeFromFile<ContentUploadHistory>(path);
}
catch (IOException)
{
history = new ContentUploadHistory
{
DeviceId = deviceId
};
}
history.DeviceId = deviceId;
2017-08-19 12:43:35 -07:00
var list = history.FilesUploaded.ToList();
list.Add(file);
history.FilesUploaded = list.ToArray(list.Count);
2014-10-11 13:38:13 -07:00
_json.SerializeToFile(history, path);
}
}
}
}