GenresController.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.Enums;
  8. using Jellyfin.Database.Implementations.Entities;
  9. using Jellyfin.Database.Implementations.Enums;
  10. using Jellyfin.Extensions;
  11. using MediaBrowser.Controller.Dto;
  12. using MediaBrowser.Controller.Entities;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.Querying;
  17. using Microsoft.AspNetCore.Authorization;
  18. using Microsoft.AspNetCore.Http;
  19. using Microsoft.AspNetCore.Mvc;
  20. using Genre = MediaBrowser.Controller.Entities.Genre;
  21. namespace Jellyfin.Api.Controllers;
  22. /// <summary>
  23. /// The genres controller.
  24. /// </summary>
  25. [Authorize]
  26. public class GenresController : BaseJellyfinApiController
  27. {
  28. private readonly IUserManager _userManager;
  29. private readonly ILibraryManager _libraryManager;
  30. private readonly IDtoService _dtoService;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="GenresController"/> class.
  33. /// </summary>
  34. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  35. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  36. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  37. public GenresController(
  38. IUserManager userManager,
  39. ILibraryManager libraryManager,
  40. IDtoService dtoService)
  41. {
  42. _userManager = userManager;
  43. _libraryManager = libraryManager;
  44. _dtoService = dtoService;
  45. }
  46. /// <summary>
  47. /// Gets all genres from a given item, folder, or the entire library.
  48. /// </summary>
  49. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  50. /// <param name="limit">Optional. The maximum number of records to return.</param>
  51. /// <param name="searchTerm">The search term.</param>
  52. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  53. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  54. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  55. /// <param name="includeItemTypes">Optional. If specified, results will be filtered in based on item type. This allows multiple, comma delimited.</param>
  56. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  57. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  58. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  59. /// <param name="userId">User id.</param>
  60. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  61. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  62. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  63. /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited.</param>
  64. /// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
  65. /// <param name="enableImages">Optional, include image information in output.</param>
  66. /// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
  67. /// <response code="200">Genres returned.</response>
  68. /// <returns>An <see cref="OkResult"/> containing the queryresult of genres.</returns>
  69. [HttpGet]
  70. [ProducesResponseType(StatusCodes.Status200OK)]
  71. public ActionResult<QueryResult<BaseItemDto>> GetGenres(
  72. [FromQuery] int? startIndex,
  73. [FromQuery] int? limit,
  74. [FromQuery] string? searchTerm,
  75. [FromQuery] Guid? parentId,
  76. [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields,
  77. [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] excludeItemTypes,
  78. [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] includeItemTypes,
  79. [FromQuery] bool? isFavorite,
  80. [FromQuery] int? imageTypeLimit,
  81. [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ImageType[] enableImageTypes,
  82. [FromQuery] Guid? userId,
  83. [FromQuery] string? nameStartsWithOrGreater,
  84. [FromQuery] string? nameStartsWith,
  85. [FromQuery] string? nameLessThan,
  86. [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemSortBy[] sortBy,
  87. [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] SortOrder[] sortOrder,
  88. [FromQuery] bool? enableImages = true,
  89. [FromQuery] bool enableTotalRecordCount = true)
  90. {
  91. userId = RequestHelpers.GetUserId(User, userId);
  92. var dtoOptions = new DtoOptions { Fields = fields }
  93. .AddClientFields(User)
  94. .AddAdditionalDtoOptions(enableImages, false, imageTypeLimit, enableImageTypes);
  95. User? user = userId.IsNullOrEmpty()
  96. ? null
  97. : _userManager.GetUserById(userId.Value);
  98. var parentItem = _libraryManager.GetParentItem(parentId, userId);
  99. var query = new InternalItemsQuery(user)
  100. {
  101. ExcludeItemTypes = excludeItemTypes,
  102. IncludeItemTypes = includeItemTypes,
  103. StartIndex = startIndex,
  104. Limit = limit,
  105. IsFavorite = isFavorite,
  106. NameLessThan = nameLessThan,
  107. NameStartsWith = nameStartsWith,
  108. NameStartsWithOrGreater = nameStartsWithOrGreater,
  109. DtoOptions = dtoOptions,
  110. SearchTerm = searchTerm,
  111. EnableTotalRecordCount = enableTotalRecordCount,
  112. OrderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder)
  113. };
  114. if (parentId.HasValue)
  115. {
  116. if (parentItem is Folder)
  117. {
  118. query.AncestorIds = new[] { parentId.Value };
  119. }
  120. else
  121. {
  122. query.ItemIds = new[] { parentId.Value };
  123. }
  124. }
  125. QueryResult<(BaseItem, ItemCounts)> result;
  126. if (parentItem is ICollectionFolder parentCollectionFolder
  127. && (parentCollectionFolder.CollectionType == CollectionType.music
  128. || parentCollectionFolder.CollectionType == CollectionType.musicvideos))
  129. {
  130. result = _libraryManager.GetMusicGenres(query);
  131. }
  132. else
  133. {
  134. result = _libraryManager.GetGenres(query);
  135. }
  136. var shouldIncludeItemTypes = includeItemTypes.Length != 0;
  137. return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
  138. }
  139. /// <summary>
  140. /// Gets a genre, by name.
  141. /// </summary>
  142. /// <param name="genreName">The genre name.</param>
  143. /// <param name="userId">The user id.</param>
  144. /// <response code="200">Genres returned.</response>
  145. /// <returns>An <see cref="OkResult"/> containing the genre.</returns>
  146. [HttpGet("{genreName}")]
  147. [ProducesResponseType(StatusCodes.Status200OK)]
  148. public ActionResult<BaseItemDto> GetGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
  149. {
  150. userId = RequestHelpers.GetUserId(User, userId);
  151. var dtoOptions = new DtoOptions()
  152. .AddClientFields(User);
  153. Genre? item;
  154. if (genreName.Contains(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase))
  155. {
  156. item = GetItemFromSlugName<Genre>(_libraryManager, genreName, dtoOptions, BaseItemKind.Genre);
  157. }
  158. else
  159. {
  160. item = _libraryManager.GetGenre(genreName);
  161. }
  162. item ??= new Genre();
  163. var user = userId.IsNullOrEmpty()
  164. ? null
  165. : _userManager.GetUserById(userId.Value);
  166. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  167. }
  168. private T? GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions, BaseItemKind baseItemKind)
  169. where T : BaseItem, new()
  170. {
  171. var result = libraryManager.GetItemList(new InternalItemsQuery
  172. {
  173. Name = name.Replace(BaseItem.SlugChar, '&'),
  174. IncludeItemTypes = new[] { baseItemKind },
  175. DtoOptions = dtoOptions
  176. }).OfType<T>().FirstOrDefault();
  177. result ??= libraryManager.GetItemList(new InternalItemsQuery
  178. {
  179. Name = name.Replace(BaseItem.SlugChar, '/'),
  180. IncludeItemTypes = new[] { baseItemKind },
  181. DtoOptions = dtoOptions
  182. }).OfType<T>().FirstOrDefault();
  183. result ??= libraryManager.GetItemList(new InternalItemsQuery
  184. {
  185. Name = name.Replace(BaseItem.SlugChar, '?'),
  186. IncludeItemTypes = new[] { baseItemKind },
  187. DtoOptions = dtoOptions
  188. }).OfType<T>().FirstOrDefault();
  189. return result;
  190. }
  191. }