StudiosController.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using Jellyfin.Api.Constants;
  4. using Jellyfin.Api.Extensions;
  5. using Jellyfin.Api.Helpers;
  6. using Jellyfin.Api.ModelBinders;
  7. using Jellyfin.Data.Entities;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Querying;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. namespace Jellyfin.Api.Controllers
  18. {
  19. /// <summary>
  20. /// Studios controller.
  21. /// </summary>
  22. [Authorize(Policy = Policies.DefaultAuthorization)]
  23. public class StudiosController : BaseJellyfinApiController
  24. {
  25. private readonly ILibraryManager _libraryManager;
  26. private readonly IUserManager _userManager;
  27. private readonly IDtoService _dtoService;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="StudiosController"/> class.
  30. /// </summary>
  31. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  32. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  33. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  34. public StudiosController(
  35. ILibraryManager libraryManager,
  36. IUserManager userManager,
  37. IDtoService dtoService)
  38. {
  39. _libraryManager = libraryManager;
  40. _userManager = userManager;
  41. _dtoService = dtoService;
  42. }
  43. /// <summary>
  44. /// Gets all studios from a given item, folder, or the entire library.
  45. /// </summary>
  46. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  47. /// <param name="limit">Optional. The maximum number of records to return.</param>
  48. /// <param name="searchTerm">Optional. Search term.</param>
  49. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  50. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  51. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  52. /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>
  53. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  54. /// <param name="enableUserData">Optional, include user data.</param>
  55. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  56. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  57. /// <param name="userId">User id.</param>
  58. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  59. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  60. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  61. /// <param name="enableImages">Optional, include image information in output.</param>
  62. /// <param name="enableTotalRecordCount">Total record count.</param>
  63. /// <response code="200">Studios returned.</response>
  64. /// <returns>An <see cref="OkResult"/> containing the studios.</returns>
  65. [HttpGet]
  66. [ProducesResponseType(StatusCodes.Status200OK)]
  67. public ActionResult<QueryResult<BaseItemDto>> GetStudios(
  68. [FromQuery] int? startIndex,
  69. [FromQuery] int? limit,
  70. [FromQuery] string? searchTerm,
  71. [FromQuery] string? parentId,
  72. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  73. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] excludeItemTypes,
  74. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] includeItemTypes,
  75. [FromQuery] bool? isFavorite,
  76. [FromQuery] bool? enableUserData,
  77. [FromQuery] int? imageTypeLimit,
  78. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
  79. [FromQuery] Guid? userId,
  80. [FromQuery] string? nameStartsWithOrGreater,
  81. [FromQuery] string? nameStartsWith,
  82. [FromQuery] string? nameLessThan,
  83. [FromQuery] bool? enableImages = true,
  84. [FromQuery] bool enableTotalRecordCount = true)
  85. {
  86. var dtoOptions = new DtoOptions { Fields = fields }
  87. .AddClientFields(Request)
  88. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  89. User? user = userId.HasValue && userId != Guid.Empty ? _userManager.GetUserById(userId.Value) : null;
  90. var parentItem = _libraryManager.GetParentItem(parentId, userId);
  91. var query = new InternalItemsQuery(user)
  92. {
  93. ExcludeItemTypes = excludeItemTypes,
  94. IncludeItemTypes = includeItemTypes,
  95. StartIndex = startIndex,
  96. Limit = limit,
  97. IsFavorite = isFavorite,
  98. NameLessThan = nameLessThan,
  99. NameStartsWith = nameStartsWith,
  100. NameStartsWithOrGreater = nameStartsWithOrGreater,
  101. DtoOptions = dtoOptions,
  102. SearchTerm = searchTerm,
  103. EnableTotalRecordCount = enableTotalRecordCount
  104. };
  105. if (!string.IsNullOrWhiteSpace(parentId))
  106. {
  107. if (parentItem is Folder)
  108. {
  109. query.AncestorIds = new[] { new Guid(parentId) };
  110. }
  111. else
  112. {
  113. query.ItemIds = new[] { new Guid(parentId) };
  114. }
  115. }
  116. var result = _libraryManager.GetStudios(query);
  117. var shouldIncludeItemTypes = includeItemTypes.Length != 0;
  118. return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
  119. }
  120. /// <summary>
  121. /// Gets a studio by name.
  122. /// </summary>
  123. /// <param name="name">Studio name.</param>
  124. /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
  125. /// <response code="200">Studio returned.</response>
  126. /// <returns>An <see cref="OkResult"/> containing the studio.</returns>
  127. [HttpGet("{name}")]
  128. [ProducesResponseType(StatusCodes.Status200OK)]
  129. public ActionResult<BaseItemDto> GetStudio([FromRoute, Required] string name, [FromQuery] Guid? userId)
  130. {
  131. var dtoOptions = new DtoOptions().AddClientFields(Request);
  132. var item = _libraryManager.GetStudio(name);
  133. if (userId.HasValue && !userId.Equals(Guid.Empty))
  134. {
  135. var user = _userManager.GetUserById(userId.Value);
  136. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  137. }
  138. return _dtoService.GetBaseItemDto(item, dtoOptions);
  139. }
  140. }
  141. }