ContentDirectory.cs 3.6 KB

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