DeviceManager.cs 9.3 KB

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