DeviceManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.SupportsUniqueIdentifier.HasValue)
  78. {
  79. var val = query.SupportsUniqueIdentifier.Value;
  80. devices = devices.Where(i => GetCapabilities(i.Id).SupportsUniqueIdentifier == val);
  81. }
  82. var array = devices.ToArray();
  83. return new QueryResult<DeviceInfo>
  84. {
  85. Items = array,
  86. TotalRecordCount = array.Length
  87. };
  88. }
  89. public Task DeleteDevice(string id)
  90. {
  91. return _repo.DeleteDevice(id);
  92. }
  93. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  94. {
  95. return _repo.GetCameraUploadHistory(deviceId);
  96. }
  97. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  98. {
  99. var path = GetUploadPath(deviceId);
  100. if (!string.IsNullOrWhiteSpace(file.Album))
  101. {
  102. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  103. }
  104. Directory.CreateDirectory(path);
  105. path = Path.Combine(path, file.Name);
  106. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  107. try
  108. {
  109. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  110. {
  111. await stream.CopyToAsync(fs).ConfigureAwait(false);
  112. }
  113. _repo.AddCameraUpload(deviceId, file);
  114. }
  115. finally
  116. {
  117. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  118. }
  119. }
  120. private string GetUploadPath(string deviceId)
  121. {
  122. var device = GetDevice(deviceId);
  123. if (!string.IsNullOrWhiteSpace(device.CameraUploadPath))
  124. {
  125. return device.CameraUploadPath;
  126. }
  127. var config = _config.GetUploadOptions();
  128. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  129. {
  130. return config.CameraUploadPath;
  131. }
  132. var path = Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  133. if (config.EnableCameraUploadSubfolders)
  134. {
  135. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  136. }
  137. return path;
  138. }
  139. public async Task UpdateDeviceInfo(string id, DeviceOptions options)
  140. {
  141. var device = GetDevice(id);
  142. device.CustomName = options.CustomName;
  143. device.CameraUploadPath = options.CameraUploadPath;
  144. await _repo.SaveDevice(device).ConfigureAwait(false);
  145. EventHelper.FireEventIfNotNull(DeviceOptionsUpdated, this, new GenericEventArgs<DeviceInfo>(device), _logger);
  146. }
  147. }
  148. public class DevicesConfigStore : IConfigurationFactory
  149. {
  150. public IEnumerable<ConfigurationStore> GetConfigurations()
  151. {
  152. return new List<ConfigurationStore>
  153. {
  154. new ConfigurationStore
  155. {
  156. Key = "devices",
  157. ConfigurationType = typeof(DevicesOptions)
  158. }
  159. };
  160. }
  161. }
  162. public static class UploadConfigExtension
  163. {
  164. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  165. {
  166. return config.GetConfiguration<DevicesOptions>("devices");
  167. }
  168. }
  169. }