2
0

DeviceManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Events;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller.Devices;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Model.Devices;
  7. using MediaBrowser.Model.Events;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Session;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Server.Implementations.Devices
  16. {
  17. public class DeviceManager : IDeviceManager
  18. {
  19. private readonly IDeviceRepository _repo;
  20. private readonly IUserManager _userManager;
  21. private readonly IFileSystem _fileSystem;
  22. private readonly ILibraryMonitor _libraryMonitor;
  23. private readonly IConfigurationManager _config;
  24. private readonly ILogger _logger;
  25. /// <summary>
  26. /// Occurs when [device options updated].
  27. /// </summary>
  28. public event EventHandler<GenericEventArgs<DeviceInfo>> DeviceOptionsUpdated;
  29. public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IConfigurationManager config, ILogger logger)
  30. {
  31. _repo = repo;
  32. _userManager = userManager;
  33. _fileSystem = fileSystem;
  34. _libraryMonitor = libraryMonitor;
  35. _config = config;
  36. _logger = logger;
  37. }
  38. public async Task<DeviceInfo> RegisterDevice(string reportedId, string name, string appName, string usedByUserId)
  39. {
  40. var device = GetDevice(reportedId) ?? new DeviceInfo
  41. {
  42. Id = reportedId
  43. };
  44. device.ReportedName = name;
  45. device.AppName = appName;
  46. if (!string.IsNullOrWhiteSpace(usedByUserId))
  47. {
  48. var user = _userManager.GetUserById(usedByUserId);
  49. device.LastUserId = user.Id.ToString("N");
  50. device.LastUserName = user.Name;
  51. }
  52. device.DateLastModified = DateTime.UtcNow;
  53. await _repo.SaveDevice(device).ConfigureAwait(false);
  54. return device;
  55. }
  56. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  57. {
  58. return _repo.SaveCapabilities(reportedId, capabilities);
  59. }
  60. public ClientCapabilities GetCapabilities(string reportedId)
  61. {
  62. return _repo.GetCapabilities(reportedId);
  63. }
  64. public DeviceInfo GetDevice(string id)
  65. {
  66. return _repo.GetDevice(id);
  67. }
  68. public IEnumerable<DeviceInfo> GetDevices()
  69. {
  70. return _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
  71. }
  72. public Task DeleteDevice(string id)
  73. {
  74. return _repo.DeleteDevice(id);
  75. }
  76. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  77. {
  78. return _repo.GetCameraUploadHistory(deviceId);
  79. }
  80. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  81. {
  82. var path = GetUploadPath(deviceId);
  83. if (!string.IsNullOrWhiteSpace(file.Album))
  84. {
  85. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  86. }
  87. Directory.CreateDirectory(path);
  88. path = Path.Combine(path, file.Name);
  89. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  90. try
  91. {
  92. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  93. {
  94. await stream.CopyToAsync(fs).ConfigureAwait(false);
  95. }
  96. _repo.AddCameraUpload(deviceId, file);
  97. }
  98. finally
  99. {
  100. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  101. }
  102. }
  103. private string GetUploadPath(string deviceId)
  104. {
  105. var device = GetDevice(deviceId);
  106. if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
  107. {
  108. return device.CameraUploadPath;
  109. }
  110. var config = _config.GetUploadOptions();
  111. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  112. {
  113. return config.CameraUploadPath;
  114. }
  115. var path = Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  116. if (config.EnableCameraUploadSubfolders)
  117. {
  118. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  119. }
  120. return path;
  121. }
  122. public async Task UpdateDeviceInfo(string id, DeviceOptions options)
  123. {
  124. var device = GetDevice(id);
  125. device.CustomName = options.CustomName;
  126. device.CameraUploadPath = options.CameraUploadPath;
  127. await _repo.SaveDevice(device).ConfigureAwait(false);
  128. EventHelper.FireEventIfNotNull(DeviceOptionsUpdated, this, new GenericEventArgs<DeviceInfo>(device), _logger);
  129. }
  130. }
  131. public class DevicesConfigStore : IConfigurationFactory
  132. {
  133. public IEnumerable<ConfigurationStore> GetConfigurations()
  134. {
  135. return new List<ConfigurationStore>
  136. {
  137. new ConfigurationStore
  138. {
  139. Key = "devices",
  140. ConfigurationType = typeof(DevicesOptions)
  141. }
  142. };
  143. }
  144. }
  145. public static class UploadConfigExtension
  146. {
  147. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  148. {
  149. return config.GetConfiguration<DevicesOptions>("devices");
  150. }
  151. }
  152. }