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

298 lines
9.3 KiB
C#
Raw Normal View History

2014-10-11 20:38:13 +00:00
using MediaBrowser.Common.Configuration;
2014-10-13 20:14:53 +00:00
using MediaBrowser.Common.Events;
2015-05-26 15:31:50 +00:00
using MediaBrowser.Common.Net;
2014-10-11 20:38:13 +00:00
using MediaBrowser.Controller.Devices;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Devices;
2014-10-13 20:14:53 +00:00
using MediaBrowser.Model.Events;
2014-12-29 20:18:48 +00:00
using MediaBrowser.Model.Extensions;
2014-10-13 20:14:53 +00:00
using MediaBrowser.Model.Logging;
2015-09-10 18:28:22 +00:00
using MediaBrowser.Model.Net;
2014-12-13 03:56:30 +00:00
using MediaBrowser.Model.Querying;
2014-10-11 20:38:13 +00:00
using MediaBrowser.Model.Session;
2015-01-02 05:36:27 +00:00
using MediaBrowser.Model.Users;
2014-10-11 20:38:13 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Model.IO;
2015-11-03 04:34:47 +00:00
using MediaBrowser.Controller.Configuration;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Controller.IO;
2014-10-11 20:38:13 +00:00
2016-11-03 07:14:14 +00:00
namespace Emby.Server.Implementations.Devices
2014-10-11 20:38:13 +00:00
{
public class DeviceManager : IDeviceManager
{
private readonly IDeviceRepository _repo;
private readonly IUserManager _userManager;
private readonly IFileSystem _fileSystem;
private readonly ILibraryMonitor _libraryMonitor;
2015-11-03 04:34:47 +00:00
private readonly IServerConfigurationManager _config;
2014-10-13 20:14:53 +00:00
private readonly ILogger _logger;
2015-05-26 15:31:50 +00:00
private readonly INetworkManager _network;
2014-10-13 20:14:53 +00:00
public event EventHandler<GenericEventArgs<CameraImageUploadInfo>> CameraImageUploaded;
2014-10-13 20:14:53 +00:00
/// <summary>
/// Occurs when [device options updated].
/// </summary>
public event EventHandler<GenericEventArgs<DeviceInfo>> DeviceOptionsUpdated;
2014-12-13 03:56:30 +00:00
2015-11-03 04:34:47 +00:00
public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IServerConfigurationManager config, ILogger logger, INetworkManager network)
2014-10-11 20:38:13 +00:00
{
_repo = repo;
_userManager = userManager;
_fileSystem = fileSystem;
_libraryMonitor = libraryMonitor;
_config = config;
2014-10-13 20:14:53 +00:00
_logger = logger;
2015-05-26 15:31:50 +00:00
_network = network;
2014-10-11 20:38:13 +00:00
}
2015-03-15 01:42:09 +00:00
public async Task<DeviceInfo> RegisterDevice(string reportedId, string name, string appName, string appVersion, string usedByUserId)
2014-10-11 20:38:13 +00:00
{
if (string.IsNullOrWhiteSpace(reportedId))
{
throw new ArgumentNullException("reportedId");
}
2014-10-11 20:38:13 +00:00
var device = GetDevice(reportedId) ?? new DeviceInfo
{
Id = reportedId
};
2014-10-13 20:14:53 +00:00
device.ReportedName = name;
2014-10-12 01:46:02 +00:00
device.AppName = appName;
2015-03-15 01:42:09 +00:00
device.AppVersion = appVersion;
2014-10-11 20:38:13 +00:00
if (!string.IsNullOrWhiteSpace(usedByUserId))
{
var user = _userManager.GetUserById(usedByUserId);
device.LastUserId = user.Id.ToString("N");
device.LastUserName = user.Name;
}
device.DateLastModified = DateTime.UtcNow;
2014-10-13 20:14:53 +00:00
await _repo.SaveDevice(device).ConfigureAwait(false);
return device;
2014-10-11 20:38:13 +00:00
}
public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
{
return _repo.SaveCapabilities(reportedId, capabilities);
}
public ClientCapabilities GetCapabilities(string reportedId)
{
return _repo.GetCapabilities(reportedId);
}
public DeviceInfo GetDevice(string id)
{
return _repo.GetDevice(id);
}
2014-12-13 03:56:30 +00:00
public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
2014-10-11 20:38:13 +00:00
{
2014-12-13 03:56:30 +00:00
IEnumerable<DeviceInfo> devices = _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
2014-12-15 05:49:04 +00:00
if (query.SupportsSync.HasValue)
{
var val = query.SupportsSync.Value;
devices = devices.Where(i => GetCapabilities(i.Id).SupportsSync == val);
}
2015-01-20 05:19:13 +00:00
if (query.SupportsPersistentIdentifier.HasValue)
2014-12-13 03:56:30 +00:00
{
2015-01-20 05:19:13 +00:00
var val = query.SupportsPersistentIdentifier.Value;
2014-12-13 03:56:30 +00:00
2015-01-20 05:19:13 +00:00
devices = devices.Where(i =>
{
var caps = GetCapabilities(i.Id);
2015-02-19 19:21:03 +00:00
var deviceVal = caps.SupportsPersistentIdentifier;
2015-01-20 05:19:13 +00:00
return deviceVal == val;
});
2014-12-31 06:24:49 +00:00
}
if (!string.IsNullOrWhiteSpace(query.UserId))
{
devices = devices.Where(i => CanAccessDevice(query.UserId, i.Id));
}
2014-12-13 03:56:30 +00:00
var array = devices.ToArray();
return new QueryResult<DeviceInfo>
{
Items = array,
TotalRecordCount = array.Length
};
2014-10-11 20:38:13 +00:00
}
public Task DeleteDevice(string id)
{
return _repo.DeleteDevice(id);
}
public ContentUploadHistory GetCameraUploadHistory(string deviceId)
{
return _repo.GetCameraUploadHistory(deviceId);
}
public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
{
var device = GetDevice(deviceId);
var path = GetUploadPath(device);
2014-10-11 20:38:13 +00:00
if (!string.IsNullOrWhiteSpace(file.Album))
{
path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
}
path = Path.Combine(path, file.Name);
2015-09-10 18:28:22 +00:00
path = Path.ChangeExtension(path, MimeTypes.ToExtension(file.MimeType) ?? "jpg");
2014-10-11 20:38:13 +00:00
_libraryMonitor.ReportFileSystemChangeBeginning(path);
2015-09-20 17:56:26 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
2015-09-10 18:28:22 +00:00
2014-10-11 20:38:13 +00:00
try
{
2016-10-25 19:02:04 +00:00
using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
2014-10-11 20:38:13 +00:00
{
await stream.CopyToAsync(fs).ConfigureAwait(false);
}
_repo.AddCameraUpload(deviceId, file);
}
finally
{
_libraryMonitor.ReportFileSystemChangeComplete(path, true);
}
if (CameraImageUploaded != null)
{
EventHelper.FireEventIfNotNull(CameraImageUploaded, this, new GenericEventArgs<CameraImageUploadInfo>
{
Argument = new CameraImageUploadInfo
{
Device = device,
FileInfo = file
}
}, _logger);
}
2014-10-11 20:38:13 +00:00
}
private string GetUploadPath(DeviceInfo device)
{
2014-10-13 20:14:53 +00:00
if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
{
return device.CameraUploadPath;
}
2014-10-12 01:46:02 +00:00
2014-10-13 20:14:53 +00:00
var config = _config.GetUploadOptions();
2017-03-07 18:27:56 +00:00
var path = config.CameraUploadPath;
if (string.IsNullOrWhiteSpace(path))
2014-10-11 20:38:13 +00:00
{
2017-03-07 18:27:56 +00:00
path = DefaultCameraUploadsPath;
2014-10-11 20:38:13 +00:00
}
2014-10-12 01:46:02 +00:00
if (config.EnableCameraUploadSubfolders)
{
path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
}
return path;
2014-10-11 20:38:13 +00:00
}
2014-10-13 20:14:53 +00:00
2015-11-03 04:34:47 +00:00
private string DefaultCameraUploadsPath
{
get { return Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads"); }
}
2014-10-13 20:14:53 +00:00
public async Task UpdateDeviceInfo(string id, DeviceOptions options)
{
var device = GetDevice(id);
device.CustomName = options.CustomName;
device.CameraUploadPath = options.CameraUploadPath;
await _repo.SaveDevice(device).ConfigureAwait(false);
EventHelper.FireEventIfNotNull(DeviceOptionsUpdated, this, new GenericEventArgs<DeviceInfo>(device), _logger);
}
2014-12-29 20:18:48 +00:00
public bool CanAccessDevice(string userId, string deviceId)
{
if (string.IsNullOrWhiteSpace(userId))
{
throw new ArgumentNullException("userId");
}
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException("deviceId");
}
var user = _userManager.GetUserById(userId);
2015-05-18 22:23:03 +00:00
if (user == null)
{
throw new ArgumentException("user not found");
}
2014-12-29 20:18:48 +00:00
if (!CanAccessDevice(user.Policy, deviceId))
{
var capabilities = GetCapabilities(deviceId);
2015-01-20 05:19:13 +00:00
if (capabilities != null && capabilities.SupportsPersistentIdentifier)
2014-12-29 20:18:48 +00:00
{
return false;
}
}
return true;
}
private bool CanAccessDevice(UserPolicy policy, string id)
{
if (policy.EnableAllDevices)
{
return true;
}
2015-10-26 05:29:32 +00:00
if (policy.IsAdministrator)
{
return true;
}
2014-12-29 20:18:48 +00:00
return ListHelper.ContainsIgnoreCase(policy.EnabledDevices, id);
}
2014-10-11 20:38:13 +00:00
}
public class DevicesConfigStore : IConfigurationFactory
{
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return new List<ConfigurationStore>
{
new ConfigurationStore
{
Key = "devices",
ConfigurationType = typeof(DevicesOptions)
}
};
}
}
public static class UploadConfigExtension
{
public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
{
return config.GetConfiguration<DevicesOptions>("devices");
}
}
2015-09-20 17:56:26 +00:00
}