GenresController.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Linq;
  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. using Genre = MediaBrowser.Controller.Entities.Genre;
  19. namespace Jellyfin.Api.Controllers;
  20. /// <summary>
  21. /// The genres controller.
  22. /// </summary>
  23. [Authorize]
  24. public class GenresController : BaseJellyfinApiController
  25. {
  26. private readonly IUserManager _userManager;
  27. private readonly ILibraryManager _libraryManager;
  28. private readonly IDtoService _dtoService;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="GenresController"/> class.
  31. /// </summary>
  32. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  33. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  34. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  35. public GenresController(
  36. IUserManager userManager,
  37. ILibraryManager libraryManager,
  38. IDtoService dtoService)
  39. {
  40. _userManager = userManager;
  41. _libraryManager = libraryManager;
  42. _dtoService = dtoService;
  43. }
  44. /// <summary>
  45. /// Gets all genres 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">The 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 in 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="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="sortBy">Optional. Specify one or more sort orders, comma delimited.</param>
  62. /// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
  63. /// <param name="enableImages">Optional, include image information in output.</param>
  64. /// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
  65. /// <response code="200">Genres returned.</response>
  66. /// <returns>An <see cref="OkResult"/> containing the queryresult of genres.</returns>
  67. [HttpGet]
  68. [ProducesResponseType(StatusCodes.Status200OK)]
  69. public ActionResult<QueryResult<BaseItemDto>> GetGenres(
  70. [FromQuery] int? startIndex,
  71. [FromQuery] int? limit,
  72. [FromQuery] string? searchTerm,
  73. [FromQuery] Guid? parentId,
  74. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  75. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
  76. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
  77. [FromQuery] bool? isFavorite,
  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, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] sortBy,
  85. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
  86. [FromQuery] bool? enableImages = true,
  87. [FromQuery] bool enableTotalRecordCount = true)
  88. {
  89. var dtoOptions = new DtoOptions { Fields = fields }
  90. .AddClientFields(User)
  91. .AddAdditionalDtoOptions(enableImages, false, imageTypeLimit, enableImageTypes);
  92. User? user = userId is null || userId.Value.Equals(default)
  93. ? null
  94. : _userManager.GetUserById(userId.Value);
  95. var parentItem = _libraryManager.GetParentItem(parentId, userId);
  96. var query = new InternalItemsQuery(user)
  97. {
  98. ExcludeItemTypes = excludeItemTypes,
  99. IncludeItemTypes = includeItemTypes,
  100. StartIndex = startIndex,
  101. Limit = limit,
  102. IsFavorite = isFavorite,
  103. NameLessThan = nameLessThan,
  104. NameStartsWith = nameStartsWith,
  105. NameStartsWithOrGreater = nameStartsWithOrGreater,
  106. DtoOptions = dtoOptions,
  107. SearchTerm = searchTerm,
  108. EnableTotalRecordCount = enableTotalRecordCount,
  109. OrderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder)
  110. };
  111. if (parentId.HasValue)
  112. {
  113. if (parentItem is Folder)
  114. {
  115. query.AncestorIds = new[] { parentId.Value };
  116. }
  117. else
  118. {
  119. query.ItemIds = new[] { parentId.Value };
  120. }
  121. }
  122. QueryResult<(BaseItem, ItemCounts)> result;
  123. if (parentItem is ICollectionFolder parentCollectionFolder
  124. && (string.Equals(parentCollectionFolder.CollectionType, CollectionType.Music, StringComparison.Ordinal)
  125. || string.Equals(parentCollectionFolder.CollectionType, CollectionType.MusicVideos, StringComparison.Ordinal)))
  126. {
  127. result = _libraryManager.GetMusicGenres(query);
  128. }
  129. else
  130. {
  131. result = _libraryManager.GetGenres(query);
  132. }
  133. var shouldIncludeItemTypes = includeItemTypes.Length != 0;
  134. return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
  135. }
  136. /// <summary>
  137. /// Gets a genre, by name.
  138. /// </summary>
  139. /// <param name="genreName">The genre name.</param>
  140. /// <param name="userId">The user id.</param>
  141. /// <response code="200">Genres returned.</response>
  142. /// <returns>An <see cref="OkResult"/> containing the genre.</returns>
  143. [HttpGet("{genreName}")]
  144. [ProducesResponseType(StatusCodes.Status200OK)]
  145. public ActionResult<BaseItemDto> GetGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
  146. {
  147. var dtoOptions = new DtoOptions()
  148. .AddClientFields(User);
  149. Genre? item;
  150. if (genreName.Contains(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase))
  151. {
  152. item = GetItemFromSlugName<Genre>(_libraryManager, genreName, dtoOptions, BaseItemKind.Genre);
  153. }
  154. else
  155. {
  156. item = _libraryManager.GetGenre(genreName);
  157. }
  158. item ??= new Genre();
  159. if (userId is null || userId.Value.Equals(default))
  160. {
  161. return _dtoService.GetBaseItemDto(item, dtoOptions);
  162. }
  163. var user = _userManager.GetUserById(userId.Value);
  164. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  165. }
  166. private T? GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions, BaseItemKind baseItemKind)
  167. where T : BaseItem, new()
  168. {
  169. var result = libraryManager.GetItemList(new InternalItemsQuery
  170. {
  171. Name = name.Replace(BaseItem.SlugChar, '&'),
  172. IncludeItemTypes = new[] { baseItemKind },
  173. DtoOptions = dtoOptions
  174. }).OfType<T>().FirstOrDefault();
  175. result ??= libraryManager.GetItemList(new InternalItemsQuery
  176. {
  177. Name = name.Replace(BaseItem.SlugChar, '/'),
  178. IncludeItemTypes = new[] { baseItemKind },
  179. DtoOptions = dtoOptions
  180. }).OfType<T>().FirstOrDefault();
  181. result ??= libraryManager.GetItemList(new InternalItemsQuery
  182. {
  183. Name = name.Replace(BaseItem.SlugChar, '?'),
  184. IncludeItemTypes = new[] { baseItemKind },
  185. DtoOptions = dtoOptions
  186. }).OfType<T>().FirstOrDefault();
  187. return result;
  188. }
  189. }