PersonsController.cs 14 KB

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