DeviceManager.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.Logging;
  9. using MediaBrowser.Model.Querying;
  10. using MediaBrowser.Model.Session;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Server.Implementations.Devices
  17. {
  18. public class DeviceManager : IDeviceManager
  19. {
  20. private readonly IDeviceRepository _repo;
  21. private readonly IUserManager _userManager;
  22. private readonly IFileSystem _fileSystem;
  23. private readonly ILibraryMonitor _libraryMonitor;
  24. private readonly IConfigurationManager _config;
  25. private readonly ILogger _logger;
  26. /// <summary>
  27. /// Occurs when [device options updated].
  28. /// </summary>
  29. public event EventHandler<GenericEventArgs<DeviceInfo>> DeviceOptionsUpdated;
  30. public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IConfigurationManager config, ILogger logger)
  31. {
  32. _repo = repo;
  33. _userManager = userManager;
  34. _fileSystem = fileSystem;
  35. _libraryMonitor = libraryMonitor;
  36. _config = config;
  37. _logger = logger;
  38. }
  39. public async Task<DeviceInfo> RegisterDevice(string reportedId, string name, string appName, string usedByUserId)
  40. {
  41. var device = GetDevice(reportedId) ?? new DeviceInfo
  42. {
  43. Id = reportedId
  44. };
  45. device.ReportedName = name;
  46. device.AppName = appName;
  47. if (!string.IsNullOrWhiteSpace(usedByUserId))
  48. {
  49. var user = _userManager.GetUserById(usedByUserId);
  50. device.LastUserId = user.Id.ToString("N");
  51. device.LastUserName = user.Name;
  52. }
  53. device.DateLastModified = DateTime.UtcNow;
  54. await _repo.SaveDevice(device).ConfigureAwait(false);
  55. return device;
  56. }
  57. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  58. {
  59. return _repo.SaveCapabilities(reportedId, capabilities);
  60. }
  61. public ClientCapabilities GetCapabilities(string reportedId)
  62. {
  63. return _repo.GetCapabilities(reportedId);
  64. }
  65. public DeviceInfo GetDevice(string id)
  66. {
  67. return _repo.GetDevice(id);
  68. }
  69. public QueryResult<DeviceInfo> GetDevices(DeviceQuery query)
  70. {
  71. IEnumerable<DeviceInfo> devices = _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
  72. if (query.SupportsContentUploading.HasValue)
  73. {
  74. var val = query.SupportsContentUploading.Value;
  75. devices = devices.Where(i => GetCapabilities(i.Id).SupportsContentUploading == val);
  76. }
  77. if (query.SupportsSync.HasValue)
  78. {
  79. var val = query.SupportsSync.Value;
  80. devices = devices.Where(i => GetCapabilities(i.Id).SupportsSync == val);
  81. }
  82. if (query.SupportsUniqueIdentifier.HasValue)
  83. {
  84. var val = query.SupportsUniqueIdentifier.Value;
  85. devices = devices.Where(i => GetCapabilities(i.Id).SupportsUniqueIdentifier == val);
  86. }
  87. var array = devices.ToArray();
  88. return new QueryResult<DeviceInfo>
  89. {
  90. Items = array,
  91. TotalRecordCount = array.Length
  92. };
  93. }
  94. public Task DeleteDevice(string id)
  95. {
  96. return _repo.DeleteDevice(id);
  97. }
  98. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  99. {
  100. return _repo.GetCameraUploadHistory(deviceId);
  101. }
  102. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  103. {
  104. var path = GetUploadPath(deviceId);
  105. if (!string.IsNullOrWhiteSpace(file.Album))
  106. {
  107. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  108. }
  109. Directory.CreateDirectory(path);
  110. path = Path.Combine(path, file.Name);
  111. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  112. try
  113. {
  114. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  115. {
  116. await stream.CopyToAsync(fs).ConfigureAwait(false);
  117. }
  118. _repo.AddCameraUpload(deviceId, file);
  119. }
  120. finally
  121. {
  122. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  123. }
  124. }
  125. private string GetUploadPath(string deviceId)
  126. {
  127. var device = GetDevice(deviceId);
  128. if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
  129. {
  130. return device.CameraUploadPath;
  131. }
  132. var config = _config.GetUploadOptions();
  133. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  134. {
  135. return config.CameraUploadPath;
  136. }
  137. var path = Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  138. if (config.EnableCameraUploadSubfolders)
  139. {
  140. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  141. }
  142. return path;
  143. }
  144. public async Task UpdateDeviceInfo(string id, DeviceOptions options)
  145. {
  146. var device = GetDevice(id);
  147. device.CustomName = options.CustomName;
  148. device.CameraUploadPath = options.CameraUploadPath;
  149. await _repo.SaveDevice(device).ConfigureAwait(false);
  150. EventHelper.FireEventIfNotNull(DeviceOptionsUpdated, this, new GenericEventArgs<DeviceInfo>(device), _logger);
  151. }
  152. }
  153. public class DevicesConfigStore : IConfigurationFactory
  154. {
  155. public IEnumerable<ConfigurationStore> GetConfigurations()
  156. {
  157. return new List<ConfigurationStore>
  158. {
  159. new ConfigurationStore
  160. {
  161. Key = "devices",
  162. ConfigurationType = typeof(DevicesOptions)
  163. }
  164. };
  165. }
  166. }
  167. public static class UploadConfigExtension
  168. {
  169. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  170. {
  171. return config.GetConfiguration<DevicesOptions>("devices");
  172. }
  173. }
  174. }