ContentDirectoryService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Threading.Tasks;
  6. using Emby.Dlna.Service;
  7. using Jellyfin.Data.Entities;
  8. using Jellyfin.Data.Enums;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Dlna;
  11. using MediaBrowser.Controller.Drawing;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.MediaEncoding;
  14. using MediaBrowser.Controller.TV;
  15. using MediaBrowser.Model.Dlna;
  16. using MediaBrowser.Model.Globalization;
  17. using Microsoft.Extensions.Logging;
  18. namespace Emby.Dlna.ContentDirectory
  19. {
  20. /// <summary>
  21. /// Defines the <see cref="ContentDirectoryService" />.
  22. /// </summary>
  23. public class ContentDirectoryService : BaseService, IContentDirectory
  24. {
  25. private readonly ILibraryManager _libraryManager;
  26. private readonly IImageProcessor _imageProcessor;
  27. private readonly IUserDataManager _userDataManager;
  28. private readonly IDlnaManager _dlna;
  29. private readonly IServerConfigurationManager _config;
  30. private readonly IUserManager _userManager;
  31. private readonly ILocalizationManager _localization;
  32. private readonly IMediaSourceManager _mediaSourceManager;
  33. private readonly IUserViewManager _userViewManager;
  34. private readonly IMediaEncoder _mediaEncoder;
  35. private readonly ITVSeriesManager _tvSeriesManager;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="ContentDirectoryService"/> class.
  38. /// </summary>
  39. /// <param name="dlna">The <see cref="IDlnaManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  40. /// <param name="userDataManager">The <see cref="IUserDataManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  41. /// <param name="imageProcessor">The <see cref="IImageProcessor"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  42. /// <param name="libraryManager">The <see cref="ILibraryManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  43. /// <param name="config">The <see cref="IServerConfigurationManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  44. /// <param name="userManager">The <see cref="IUserManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  45. /// <param name="logger">The <see cref="ILogger{ContentDirectoryService}"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  46. /// <param name="httpClient">The <see cref="IHttpClientFactory"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  47. /// <param name="localization">The <see cref="ILocalizationManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  48. /// <param name="mediaSourceManager">The <see cref="IMediaSourceManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  49. /// <param name="userViewManager">The <see cref="IUserViewManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  50. /// <param name="mediaEncoder">The <see cref="IMediaEncoder"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  51. /// <param name="tvSeriesManager">The <see cref="ITVSeriesManager"/> to use in the <see cref="ContentDirectoryService"/> instance.</param>
  52. public ContentDirectoryService(
  53. IDlnaManager dlna,
  54. IUserDataManager userDataManager,
  55. IImageProcessor imageProcessor,
  56. ILibraryManager libraryManager,
  57. IServerConfigurationManager config,
  58. IUserManager userManager,
  59. ILogger<ContentDirectoryService> logger,
  60. IHttpClientFactory httpClient,
  61. ILocalizationManager localization,
  62. IMediaSourceManager mediaSourceManager,
  63. IUserViewManager userViewManager,
  64. IMediaEncoder mediaEncoder,
  65. ITVSeriesManager tvSeriesManager)
  66. : base(logger, httpClient)
  67. {
  68. _dlna = dlna;
  69. _userDataManager = userDataManager;
  70. _imageProcessor = imageProcessor;
  71. _libraryManager = libraryManager;
  72. _config = config;
  73. _userManager = userManager;
  74. _localization = localization;
  75. _mediaSourceManager = mediaSourceManager;
  76. _userViewManager = userViewManager;
  77. _mediaEncoder = mediaEncoder;
  78. _tvSeriesManager = tvSeriesManager;
  79. }
  80. /// <summary>
  81. /// Gets the system id. (A unique id which changes on when our definition changes.)
  82. /// </summary>
  83. private static int SystemUpdateId
  84. {
  85. get
  86. {
  87. var now = DateTime.UtcNow;
  88. return now.Year + now.DayOfYear + now.Hour;
  89. }
  90. }
  91. /// <inheritdoc />
  92. public string GetServiceXml()
  93. {
  94. return ContentDirectoryXmlBuilder.GetXml();
  95. }
  96. /// <inheritdoc />
  97. public Task<ControlResponse> ProcessControlRequestAsync(ControlRequest request)
  98. {
  99. if (request == null)
  100. {
  101. throw new ArgumentNullException(nameof(request));
  102. }
  103. var profile = _dlna.GetProfile(request.Headers) ?? _dlna.GetDefaultProfile();
  104. var serverAddress = request.RequestedUrl.Substring(0, request.RequestedUrl.IndexOf("/dlna", StringComparison.OrdinalIgnoreCase));
  105. var user = GetUser(profile);
  106. return new ControlHandler(
  107. Logger,
  108. _libraryManager,
  109. profile,
  110. serverAddress,
  111. null,
  112. _imageProcessor,
  113. _userDataManager,
  114. user,
  115. SystemUpdateId,
  116. _config,
  117. _localization,
  118. _mediaSourceManager,
  119. _userViewManager,
  120. _mediaEncoder,
  121. _tvSeriesManager)
  122. .ProcessControlRequestAsync(request);
  123. }
  124. /// <summary>
  125. /// Get the user stored in the device profile.
  126. /// </summary>
  127. /// <param name="profile">The <see cref="DeviceProfile"/>.</param>
  128. /// <returns>The <see cref="User"/>.</returns>
  129. private User? GetUser(DeviceProfile profile)
  130. {
  131. if (!string.IsNullOrEmpty(profile.UserId))
  132. {
  133. var user = _userManager.GetUserById(Guid.Parse(profile.UserId));
  134. if (user != null)
  135. {
  136. return user;
  137. }
  138. }
  139. var userId = _config.GetDlnaConfiguration().DefaultUserId;
  140. if (!string.IsNullOrEmpty(userId))
  141. {
  142. var user = _userManager.GetUserById(Guid.Parse(userId));
  143. if (user != null)
  144. {
  145. return user;
  146. }
  147. }
  148. foreach (var user in _userManager.Users)
  149. {
  150. if (user.HasPermission(PermissionKind.IsAdministrator))
  151. {
  152. return user;
  153. }
  154. }
  155. return _userManager.Users.FirstOrDefault();
  156. }
  157. }
  158. }