StudiosController.cs 14 KB

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