2
0

ContentDirectoryService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. ArgumentNullException.ThrowIfNull(request);
  100. var profile = _dlna.GetProfile(request.Headers) ?? _dlna.GetDefaultProfile();
  101. var serverAddress = request.RequestedUrl.Substring(0, request.RequestedUrl.IndexOf("/dlna", StringComparison.OrdinalIgnoreCase));
  102. var user = GetUser(profile);
  103. return new ControlHandler(
  104. Logger,
  105. _libraryManager,
  106. profile,
  107. serverAddress,
  108. null,
  109. _imageProcessor,
  110. _userDataManager,
  111. user,
  112. SystemUpdateId,
  113. _config,
  114. _localization,
  115. _mediaSourceManager,
  116. _userViewManager,
  117. _mediaEncoder,
  118. _tvSeriesManager)
  119. .ProcessControlRequestAsync(request);
  120. }
  121. /// <summary>
  122. /// Get the user stored in the device profile.
  123. /// </summary>
  124. /// <param name="profile">The <see cref="DeviceProfile"/>.</param>
  125. /// <returns>The <see cref="User"/>.</returns>
  126. private User? GetUser(DeviceProfile profile)
  127. {
  128. if (!string.IsNullOrEmpty(profile.UserId))
  129. {
  130. var user = _userManager.GetUserById(Guid.Parse(profile.UserId));
  131. if (user is not null)
  132. {
  133. return user;
  134. }
  135. }
  136. var userId = _config.GetDlnaConfiguration().DefaultUserId;
  137. if (!string.IsNullOrEmpty(userId))
  138. {
  139. var user = _userManager.GetUserById(Guid.Parse(userId));
  140. if (user is not null)
  141. {
  142. return user;
  143. }
  144. }
  145. foreach (var user in _userManager.Users)
  146. {
  147. if (user.HasPermission(PermissionKind.IsAdministrator))
  148. {
  149. return user;
  150. }
  151. }
  152. return _userManager.Users.FirstOrDefault();
  153. }
  154. }
  155. }