GenresController.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Linq;
  4. using Jellyfin.Api.Constants;
  5. using Jellyfin.Api.Extensions;
  6. using Jellyfin.Api.Helpers;
  7. using Jellyfin.Api.ModelBinders;
  8. using Jellyfin.Data.Entities;
  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. using Genre = MediaBrowser.Controller.Entities.Genre;
  19. namespace Jellyfin.Api.Controllers
  20. {
  21. /// <summary>
  22. /// The genres controller.
  23. /// </summary>
  24. [Authorize(Policy = Policies.DefaultAuthorization)]
  25. public class GenresController : BaseJellyfinApiController
  26. {
  27. private readonly IUserManager _userManager;
  28. private readonly ILibraryManager _libraryManager;
  29. private readonly IDtoService _dtoService;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="GenresController"/> class.
  32. /// </summary>
  33. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  34. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  35. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  36. public GenresController(
  37. IUserManager userManager,
  38. ILibraryManager libraryManager,
  39. IDtoService dtoService)
  40. {
  41. _userManager = userManager;
  42. _libraryManager = libraryManager;
  43. _dtoService = dtoService;
  44. }
  45. /// <summary>
  46. /// Gets all genres from a given item, folder, or the entire library.
  47. /// </summary>
  48. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  49. /// <param name="limit">Optional. The maximum number of records to return.</param>
  50. /// <param name="searchTerm">The search term.</param>
  51. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  52. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  53. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  54. /// <param name="includeItemTypes">Optional. If specified, results will be filtered in based on item type. This allows multiple, comma delimited.</param>
  55. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</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">Optional. Include total record count.</param>
  64. /// <response code="200">Genres returned.</response>
  65. /// <returns>An <see cref="OkResult"/> containing the queryresult of genres.</returns>
  66. [HttpGet]
  67. [ProducesResponseType(StatusCodes.Status200OK)]
  68. public ActionResult<QueryResult<BaseItemDto>> GetGenres(
  69. [FromQuery] int? startIndex,
  70. [FromQuery] int? limit,
  71. [FromQuery] string? searchTerm,
  72. [FromQuery] string? parentId,
  73. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  74. [FromQuery] string? excludeItemTypes,
  75. [FromQuery] string? includeItemTypes,
  76. [FromQuery] bool? isFavorite,
  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, false, 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 = RequestHelpers.Split(excludeItemTypes, ',', true),
  94. IncludeItemTypes = RequestHelpers.Split(includeItemTypes, ',', true),
  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. QueryResult<(BaseItem, ItemCounts)> result;
  117. if (parentItem is ICollectionFolder parentCollectionFolder
  118. && (string.Equals(parentCollectionFolder.CollectionType, CollectionType.Music, StringComparison.Ordinal)
  119. || string.Equals(parentCollectionFolder.CollectionType, CollectionType.MusicVideos, StringComparison.Ordinal)))
  120. {
  121. result = _libraryManager.GetMusicGenres(query);
  122. }
  123. else
  124. {
  125. result = _libraryManager.GetGenres(query);
  126. }
  127. var shouldIncludeItemTypes = !string.IsNullOrEmpty(includeItemTypes);
  128. return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
  129. }
  130. /// <summary>
  131. /// Gets a genre, by name.
  132. /// </summary>
  133. /// <param name="genreName">The genre name.</param>
  134. /// <param name="userId">The user id.</param>
  135. /// <response code="200">Genres returned.</response>
  136. /// <returns>An <see cref="OkResult"/> containing the genre.</returns>
  137. [HttpGet("{genreName}")]
  138. [ProducesResponseType(StatusCodes.Status200OK)]
  139. public ActionResult<BaseItemDto> GetGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
  140. {
  141. var dtoOptions = new DtoOptions()
  142. .AddClientFields(Request);
  143. Genre item = new Genre();
  144. if (genreName.IndexOf(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase) != -1)
  145. {
  146. var result = GetItemFromSlugName<Genre>(_libraryManager, genreName, dtoOptions);
  147. if (result != null)
  148. {
  149. item = result;
  150. }
  151. }
  152. else
  153. {
  154. item = _libraryManager.GetGenre(genreName);
  155. }
  156. if (userId.HasValue && !userId.Equals(Guid.Empty))
  157. {
  158. var user = _userManager.GetUserById(userId.Value);
  159. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  160. }
  161. return _dtoService.GetBaseItemDto(item, dtoOptions);
  162. }
  163. private T GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions)
  164. where T : BaseItem, new()
  165. {
  166. var result = libraryManager.GetItemList(new InternalItemsQuery
  167. {
  168. Name = name.Replace(BaseItem.SlugChar, '&'),
  169. IncludeItemTypes = new[] { typeof(T).Name },
  170. DtoOptions = dtoOptions
  171. }).OfType<T>().FirstOrDefault();
  172. result ??= libraryManager.GetItemList(new InternalItemsQuery
  173. {
  174. Name = name.Replace(BaseItem.SlugChar, '/'),
  175. IncludeItemTypes = new[] { typeof(T).Name },
  176. DtoOptions = dtoOptions
  177. }).OfType<T>().FirstOrDefault();
  178. result ??= libraryManager.GetItemList(new InternalItemsQuery
  179. {
  180. Name = name.Replace(BaseItem.SlugChar, '?'),
  181. IncludeItemTypes = new[] { typeof(T).Name },
  182. DtoOptions = dtoOptions
  183. }).OfType<T>().FirstOrDefault();
  184. return result;
  185. }
  186. }
  187. }