DeviceManager.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.Model.IO;
  20. using MediaBrowser.Controller.Configuration;
  21. using MediaBrowser.Controller.IO;
  22. namespace Emby.Server.Implementations.Devices
  23. {
  24. public class DeviceManager : IDeviceManager
  25. {
  26. private readonly IDeviceRepository _repo;
  27. private readonly IUserManager _userManager;
  28. private readonly IFileSystem _fileSystem;
  29. private readonly ILibraryMonitor _libraryMonitor;
  30. private readonly IServerConfigurationManager _config;
  31. private readonly ILogger _logger;
  32. private readonly INetworkManager _network;
  33. public event EventHandler<GenericEventArgs<CameraImageUploadInfo>> CameraImageUploaded;
  34. /// <summary>
  35. /// Occurs when [device options updated].
  36. /// </summary>
  37. public event EventHandler<GenericEventArgs<DeviceInfo>> DeviceOptionsUpdated;
  38. public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IServerConfigurationManager config, ILogger logger, INetworkManager network)
  39. {
  40. _repo = repo;
  41. _userManager = userManager;
  42. _fileSystem = fileSystem;
  43. _libraryMonitor = libraryMonitor;
  44. _config = config;
  45. _logger = logger;
  46. _network = network;
  47. }
  48. public DeviceInfo RegisterDevice(string reportedId, string name, string appName, string appVersion, string usedByUserId)
  49. {
  50. if (string.IsNullOrWhiteSpace(reportedId))
  51. {
  52. throw new ArgumentNullException("reportedId");
  53. }
  54. var device = GetDevice(reportedId) ?? new DeviceInfo
  55. {
  56. Id = reportedId
  57. };
  58. device.ReportedName = name;
  59. device.AppName = appName;
  60. device.AppVersion = appVersion;
  61. if (!string.IsNullOrWhiteSpace(usedByUserId))
  62. {
  63. var user = _userManager.GetUserById(usedByUserId);
  64. device.LastUserId = user.Id.ToString("N");
  65. device.LastUserName = user.Name;
  66. }
  67. device.DateLastModified = DateTime.UtcNow;
  68. device.Name = string.IsNullOrWhiteSpace(device.CustomName) ? device.ReportedName : device.CustomName;
  69. _repo.SaveDevice(device);
  70. return device;
  71. }
  72. public void SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  73. {
  74. _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();
  87. if (query.SupportsSync.HasValue)
  88. {
  89. var val = query.SupportsSync.Value;
  90. devices = devices.Where(i => i.Capabilities.SupportsSync == val);
  91. }
  92. if (query.SupportsPersistentIdentifier.HasValue)
  93. {
  94. var val = query.SupportsPersistentIdentifier.Value;
  95. devices = devices.Where(i =>
  96. {
  97. var deviceVal = i.Capabilities.SupportsPersistentIdentifier;
  98. return deviceVal == val;
  99. });
  100. }
  101. if (!string.IsNullOrWhiteSpace(query.UserId))
  102. {
  103. devices = devices.Where(i => CanAccessDevice(query.UserId, i.Id));
  104. }
  105. var array = devices.ToArray();
  106. return new QueryResult<DeviceInfo>
  107. {
  108. Items = array,
  109. TotalRecordCount = array.Length
  110. };
  111. }
  112. public void DeleteDevice(string id)
  113. {
  114. _repo.DeleteDevice(id);
  115. }
  116. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  117. {
  118. return _repo.GetCameraUploadHistory(deviceId);
  119. }
  120. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  121. {
  122. var device = GetDevice(deviceId);
  123. var path = GetUploadPath(device);
  124. if (!string.IsNullOrWhiteSpace(file.Album))
  125. {
  126. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  127. }
  128. path = Path.Combine(path, file.Name);
  129. path = Path.ChangeExtension(path, MimeTypes.ToExtension(file.MimeType) ?? "jpg");
  130. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  131. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
  132. try
  133. {
  134. using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
  135. {
  136. await stream.CopyToAsync(fs).ConfigureAwait(false);
  137. }
  138. _repo.AddCameraUpload(deviceId, file);
  139. }
  140. finally
  141. {
  142. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  143. }
  144. if (CameraImageUploaded != null)
  145. {
  146. EventHelper.FireEventIfNotNull(CameraImageUploaded, this, new GenericEventArgs<CameraImageUploadInfo>
  147. {
  148. Argument = new CameraImageUploadInfo
  149. {
  150. Device = device,
  151. FileInfo = file
  152. }
  153. }, _logger);
  154. }
  155. }
  156. private string GetUploadPath(DeviceInfo device)
  157. {
  158. if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
  159. {
  160. return device.CameraUploadPath;
  161. }
  162. var config = _config.GetUploadOptions();
  163. var path = config.CameraUploadPath;
  164. if (string.IsNullOrWhiteSpace(path))
  165. {
  166. path = DefaultCameraUploadsPath;
  167. }
  168. if (config.EnableCameraUploadSubfolders)
  169. {
  170. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  171. }
  172. return path;
  173. }
  174. private string DefaultCameraUploadsPath
  175. {
  176. get { return Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads"); }
  177. }
  178. public void UpdateDeviceInfo(string id, DeviceOptions options)
  179. {
  180. var device = GetDevice(id);
  181. device.CustomName = options.CustomName;
  182. device.CameraUploadPath = options.CameraUploadPath;
  183. device.Name = string.IsNullOrWhiteSpace(device.CustomName) ? device.ReportedName : device.CustomName;
  184. _repo.SaveDevice(device);
  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. }