DeviceManager.cs 7.8 KB

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