DeviceManager.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading.Tasks;
  17. using MediaBrowser.Model.Users;
  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.SupportsUniqueIdentifier.HasValue)
  85. {
  86. var val = query.SupportsUniqueIdentifier.Value;
  87. devices = devices.Where(i => GetCapabilities(i.Id).SupportsUniqueIdentifier == val);
  88. }
  89. if (!string.IsNullOrWhiteSpace(query.UserId))
  90. {
  91. devices = devices.Where(i => CanAccessDevice(query.UserId, i.Id));
  92. }
  93. var array = devices.ToArray();
  94. return new QueryResult<DeviceInfo>
  95. {
  96. Items = array,
  97. TotalRecordCount = array.Length
  98. };
  99. }
  100. public Task DeleteDevice(string id)
  101. {
  102. return _repo.DeleteDevice(id);
  103. }
  104. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  105. {
  106. return _repo.GetCameraUploadHistory(deviceId);
  107. }
  108. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  109. {
  110. var path = GetUploadPath(deviceId);
  111. if (!string.IsNullOrWhiteSpace(file.Album))
  112. {
  113. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  114. }
  115. Directory.CreateDirectory(path);
  116. path = Path.Combine(path, file.Name);
  117. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  118. try
  119. {
  120. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  121. {
  122. await stream.CopyToAsync(fs).ConfigureAwait(false);
  123. }
  124. _repo.AddCameraUpload(deviceId, file);
  125. }
  126. finally
  127. {
  128. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  129. }
  130. }
  131. private string GetUploadPath(string deviceId)
  132. {
  133. var device = GetDevice(deviceId);
  134. if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
  135. {
  136. return device.CameraUploadPath;
  137. }
  138. var config = _config.GetUploadOptions();
  139. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  140. {
  141. return config.CameraUploadPath;
  142. }
  143. var path = Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  144. if (config.EnableCameraUploadSubfolders)
  145. {
  146. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  147. }
  148. return path;
  149. }
  150. public async Task UpdateDeviceInfo(string id, DeviceOptions options)
  151. {
  152. var device = GetDevice(id);
  153. device.CustomName = options.CustomName;
  154. device.CameraUploadPath = options.CameraUploadPath;
  155. await _repo.SaveDevice(device).ConfigureAwait(false);
  156. EventHelper.FireEventIfNotNull(DeviceOptionsUpdated, this, new GenericEventArgs<DeviceInfo>(device), _logger);
  157. }
  158. public bool CanAccessDevice(string userId, string deviceId)
  159. {
  160. if (string.IsNullOrWhiteSpace(userId))
  161. {
  162. throw new ArgumentNullException("userId");
  163. }
  164. if (string.IsNullOrWhiteSpace(deviceId))
  165. {
  166. throw new ArgumentNullException("deviceId");
  167. }
  168. var user = _userManager.GetUserById(userId);
  169. if (!CanAccessDevice(user.Policy, deviceId))
  170. {
  171. var capabilities = GetCapabilities(deviceId);
  172. if (capabilities.SupportsUniqueIdentifier)
  173. {
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. private bool CanAccessDevice(UserPolicy policy, string id)
  180. {
  181. if (policy.EnableAllDevices)
  182. {
  183. return true;
  184. }
  185. return ListHelper.ContainsIgnoreCase(policy.EnabledDevices, id);
  186. }
  187. }
  188. public class DevicesConfigStore : IConfigurationFactory
  189. {
  190. public IEnumerable<ConfigurationStore> GetConfigurations()
  191. {
  192. return new List<ConfigurationStore>
  193. {
  194. new ConfigurationStore
  195. {
  196. Key = "devices",
  197. ConfigurationType = typeof(DevicesOptions)
  198. }
  199. };
  200. }
  201. }
  202. public static class UploadConfigExtension
  203. {
  204. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  205. {
  206. return config.GetConfiguration<DevicesOptions>("devices");
  207. }
  208. }
  209. }