DeviceManager.cs 9.0 KB

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