aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Devices
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/Devices')
-rw-r--r--Emby.Server.Implementations/Devices/CameraUploadsDynamicFolder.cs41
-rw-r--r--Emby.Server.Implementations/Devices/DeviceManager.cs299
-rw-r--r--Emby.Server.Implementations/Devices/DeviceRepository.cs208
3 files changed, 548 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/Devices/CameraUploadsDynamicFolder.cs b/Emby.Server.Implementations/Devices/CameraUploadsDynamicFolder.cs
new file mode 100644
index 000000000..e2d5d0272
--- /dev/null
+++ b/Emby.Server.Implementations/Devices/CameraUploadsDynamicFolder.cs
@@ -0,0 +1,41 @@
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Controller.Entities;
+using System;
+using System.IO;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using MediaBrowser.Common.IO;
+using MediaBrowser.Controller.IO;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Controller.Providers;
+using MediaBrowser.Model.Serialization;
+using MediaBrowser.Server.Implementations.Devices;
+
+namespace Emby.Server.Implementations.Devices
+{
+ public class CameraUploadsDynamicFolder : IVirtualFolderCreator
+ {
+ private readonly IApplicationPaths _appPaths;
+ private readonly IFileSystem _fileSystem;
+
+ public CameraUploadsDynamicFolder(IApplicationPaths appPaths, IFileSystem fileSystem)
+ {
+ _appPaths = appPaths;
+ _fileSystem = fileSystem;
+ }
+
+ public BasePluginFolder GetFolder()
+ {
+ var path = Path.Combine(_appPaths.DataPath, "camerauploads");
+
+ _fileSystem.CreateDirectory(path);
+
+ return new CameraUploadsFolder
+ {
+ Path = path
+ };
+ }
+ }
+
+}
diff --git a/Emby.Server.Implementations/Devices/DeviceManager.cs b/Emby.Server.Implementations/Devices/DeviceManager.cs
new file mode 100644
index 000000000..88c0ea203
--- /dev/null
+++ b/Emby.Server.Implementations/Devices/DeviceManager.cs
@@ -0,0 +1,299 @@
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Events;
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Devices;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Devices;
+using MediaBrowser.Model.Events;
+using MediaBrowser.Model.Extensions;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Querying;
+using MediaBrowser.Model.Session;
+using MediaBrowser.Model.Users;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using MediaBrowser.Common.IO;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.IO;
+
+namespace Emby.Server.Implementations.Devices
+{
+ public class DeviceManager : IDeviceManager
+ {
+ private readonly IDeviceRepository _repo;
+ private readonly IUserManager _userManager;
+ private readonly IFileSystem _fileSystem;
+ private readonly ILibraryMonitor _libraryMonitor;
+ private readonly IServerConfigurationManager _config;
+ private readonly ILogger _logger;
+ private readonly INetworkManager _network;
+
+ public event EventHandler<GenericEventArgs<CameraImageUploadInfo>> CameraImageUploaded;
+
+ /// <summary>
+ /// Occurs when [device options updated].
+ /// </summary>
+ public event EventHandler<GenericEventArgs<DeviceInfo>> DeviceOptionsUpdated;
+
+ public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IServerConfigurationManager config, ILogger logger, INetworkManager network)
+ {
+ _repo = repo;
+ _userManager = userManager;
+ _fileSystem = fileSystem;
+ _libraryMonitor = libraryMonitor;
+ _config = config;
+ _logger = logger;
+ _network = network;
+ }
+
+ public async Task<DeviceInfo> RegisterDevice(string reportedId, string name, string appName, string appVersion, string usedByUserId)
+ {
+ if (string.IsNullOrWhiteSpace(reportedId))
+ {
+ throw new ArgumentNullException("reportedId");
+ }
+
+ var device = GetDevice(reportedId) ?? new DeviceInfo
+ {
+ Id = reportedId
+ };
+
+ device.ReportedName = name;
+ device.AppName = appName;
+ device.AppVersion = appVersion;
+
+ if (!string.IsNullOrWhiteSpace(usedByUserId))
+ {
+ var user = _userManager.GetUserById(usedByUserId);
+
+ device.LastUserId = user.Id.ToString("N");
+ device.LastUserName = user.Name;
+ }
+
+ device.DateLastModified = DateTime.UtcNow;
+
+ await _repo.SaveDevice(device).ConfigureAwait(false);
+
+ return device;
+ }
+
+ 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);
+ }
+
+ public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
+ {
+ IEnumerable<DeviceInfo> devices = _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
+
+ if (query.SupportsSync.HasValue)
+ {
+ var val = query.SupportsSync.Value;
+
+ devices = devices.Where(i => GetCapabilities(i.Id).SupportsSync == val);
+ }
+
+ if (query.SupportsPersistentIdentifier.HasValue)
+ {
+ var val = query.SupportsPersistentIdentifier.Value;
+
+ devices = devices.Where(i =>
+ {
+ var caps = GetCapabilities(i.Id);
+ var deviceVal = caps.SupportsPersistentIdentifier;
+ return deviceVal == val;
+ });
+ }
+
+ if (!string.IsNullOrWhiteSpace(query.UserId))
+ {
+ devices = devices.Where(i => CanAccessDevice(query.UserId, i.Id));
+ }
+
+ var array = devices.ToArray();
+ return new QueryResult<DeviceInfo>
+ {
+ Items = array,
+ TotalRecordCount = array.Length
+ };
+ }
+
+ 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);
+
+ if (!string.IsNullOrWhiteSpace(file.Album))
+ {
+ path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
+ }
+
+ path = Path.Combine(path, file.Name);
+ path = Path.ChangeExtension(path, MimeTypes.ToExtension(file.MimeType) ?? "jpg");
+
+ _libraryMonitor.ReportFileSystemChangeBeginning(path);
+
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+
+ try
+ {
+ using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
+ {
+ 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);
+ }
+ }
+
+ private string GetUploadPath(DeviceInfo device)
+ {
+ if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
+ {
+ return device.CameraUploadPath;
+ }
+
+ var config = _config.GetUploadOptions();
+ if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
+ {
+ return config.CameraUploadPath;
+ }
+
+ var path = DefaultCameraUploadsPath;
+
+ if (config.EnableCameraUploadSubfolders)
+ {
+ path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
+ }
+
+ return path;
+ }
+
+ private string DefaultCameraUploadsPath
+ {
+ get { return Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads"); }
+ }
+
+ 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);
+ }
+
+ 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);
+
+ if (user == null)
+ {
+ throw new ArgumentException("user not found");
+ }
+
+ if (!CanAccessDevice(user.Policy, deviceId))
+ {
+ var capabilities = GetCapabilities(deviceId);
+
+ if (capabilities != null && capabilities.SupportsPersistentIdentifier)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private bool CanAccessDevice(UserPolicy policy, string id)
+ {
+ if (policy.EnableAllDevices)
+ {
+ return true;
+ }
+
+ if (policy.IsAdministrator)
+ {
+ return true;
+ }
+
+ return ListHelper.ContainsIgnoreCase(policy.EnabledDevices, id);
+ }
+ }
+
+ 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");
+ }
+ }
+} \ No newline at end of file
diff --git a/Emby.Server.Implementations/Devices/DeviceRepository.cs b/Emby.Server.Implementations/Devices/DeviceRepository.cs
new file mode 100644
index 000000000..f739765b3
--- /dev/null
+++ b/Emby.Server.Implementations/Devices/DeviceRepository.cs
@@ -0,0 +1,208 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using MediaBrowser.Common.Configuration;
+using MediaBrowser.Common.Extensions;
+using MediaBrowser.Controller.Devices;
+using MediaBrowser.Model.Devices;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Model.Serialization;
+using MediaBrowser.Model.Session;
+
+namespace Emby.Server.Implementations.Devices
+{
+ public class DeviceRepository : IDeviceRepository
+ {
+ private readonly object _syncLock = new object();
+
+ private readonly IApplicationPaths _appPaths;
+ private readonly IJsonSerializer _json;
+ private readonly ILogger _logger;
+ private readonly IFileSystem _fileSystem;
+
+ private Dictionary<string, DeviceInfo> _devices;
+
+ public DeviceRepository(IApplicationPaths appPaths, IJsonSerializer json, ILogger logger, IFileSystem fileSystem)
+ {
+ _appPaths = appPaths;
+ _json = json;
+ _logger = logger;
+ _fileSystem = fileSystem;
+ }
+
+ 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");
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+
+ lock (_syncLock)
+ {
+ _json.SerializeToFile(device, path);
+ _devices[device.Id] = device;
+ }
+ return Task.FromResult(true);
+ }
+
+ public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
+ {
+ var device = GetDevice(reportedId);
+
+ if (device == null)
+ {
+ throw new ArgumentException("No device has been registed with id " + reportedId);
+ }
+
+ 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)
+ {
+ if (string.IsNullOrWhiteSpace(id))
+ {
+ throw new ArgumentNullException("id");
+ }
+
+ return GetDevices()
+ .FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
+ }
+
+ public IEnumerable<DeviceInfo> GetDevices()
+ {
+ lock (_syncLock)
+ {
+ if (_devices == null)
+ {
+ _devices = new Dictionary<string, DeviceInfo>(StringComparer.OrdinalIgnoreCase);
+
+ var devices = LoadDevices().ToList();
+ foreach (var device in devices)
+ {
+ _devices[device.Id] = device;
+ }
+ }
+ return _devices.Values.ToList();
+ }
+ }
+
+ private IEnumerable<DeviceInfo> LoadDevices()
+ {
+ var path = GetDevicesPath();
+
+ try
+ {
+ return _fileSystem
+ .GetFilePaths(path, true)
+ .Where(i => string.Equals(Path.GetFileName(i), "device.json", StringComparison.OrdinalIgnoreCase))
+ .ToList()
+ .Select(i =>
+ {
+ try
+ {
+ return _json.DeserializeFromFile<DeviceInfo>(i);
+ }
+ catch (Exception ex)
+ {
+ _logger.ErrorException("Error reading {0}", ex, i);
+ return null;
+ }
+ })
+ .Where(i => i != null);
+ }
+ catch (IOException)
+ {
+ return new List<DeviceInfo>();
+ }
+ }
+
+ public Task DeleteDevice(string id)
+ {
+ var path = GetDevicePath(id);
+
+ lock (_syncLock)
+ {
+ try
+ {
+ _fileSystem.DeleteDirectory(path, true);
+ }
+ catch (IOException)
+ {
+ }
+
+ _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");
+ _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+
+ lock (_syncLock)
+ {
+ ContentUploadHistory history;
+
+ try
+ {
+ history = _json.DeserializeFromFile<ContentUploadHistory>(path);
+ }
+ catch (IOException)
+ {
+ history = new ContentUploadHistory
+ {
+ DeviceId = deviceId
+ };
+ }
+
+ history.DeviceId = deviceId;
+ history.FilesUploaded.Add(file);
+
+ _json.SerializeToFile(history, path);
+ }
+ }
+ }
+}