ContentDirectory.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Dlna;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Dto;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Dlna;
  8. using MediaBrowser.Model.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. namespace MediaBrowser.Dlna.Server
  13. {
  14. public class ContentDirectory : IContentDirectory, IDisposable
  15. {
  16. private readonly ILogger _logger;
  17. private readonly ILibraryManager _libraryManager;
  18. private readonly IDtoService _dtoService;
  19. private readonly IImageProcessor _imageProcessor;
  20. private readonly IUserDataManager _userDataManager;
  21. private readonly IDlnaManager _dlna;
  22. private readonly IServerConfigurationManager _config;
  23. private readonly IUserManager _userManager;
  24. private readonly IEventManager _eventManager;
  25. public ContentDirectory(IDlnaManager dlna,
  26. IUserDataManager userDataManager,
  27. IImageProcessor imageProcessor,
  28. IDtoService dtoService,
  29. ILibraryManager libraryManager,
  30. ILogManager logManager,
  31. IServerConfigurationManager config,
  32. IUserManager userManager,
  33. IEventManager eventManager)
  34. {
  35. _dlna = dlna;
  36. _userDataManager = userDataManager;
  37. _imageProcessor = imageProcessor;
  38. _dtoService = dtoService;
  39. _libraryManager = libraryManager;
  40. _config = config;
  41. _userManager = userManager;
  42. _eventManager = eventManager;
  43. _logger = logManager.GetLogger("DlnaContentDirectory");
  44. }
  45. private int SystemUpdateId
  46. {
  47. get
  48. {
  49. var now = DateTime.UtcNow;
  50. return now.Year + now.DayOfYear + now.Hour;
  51. }
  52. }
  53. public string GetContentDirectoryXml(IDictionary<string, string> headers)
  54. {
  55. var profile = _dlna.GetProfile(headers) ??
  56. _dlna.GetDefaultProfile();
  57. return new ContentDirectoryXmlBuilder(profile).GetXml();
  58. }
  59. public ControlResponse ProcessControlRequest(ControlRequest request)
  60. {
  61. var profile = _dlna.GetProfile(request.Headers) ??
  62. _dlna.GetDefaultProfile();
  63. var serverAddress = request.RequestedUrl.Substring(0, request.RequestedUrl.IndexOf("/dlna", StringComparison.OrdinalIgnoreCase));
  64. var user = GetUser(profile);
  65. return new ControlHandler(
  66. _logger,
  67. _libraryManager,
  68. profile,
  69. serverAddress,
  70. _dtoService,
  71. _imageProcessor,
  72. _userDataManager,
  73. user,
  74. SystemUpdateId,
  75. _config)
  76. .ProcessControlRequest(request);
  77. }
  78. private User GetUser(DeviceProfile profile)
  79. {
  80. if (!string.IsNullOrEmpty(profile.UserId))
  81. {
  82. var user = _userManager.GetUserById(new Guid(profile.UserId));
  83. if (user != null)
  84. {
  85. return user;
  86. }
  87. }
  88. if (!string.IsNullOrEmpty(_config.Configuration.DlnaOptions.DefaultUserId))
  89. {
  90. var user = _userManager.GetUserById(new Guid(_config.Configuration.DlnaOptions.DefaultUserId));
  91. if (user != null)
  92. {
  93. return user;
  94. }
  95. }
  96. // No configuration so it's going to be pretty arbitrary
  97. return _userManager.Users.First();
  98. }
  99. public void Dispose()
  100. {
  101. }
  102. }
  103. }