DeviceManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.Extensions;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Querying;
  11. using MediaBrowser.Model.Session;
  12. using MediaBrowser.Model.Users;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Server.Implementations.Devices
  19. {
  20. public class DeviceManager : IDeviceManager
  21. {
  22. private readonly IDeviceRepository _repo;
  23. private readonly IUserManager _userManager;
  24. private readonly IFileSystem _fileSystem;
  25. private readonly ILibraryMonitor _libraryMonitor;
  26. private readonly IConfigurationManager _config;
  27. private readonly ILogger _logger;
  28. /// <summary>
  29. /// Occurs when [device options updated].
  30. /// </summary>
  31. public event EventHandler<GenericEventArgs<DeviceInfo>> DeviceOptionsUpdated;
  32. public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IConfigurationManager config, ILogger logger)
  33. {
  34. _repo = repo;
  35. _userManager = userManager;
  36. _fileSystem = fileSystem;
  37. _libraryMonitor = libraryMonitor;
  38. _config = config;
  39. _logger = logger;
  40. }
  41. public async Task<DeviceInfo> RegisterDevice(string reportedId, string name, string appName, string usedByUserId)
  42. {
  43. var device = GetDevice(reportedId) ?? new DeviceInfo
  44. {
  45. Id = reportedId
  46. };
  47. device.ReportedName = name;
  48. device.AppName = appName;
  49. if (!string.IsNullOrWhiteSpace(usedByUserId))
  50. {
  51. var user = _userManager.GetUserById(usedByUserId);
  52. device.LastUserId = user.Id.ToString("N");
  53. device.LastUserName = user.Name;
  54. }
  55. device.DateLastModified = DateTime.UtcNow;
  56. await _repo.SaveDevice(device).ConfigureAwait(false);
  57. return device;
  58. }
  59. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  60. {
  61. return _repo.SaveCapabilities(reportedId, capabilities);
  62. }
  63. public ClientCapabilities GetCapabilities(string reportedId)
  64. {
  65. return _repo.GetCapabilities(reportedId);
  66. }
  67. public DeviceInfo GetDevice(string id)
  68. {
  69. return _repo.GetDevice(id);
  70. }
  71. public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
  72. {
  73. IEnumerable<DeviceInfo> devices = _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
  74. if (query.SupportsContentUploading.HasValue)
  75. {
  76. var val = query.SupportsContentUploading.Value;
  77. devices = devices.Where(i => GetCapabilities(i.Id).SupportsContentUploading == val);
  78. }
  79. if (query.SupportsSync.HasValue)
  80. {
  81. var val = query.SupportsSync.Value;
  82. devices = devices.Where(i => GetCapabilities(i.Id).SupportsSync == val);
  83. }
  84. if (query.SupportsPersistentIdentifier.HasValue)
  85. {
  86. var val = query.SupportsPersistentIdentifier.Value;
  87. devices = devices.Where(i =>
  88. {
  89. var caps = GetCapabilities(i.Id);
  90. var deviceVal = caps.SupportsUniqueIdentifier ?? caps.SupportsPersistentIdentifier;
  91. return deviceVal == val;
  92. });
  93. }
  94. if (!string.IsNullOrWhiteSpace(query.UserId))
  95. {
  96. devices = devices.Where(i => CanAccessDevice(query.UserId, i.Id));
  97. }
  98. var array = devices.ToArray();
  99. return new QueryResult<DeviceInfo>
  100. {
  101. Items = array,
  102. TotalRecordCount = array.Length
  103. };
  104. }
  105. public Task DeleteDevice(string id)
  106. {
  107. return _repo.DeleteDevice(id);
  108. }
  109. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  110. {
  111. return _repo.GetCameraUploadHistory(deviceId);
  112. }
  113. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  114. {
  115. var path = GetUploadPath(deviceId);
  116. if (!string.IsNullOrWhiteSpace(file.Album))
  117. {
  118. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  119. }
  120. Directory.CreateDirectory(path);
  121. path = Path.Combine(path, file.Name);
  122. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  123. try
  124. {
  125. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  126. {
  127. await stream.CopyToAsync(fs).ConfigureAwait(false);
  128. }
  129. _repo.AddCameraUpload(deviceId, file);
  130. }
  131. finally
  132. {
  133. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  134. }
  135. }
  136. private string GetUploadPath(string deviceId)
  137. {
  138. var device = GetDevice(deviceId);
  139. if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
  140. {
  141. return device.CameraUploadPath;
  142. }
  143. var config = _config.GetUploadOptions();
  144. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  145. {
  146. return config.CameraUploadPath;
  147. }
  148. var path = Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  149. if (config.EnableCameraUploadSubfolders)
  150. {
  151. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  152. }
  153. return path;
  154. }
  155. public async Task UpdateDeviceInfo(string id, DeviceOptions options)
  156. {
  157. var device = GetDevice(id);
  158. device.CustomName = options.CustomName;
  159. device.CameraUploadPath = options.CameraUploadPath;
  160. await _repo.SaveDevice(device).ConfigureAwait(false);
  161. EventHelper.FireEventIfNotNull(DeviceOptionsUpdated, this, new GenericEventArgs<DeviceInfo>(device), _logger);
  162. }
  163. public bool CanAccessDevice(string userId, string deviceId)
  164. {
  165. if (string.IsNullOrWhiteSpace(userId))
  166. {
  167. throw new ArgumentNullException("userId");
  168. }
  169. if (string.IsNullOrWhiteSpace(deviceId))
  170. {
  171. throw new ArgumentNullException("deviceId");
  172. }
  173. var user = _userManager.GetUserById(userId);
  174. if (!CanAccessDevice(user.Policy, deviceId))
  175. {
  176. var capabilities = GetCapabilities(deviceId);
  177. if (capabilities != null && capabilities.SupportsPersistentIdentifier)
  178. {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. private bool CanAccessDevice(UserPolicy policy, string id)
  185. {
  186. if (policy.EnableAllDevices)
  187. {
  188. return true;
  189. }
  190. return ListHelper.ContainsIgnoreCase(policy.EnabledDevices, id);
  191. }
  192. }
  193. public class DevicesConfigStore : IConfigurationFactory
  194. {
  195. public IEnumerable<ConfigurationStore> GetConfigurations()
  196. {
  197. return new List<ConfigurationStore>
  198. {
  199. new ConfigurationStore
  200. {
  201. Key = "devices",
  202. ConfigurationType = typeof(DevicesOptions)
  203. }
  204. };
  205. }
  206. }
  207. public static class UploadConfigExtension
  208. {
  209. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  210. {
  211. return config.GetConfiguration<DevicesOptions>("devices");
  212. }
  213. }
  214. }