StudiosController.cs 7.5 KB

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