MusicGenresController.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Data.Entities;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Entities.Audio;
  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. /// The music genres controller.
  22. /// </summary>
  23. [Authorize(Policy = Policies.DefaultAuthorization)]
  24. public class MusicGenresController : BaseJellyfinApiController
  25. {
  26. private readonly ILibraryManager _libraryManager;
  27. private readonly IDtoService _dtoService;
  28. private readonly IUserManager _userManager;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="MusicGenresController"/> class.
  31. /// </summary>
  32. /// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param>
  33. /// <param name="userManager">Instance of <see cref="IUserManager"/> interface.</param>
  34. /// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
  35. public MusicGenresController(
  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 music 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. This allows multiple, comma delimited. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines.</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="enableImages">Optional, include image information in output.</param>
  62. /// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
  63. /// <response code="200">Music genres returned.</response>
  64. /// <returns>An <see cref="OkResult"/> containing the queryresult of music genres.</returns>
  65. [HttpGet]
  66. public ActionResult<QueryResult<BaseItemDto>> GetMusicGenres(
  67. [FromQuery] int? startIndex,
  68. [FromQuery] int? limit,
  69. [FromQuery] string? searchTerm,
  70. [FromQuery] string? parentId,
  71. [FromQuery] string? fields,
  72. [FromQuery] string? excludeItemTypes,
  73. [FromQuery] string? includeItemTypes,
  74. [FromQuery] bool? isFavorite,
  75. [FromQuery] int? imageTypeLimit,
  76. [FromQuery] ImageType[] enableImageTypes,
  77. [FromQuery] Guid? userId,
  78. [FromQuery] string? nameStartsWithOrGreater,
  79. [FromQuery] string? nameStartsWith,
  80. [FromQuery] string? nameLessThan,
  81. [FromQuery] bool? enableImages = true,
  82. [FromQuery] bool enableTotalRecordCount = true)
  83. {
  84. var dtoOptions = new DtoOptions()
  85. .AddItemFields(fields)
  86. .AddClientFields(Request)
  87. .AddAdditionalDtoOptions(enableImages, false, imageTypeLimit, enableImageTypes);
  88. User? user = userId.HasValue && userId != Guid.Empty ? _userManager.GetUserById(userId.Value) : null;
  89. var parentItem = _libraryManager.GetParentItem(parentId, userId);
  90. var query = new InternalItemsQuery(user)
  91. {
  92. ExcludeItemTypes = RequestHelpers.Split(excludeItemTypes, ',', true),
  93. IncludeItemTypes = RequestHelpers.Split(includeItemTypes, ',', true),
  94. StartIndex = startIndex,
  95. Limit = limit,
  96. IsFavorite = isFavorite,
  97. NameLessThan = nameLessThan,
  98. NameStartsWith = nameStartsWith,
  99. NameStartsWithOrGreater = nameStartsWithOrGreater,
  100. DtoOptions = dtoOptions,
  101. SearchTerm = searchTerm,
  102. EnableTotalRecordCount = enableTotalRecordCount
  103. };
  104. if (!string.IsNullOrWhiteSpace(parentId))
  105. {
  106. if (parentItem is Folder)
  107. {
  108. query.AncestorIds = new[] { new Guid(parentId) };
  109. }
  110. else
  111. {
  112. query.ItemIds = new[] { new Guid(parentId) };
  113. }
  114. }
  115. var result = _libraryManager.GetMusicGenres(query);
  116. var shouldIncludeItemTypes = !string.IsNullOrWhiteSpace(includeItemTypes);
  117. return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
  118. }
  119. /// <summary>
  120. /// Gets a music genre, by name.
  121. /// </summary>
  122. /// <param name="genreName">The genre name.</param>
  123. /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
  124. /// <returns>An <see cref="OkResult"/> containing a <see cref="BaseItemDto"/> with the music genre.</returns>
  125. [HttpGet("{genreName}")]
  126. [ProducesResponseType(StatusCodes.Status200OK)]
  127. public ActionResult<BaseItemDto> GetMusicGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
  128. {
  129. var dtoOptions = new DtoOptions().AddClientFields(Request);
  130. MusicGenre item;
  131. if (genreName.IndexOf(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase) != -1)
  132. {
  133. item = GetItemFromSlugName<MusicGenre>(_libraryManager, genreName, dtoOptions);
  134. }
  135. else
  136. {
  137. item = _libraryManager.GetMusicGenre(genreName);
  138. }
  139. if (userId.HasValue && !userId.Equals(Guid.Empty))
  140. {
  141. var user = _userManager.GetUserById(userId.Value);
  142. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  143. }
  144. return _dtoService.GetBaseItemDto(item, dtoOptions);
  145. }
  146. private T GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions)
  147. where T : BaseItem, new()
  148. {
  149. var result = libraryManager.GetItemList(new InternalItemsQuery
  150. {
  151. Name = name.Replace(BaseItem.SlugChar, '&'),
  152. IncludeItemTypes = new[] { typeof(T).Name },
  153. DtoOptions = dtoOptions
  154. }).OfType<T>().FirstOrDefault();
  155. result ??= libraryManager.GetItemList(new InternalItemsQuery
  156. {
  157. Name = name.Replace(BaseItem.SlugChar, '/'),
  158. IncludeItemTypes = new[] { typeof(T).Name },
  159. DtoOptions = dtoOptions
  160. }).OfType<T>().FirstOrDefault();
  161. result ??= libraryManager.GetItemList(new InternalItemsQuery
  162. {
  163. Name = name.Replace(BaseItem.SlugChar, '?'),
  164. IncludeItemTypes = new[] { typeof(T).Name },
  165. DtoOptions = dtoOptions
  166. }).OfType<T>().FirstOrDefault();
  167. return result;
  168. }
  169. }
  170. }