StudiosController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.Library;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Querying;
  14. using Microsoft.AspNetCore.Authorization;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. namespace Jellyfin.Api.Controllers
  18. {
  19. /// <summary>
  20. /// Studios controller.
  21. /// </summary>
  22. [Authorize(Policy = Policies.DefaultAuthorization)]
  23. public class StudiosController : BaseJellyfinApiController
  24. {
  25. private readonly ILibraryManager _libraryManager;
  26. private readonly IUserManager _userManager;
  27. private readonly IDtoService _dtoService;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="StudiosController"/> class.
  30. /// </summary>
  31. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  32. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  33. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  34. public StudiosController(
  35. ILibraryManager libraryManager,
  36. IUserManager userManager,
  37. IDtoService dtoService)
  38. {
  39. _libraryManager = libraryManager;
  40. _userManager = userManager;
  41. _dtoService = dtoService;
  42. }
  43. /// <summary>
  44. /// Gets all studios from a given item, folder, or the entire library.
  45. /// </summary>
  46. /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>
  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">Optional. 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 based on item type. This allows multiple, comma delimited.</param>
  54. /// <param name="filters">Optional. Specify additional filters to apply. This allows multiple, comma delimited. Options: IsFolder, IsNotFolder, IsUnplayed, IsPlayed, IsFavorite, IsResumable, Likes, Dislikes.</param>
  55. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  56. /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>
  57. /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>
  58. /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>
  59. /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>
  60. /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>
  61. /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>
  62. /// <param name="enableUserData">Optional, include user data.</param>
  63. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  64. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  65. /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>
  66. /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person ids.</param>
  67. /// <param name="personTypes">Optional. If specified, along with Person, results will be filtered to include only those containing the specified person and PersonType. Allows multiple, comma-delimited.</param>
  68. /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>
  69. /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>
  70. /// <param name="userId">User id.</param>
  71. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  72. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  73. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  74. /// <param name="enableImages">Optional, include image information in output.</param>
  75. /// <param name="enableTotalRecordCount">Total record count.</param>
  76. /// <response code="200">Studios returned.</response>
  77. /// <returns>An <see cref="OkResult"/> containing the studios.</returns>
  78. [HttpGet]
  79. [ProducesResponseType(StatusCodes.Status200OK)]
  80. public ActionResult<QueryResult<BaseItemDto>> GetStudios(
  81. [FromQuery] double? minCommunityRating,
  82. [FromQuery] int? startIndex,
  83. [FromQuery] int? limit,
  84. [FromQuery] string? searchTerm,
  85. [FromQuery] string? parentId,
  86. [FromQuery] string? fields,
  87. [FromQuery] string? excludeItemTypes,
  88. [FromQuery] string? includeItemTypes,
  89. [FromQuery] string? filters,
  90. [FromQuery] bool? isFavorite,
  91. [FromQuery] string? mediaTypes,
  92. [FromQuery] string? genres,
  93. [FromQuery] string? genreIds,
  94. [FromQuery] string? officialRatings,
  95. [FromQuery] string? tags,
  96. [FromQuery] string? years,
  97. [FromQuery] bool? enableUserData,
  98. [FromQuery] int? imageTypeLimit,
  99. [FromQuery] ImageType[] enableImageTypes,
  100. [FromQuery] string? person,
  101. [FromQuery] string? personIds,
  102. [FromQuery] string? personTypes,
  103. [FromQuery] string? studios,
  104. [FromQuery] string? studioIds,
  105. [FromQuery] Guid? userId,
  106. [FromQuery] string? nameStartsWithOrGreater,
  107. [FromQuery] string? nameStartsWith,
  108. [FromQuery] string? nameLessThan,
  109. [FromQuery] bool? enableImages = true,
  110. [FromQuery] bool enableTotalRecordCount = true)
  111. {
  112. var dtoOptions = new DtoOptions()
  113. .AddItemFields(fields)
  114. .AddClientFields(Request)
  115. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  116. User? user = null;
  117. BaseItem parentItem;
  118. if (userId.HasValue && !userId.Equals(Guid.Empty))
  119. {
  120. user = _userManager.GetUserById(userId.Value);
  121. parentItem = string.IsNullOrEmpty(parentId) ? _libraryManager.GetUserRootFolder() : _libraryManager.GetItemById(parentId);
  122. }
  123. else
  124. {
  125. parentItem = string.IsNullOrEmpty(parentId) ? _libraryManager.RootFolder : _libraryManager.GetItemById(parentId);
  126. }
  127. var excludeItemTypesArr = RequestHelpers.Split(excludeItemTypes, ',', true);
  128. var includeItemTypesArr = RequestHelpers.Split(includeItemTypes, ',', true);
  129. var mediaTypesArr = RequestHelpers.Split(mediaTypes, ',', true);
  130. var query = new InternalItemsQuery(user)
  131. {
  132. ExcludeItemTypes = excludeItemTypesArr,
  133. IncludeItemTypes = includeItemTypesArr,
  134. MediaTypes = mediaTypesArr,
  135. StartIndex = startIndex,
  136. Limit = limit,
  137. IsFavorite = isFavorite,
  138. NameLessThan = nameLessThan,
  139. NameStartsWith = nameStartsWith,
  140. NameStartsWithOrGreater = nameStartsWithOrGreater,
  141. Tags = RequestHelpers.Split(tags, ',', true),
  142. OfficialRatings = RequestHelpers.Split(officialRatings, ',', true),
  143. Genres = RequestHelpers.Split(genres, ',', true),
  144. GenreIds = RequestHelpers.GetGuids(genreIds),
  145. StudioIds = RequestHelpers.GetGuids(studioIds),
  146. Person = person,
  147. PersonIds = RequestHelpers.GetGuids(personIds),
  148. PersonTypes = RequestHelpers.Split(personTypes, ',', true),
  149. Years = RequestHelpers.Split(years, ',', true).Select(int.Parse).ToArray(),
  150. MinCommunityRating = minCommunityRating,
  151. DtoOptions = dtoOptions,
  152. SearchTerm = searchTerm,
  153. EnableTotalRecordCount = enableTotalRecordCount
  154. };
  155. if (!string.IsNullOrWhiteSpace(parentId))
  156. {
  157. if (parentItem is Folder)
  158. {
  159. query.AncestorIds = new[] { new Guid(parentId) };
  160. }
  161. else
  162. {
  163. query.ItemIds = new[] { new Guid(parentId) };
  164. }
  165. }
  166. // Studios
  167. if (!string.IsNullOrEmpty(studios))
  168. {
  169. query.StudioIds = studios.Split('|').Select(i =>
  170. {
  171. try
  172. {
  173. return _libraryManager.GetStudio(i);
  174. }
  175. catch
  176. {
  177. return null;
  178. }
  179. }).Where(i => i != null).Select(i => i!.Id)
  180. .ToArray();
  181. }
  182. foreach (var filter in RequestHelpers.GetFilters(filters))
  183. {
  184. switch (filter)
  185. {
  186. case ItemFilter.Dislikes:
  187. query.IsLiked = false;
  188. break;
  189. case ItemFilter.IsFavorite:
  190. query.IsFavorite = true;
  191. break;
  192. case ItemFilter.IsFavoriteOrLikes:
  193. query.IsFavoriteOrLiked = true;
  194. break;
  195. case ItemFilter.IsFolder:
  196. query.IsFolder = true;
  197. break;
  198. case ItemFilter.IsNotFolder:
  199. query.IsFolder = false;
  200. break;
  201. case ItemFilter.IsPlayed:
  202. query.IsPlayed = true;
  203. break;
  204. case ItemFilter.IsResumable:
  205. query.IsResumable = true;
  206. break;
  207. case ItemFilter.IsUnplayed:
  208. query.IsPlayed = false;
  209. break;
  210. case ItemFilter.Likes:
  211. query.IsLiked = true;
  212. break;
  213. }
  214. }
  215. var result = new QueryResult<(BaseItem, ItemCounts)>();
  216. var dtos = result.Items.Select(i =>
  217. {
  218. var (baseItem, itemCounts) = i;
  219. var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  220. if (!string.IsNullOrWhiteSpace(includeItemTypes))
  221. {
  222. dto.ChildCount = itemCounts.ItemCount;
  223. dto.ProgramCount = itemCounts.ProgramCount;
  224. dto.SeriesCount = itemCounts.SeriesCount;
  225. dto.EpisodeCount = itemCounts.EpisodeCount;
  226. dto.MovieCount = itemCounts.MovieCount;
  227. dto.TrailerCount = itemCounts.TrailerCount;
  228. dto.AlbumCount = itemCounts.AlbumCount;
  229. dto.SongCount = itemCounts.SongCount;
  230. dto.ArtistCount = itemCounts.ArtistCount;
  231. }
  232. return dto;
  233. });
  234. return new QueryResult<BaseItemDto>
  235. {
  236. Items = dtos.ToArray(),
  237. TotalRecordCount = result.TotalRecordCount
  238. };
  239. }
  240. /// <summary>
  241. /// Gets a studio by name.
  242. /// </summary>
  243. /// <param name="name">Studio name.</param>
  244. /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
  245. /// <response code="200">Studio returned.</response>
  246. /// <returns>An <see cref="OkResult"/> containing the studio.</returns>
  247. [HttpGet("{name}")]
  248. [ProducesResponseType(StatusCodes.Status200OK)]
  249. public ActionResult<BaseItemDto> GetStudio([FromRoute, Required] string name, [FromQuery] Guid? userId)
  250. {
  251. var dtoOptions = new DtoOptions().AddClientFields(Request);
  252. var item = _libraryManager.GetStudio(name);
  253. if (userId.HasValue && !userId.Equals(Guid.Empty))
  254. {
  255. var user = _userManager.GetUserById(userId.Value);
  256. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  257. }
  258. return _dtoService.GetBaseItemDto(item, dtoOptions);
  259. }
  260. }
  261. }