DeviceManager.cs 8.7 KB

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