ContentDirectoryService.cs 7.2 KB

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