DeviceManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Devices;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Devices;
  6. using MediaBrowser.Model.Session;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Server.Implementations.Devices
  13. {
  14. public class DeviceManager : IDeviceManager
  15. {
  16. private readonly IDeviceRepository _repo;
  17. private readonly IUserManager _userManager;
  18. private readonly IFileSystem _fileSystem;
  19. private readonly ILibraryMonitor _libraryMonitor;
  20. private readonly IConfigurationManager _config;
  21. public DeviceManager(IDeviceRepository repo, IUserManager userManager, IFileSystem fileSystem, ILibraryMonitor libraryMonitor, IConfigurationManager config)
  22. {
  23. _repo = repo;
  24. _userManager = userManager;
  25. _fileSystem = fileSystem;
  26. _libraryMonitor = libraryMonitor;
  27. _config = config;
  28. }
  29. public Task RegisterDevice(string reportedId, string name, string appName, string usedByUserId)
  30. {
  31. var device = GetDevice(reportedId) ?? new DeviceInfo
  32. {
  33. Id = reportedId
  34. };
  35. device.Name = name;
  36. device.AppName = appName;
  37. if (!string.IsNullOrWhiteSpace(usedByUserId))
  38. {
  39. var user = _userManager.GetUserById(usedByUserId);
  40. device.LastUserId = user.Id.ToString("N");
  41. device.LastUserName = user.Name;
  42. }
  43. device.DateLastModified = DateTime.UtcNow;
  44. return _repo.SaveDevice(device);
  45. }
  46. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  47. {
  48. return _repo.SaveCapabilities(reportedId, capabilities);
  49. }
  50. public ClientCapabilities GetCapabilities(string reportedId)
  51. {
  52. return _repo.GetCapabilities(reportedId);
  53. }
  54. public DeviceInfo GetDevice(string id)
  55. {
  56. return _repo.GetDevice(id);
  57. }
  58. public IEnumerable<DeviceInfo> GetDevices()
  59. {
  60. return _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
  61. }
  62. public Task DeleteDevice(string id)
  63. {
  64. return _repo.DeleteDevice(id);
  65. }
  66. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  67. {
  68. return _repo.GetCameraUploadHistory(deviceId);
  69. }
  70. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  71. {
  72. var path = GetUploadPath(deviceId);
  73. if (!string.IsNullOrWhiteSpace(file.Album))
  74. {
  75. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  76. }
  77. Directory.CreateDirectory(path);
  78. path = Path.Combine(path, file.Name);
  79. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  80. try
  81. {
  82. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  83. {
  84. await stream.CopyToAsync(fs).ConfigureAwait(false);
  85. }
  86. _repo.AddCameraUpload(deviceId, file);
  87. }
  88. finally
  89. {
  90. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  91. }
  92. }
  93. private string GetUploadPath(string deviceId)
  94. {
  95. var config = _config.GetUploadOptions();
  96. var device = GetDevice(deviceId);
  97. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  98. {
  99. return config.CameraUploadPath;
  100. }
  101. var path = Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  102. if (config.EnableCameraUploadSubfolders)
  103. {
  104. path = Path.Combine(path, _fileSystem.GetValidFilename(device.Name));
  105. }
  106. return path;
  107. }
  108. }
  109. public class DevicesConfigStore : IConfigurationFactory
  110. {
  111. public IEnumerable<ConfigurationStore> GetConfigurations()
  112. {
  113. return new List<ConfigurationStore>
  114. {
  115. new ConfigurationStore
  116. {
  117. Key = "devices",
  118. ConfigurationType = typeof(DevicesOptions)
  119. }
  120. };
  121. }
  122. }
  123. public static class UploadConfigExtension
  124. {
  125. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  126. {
  127. return config.GetConfiguration<DevicesOptions>("devices");
  128. }
  129. }
  130. }