DeviceManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 usedByUserId)
  30. {
  31. var device = GetDevice(reportedId) ?? new DeviceInfo
  32. {
  33. Id = reportedId
  34. };
  35. device.Name = name;
  36. if (!string.IsNullOrWhiteSpace(usedByUserId))
  37. {
  38. var user = _userManager.GetUserById(usedByUserId);
  39. device.LastUserId = user.Id.ToString("N");
  40. device.LastUserName = user.Name;
  41. }
  42. device.DateLastModified = DateTime.UtcNow;
  43. return _repo.SaveDevice(device);
  44. }
  45. public Task SaveCapabilities(string reportedId, ClientCapabilities capabilities)
  46. {
  47. return _repo.SaveCapabilities(reportedId, capabilities);
  48. }
  49. public ClientCapabilities GetCapabilities(string reportedId)
  50. {
  51. return _repo.GetCapabilities(reportedId);
  52. }
  53. public DeviceInfo GetDevice(string id)
  54. {
  55. return _repo.GetDevice(id);
  56. }
  57. public IEnumerable<DeviceInfo> GetDevices()
  58. {
  59. return _repo.GetDevices().OrderByDescending(i => i.DateLastModified);
  60. }
  61. public Task DeleteDevice(string id)
  62. {
  63. return _repo.DeleteDevice(id);
  64. }
  65. public ContentUploadHistory GetCameraUploadHistory(string deviceId)
  66. {
  67. return _repo.GetCameraUploadHistory(deviceId);
  68. }
  69. public async Task AcceptCameraUpload(string deviceId, Stream stream, LocalFileInfo file)
  70. {
  71. var path = GetUploadPath(deviceId);
  72. if (!string.IsNullOrWhiteSpace(file.Album))
  73. {
  74. path = Path.Combine(path, _fileSystem.GetValidFilename(file.Album));
  75. }
  76. Directory.CreateDirectory(path);
  77. path = Path.Combine(path, file.Name);
  78. _libraryMonitor.ReportFileSystemChangeBeginning(path);
  79. try
  80. {
  81. using (var fs = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
  82. {
  83. await stream.CopyToAsync(fs).ConfigureAwait(false);
  84. }
  85. _repo.AddCameraUpload(deviceId, file);
  86. }
  87. finally
  88. {
  89. _libraryMonitor.ReportFileSystemChangeComplete(path, true);
  90. }
  91. }
  92. private string GetUploadPath(string deviceId)
  93. {
  94. var config = _config.GetUploadOptions();
  95. if (!string.IsNullOrWhiteSpace(config.CameraUploadPath))
  96. {
  97. return config.CameraUploadPath;
  98. }
  99. return Path.Combine(_config.CommonApplicationPaths.DataPath, "camerauploads");
  100. }
  101. }
  102. public class DevicesConfigStore : IConfigurationFactory
  103. {
  104. public IEnumerable<ConfigurationStore> GetConfigurations()
  105. {
  106. return new List<ConfigurationStore>
  107. {
  108. new ConfigurationStore
  109. {
  110. Key = "devices",
  111. ConfigurationType = typeof(DevicesOptions)
  112. }
  113. };
  114. }
  115. }
  116. public static class UploadConfigExtension
  117. {
  118. public static DevicesOptions GetUploadOptions(this IConfigurationManager config)
  119. {
  120. return config.GetConfiguration<DevicesOptions>("devices");
  121. }
  122. }
  123. }