MusicGenresController.cs 8.8 KB

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