ContentDirectory.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.ContentDirectory
  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("UpnpContentDirectory");
  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 GetServiceXml(IDictionary<string, string> headers)
  54. {
  55. return new ContentDirectoryXmlBuilder().GetXml();
  56. }
  57. public ControlResponse ProcessControlRequest(ControlRequest request)
  58. {
  59. var profile = _dlna.GetProfile(request.Headers) ??
  60. _dlna.GetDefaultProfile();
  61. var serverAddress = request.RequestedUrl.Substring(0, request.RequestedUrl.IndexOf("/dlna", StringComparison.OrdinalIgnoreCase));
  62. var user = GetUser(profile);
  63. return new ControlHandler(
  64. _logger,
  65. _libraryManager,
  66. profile,
  67. serverAddress,
  68. _dtoService,
  69. _imageProcessor,
  70. _userDataManager,
  71. user,
  72. SystemUpdateId,
  73. _config)
  74. .ProcessControlRequest(request);
  75. }
  76. private User GetUser(DeviceProfile profile)
  77. {
  78. if (!string.IsNullOrEmpty(profile.UserId))
  79. {
  80. var user = _userManager.GetUserById(new Guid(profile.UserId));
  81. if (user != null)
  82. {
  83. return user;
  84. }
  85. }
  86. if (!string.IsNullOrEmpty(_config.Configuration.DlnaOptions.DefaultUserId))
  87. {
  88. var user = _userManager.GetUserById(new Guid(_config.Configuration.DlnaOptions.DefaultUserId));
  89. if (user != null)
  90. {
  91. return user;
  92. }
  93. }
  94. // No configuration so it's going to be pretty arbitrary
  95. return _userManager.Users.First();
  96. }
  97. public void Dispose()
  98. {
  99. }
  100. }
  101. }