ArtistsController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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.Api.ModelBinders;
  8. using Jellyfin.Data.Entities;
  9. using Jellyfin.Data.Enums;
  10. using MediaBrowser.Controller.Dto;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.Querying;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Mvc;
  19. namespace Jellyfin.Api.Controllers
  20. {
  21. /// <summary>
  22. /// The artists controller.
  23. /// </summary>
  24. [Route("Artists")]
  25. [Authorize(Policy = Policies.DefaultAuthorization)]
  26. public class ArtistsController : BaseJellyfinApiController
  27. {
  28. private readonly ILibraryManager _libraryManager;
  29. private readonly IUserManager _userManager;
  30. private readonly IDtoService _dtoService;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="ArtistsController"/> class.
  33. /// </summary>
  34. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  35. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  36. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  37. public ArtistsController(
  38. ILibraryManager libraryManager,
  39. IUserManager userManager,
  40. IDtoService dtoService)
  41. {
  42. _libraryManager = libraryManager;
  43. _userManager = userManager;
  44. _dtoService = dtoService;
  45. }
  46. /// <summary>
  47. /// Gets all artists from a given item, folder, or the entire library.
  48. /// </summary>
  49. /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>
  50. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  51. /// <param name="limit">Optional. The maximum number of records to return.</param>
  52. /// <param name="searchTerm">Optional. Search term.</param>
  53. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  54. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  55. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  56. /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>
  57. /// <param name="filters">Optional. Specify additional filters to apply.</param>
  58. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  59. /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>
  60. /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>
  61. /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>
  62. /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>
  63. /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>
  64. /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>
  65. /// <param name="enableUserData">Optional, include user data.</param>
  66. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  67. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  68. /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>
  69. /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person ids.</param>
  70. /// <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>
  71. /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>
  72. /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>
  73. /// <param name="userId">User id.</param>
  74. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  75. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  76. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  77. /// <param name="enableImages">Optional, include image information in output.</param>
  78. /// <param name="enableTotalRecordCount">Total record count.</param>
  79. /// <response code="200">Artists returned.</response>
  80. /// <returns>An <see cref="OkResult"/> containing the artists.</returns>
  81. [HttpGet]
  82. [ProducesResponseType(StatusCodes.Status200OK)]
  83. public ActionResult<QueryResult<BaseItemDto>> GetArtists(
  84. [FromQuery] double? minCommunityRating,
  85. [FromQuery] int? startIndex,
  86. [FromQuery] int? limit,
  87. [FromQuery] string? searchTerm,
  88. [FromQuery] Guid? parentId,
  89. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  90. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
  91. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
  92. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter[] filters,
  93. [FromQuery] bool? isFavorite,
  94. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,
  95. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] genres,
  96. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] genreIds,
  97. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] officialRatings,
  98. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] tags,
  99. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] int[] years,
  100. [FromQuery] bool? enableUserData,
  101. [FromQuery] int? imageTypeLimit,
  102. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
  103. [FromQuery] string? person,
  104. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] personIds,
  105. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] personTypes,
  106. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] studios,
  107. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] studioIds,
  108. [FromQuery] Guid? userId,
  109. [FromQuery] string? nameStartsWithOrGreater,
  110. [FromQuery] string? nameStartsWith,
  111. [FromQuery] string? nameLessThan,
  112. [FromQuery] bool? enableImages = true,
  113. [FromQuery] bool enableTotalRecordCount = true)
  114. {
  115. var dtoOptions = new DtoOptions { Fields = fields }
  116. .AddClientFields(Request)
  117. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  118. User? user = null;
  119. BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);
  120. if (userId.HasValue && !userId.Equals(Guid.Empty))
  121. {
  122. user = _userManager.GetUserById(userId.Value);
  123. }
  124. var query = new InternalItemsQuery(user)
  125. {
  126. ExcludeItemTypes = RequestHelpers.GetItemTypeStrings(excludeItemTypes),
  127. IncludeItemTypes = RequestHelpers.GetItemTypeStrings(includeItemTypes),
  128. MediaTypes = mediaTypes,
  129. StartIndex = startIndex,
  130. Limit = limit,
  131. IsFavorite = isFavorite,
  132. NameLessThan = nameLessThan,
  133. NameStartsWith = nameStartsWith,
  134. NameStartsWithOrGreater = nameStartsWithOrGreater,
  135. Tags = tags,
  136. OfficialRatings = officialRatings,
  137. Genres = genres,
  138. GenreIds = genreIds,
  139. StudioIds = studioIds,
  140. Person = person,
  141. PersonIds = personIds,
  142. PersonTypes = personTypes,
  143. Years = years,
  144. MinCommunityRating = minCommunityRating,
  145. DtoOptions = dtoOptions,
  146. SearchTerm = searchTerm,
  147. EnableTotalRecordCount = enableTotalRecordCount
  148. };
  149. if (parentId.HasValue)
  150. {
  151. if (parentItem is Folder)
  152. {
  153. query.AncestorIds = new[] { parentId.Value };
  154. }
  155. else
  156. {
  157. query.ItemIds = new[] { parentId.Value };
  158. }
  159. }
  160. // Studios
  161. if (studios.Length != 0)
  162. {
  163. query.StudioIds = studios.Select(i =>
  164. {
  165. try
  166. {
  167. return _libraryManager.GetStudio(i);
  168. }
  169. catch
  170. {
  171. return null;
  172. }
  173. }).Where(i => i != null).Select(i => i!.Id).ToArray();
  174. }
  175. foreach (var filter in filters)
  176. {
  177. switch (filter)
  178. {
  179. case ItemFilter.Dislikes:
  180. query.IsLiked = false;
  181. break;
  182. case ItemFilter.IsFavorite:
  183. query.IsFavorite = true;
  184. break;
  185. case ItemFilter.IsFavoriteOrLikes:
  186. query.IsFavoriteOrLiked = true;
  187. break;
  188. case ItemFilter.IsFolder:
  189. query.IsFolder = true;
  190. break;
  191. case ItemFilter.IsNotFolder:
  192. query.IsFolder = false;
  193. break;
  194. case ItemFilter.IsPlayed:
  195. query.IsPlayed = true;
  196. break;
  197. case ItemFilter.IsResumable:
  198. query.IsResumable = true;
  199. break;
  200. case ItemFilter.IsUnplayed:
  201. query.IsPlayed = false;
  202. break;
  203. case ItemFilter.Likes:
  204. query.IsLiked = true;
  205. break;
  206. }
  207. }
  208. var result = _libraryManager.GetArtists(query);
  209. var dtos = result.Items.Select(i =>
  210. {
  211. var (baseItem, itemCounts) = i;
  212. var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  213. if (includeItemTypes.Length != 0)
  214. {
  215. dto.ChildCount = itemCounts.ItemCount;
  216. dto.ProgramCount = itemCounts.ProgramCount;
  217. dto.SeriesCount = itemCounts.SeriesCount;
  218. dto.EpisodeCount = itemCounts.EpisodeCount;
  219. dto.MovieCount = itemCounts.MovieCount;
  220. dto.TrailerCount = itemCounts.TrailerCount;
  221. dto.AlbumCount = itemCounts.AlbumCount;
  222. dto.SongCount = itemCounts.SongCount;
  223. dto.ArtistCount = itemCounts.ArtistCount;
  224. }
  225. return dto;
  226. });
  227. return new QueryResult<BaseItemDto>
  228. {
  229. Items = dtos.ToArray(),
  230. TotalRecordCount = result.TotalRecordCount
  231. };
  232. }
  233. /// <summary>
  234. /// Gets all album artists from a given item, folder, or the entire library.
  235. /// </summary>
  236. /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>
  237. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  238. /// <param name="limit">Optional. The maximum number of records to return.</param>
  239. /// <param name="searchTerm">Optional. Search term.</param>
  240. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  241. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  242. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  243. /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>
  244. /// <param name="filters">Optional. Specify additional filters to apply.</param>
  245. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  246. /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>
  247. /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>
  248. /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>
  249. /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>
  250. /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>
  251. /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>
  252. /// <param name="enableUserData">Optional, include user data.</param>
  253. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  254. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  255. /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>
  256. /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person ids.</param>
  257. /// <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>
  258. /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>
  259. /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>
  260. /// <param name="userId">User id.</param>
  261. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  262. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  263. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  264. /// <param name="enableImages">Optional, include image information in output.</param>
  265. /// <param name="enableTotalRecordCount">Total record count.</param>
  266. /// <response code="200">Album artists returned.</response>
  267. /// <returns>An <see cref="OkResult"/> containing the album artists.</returns>
  268. [HttpGet("AlbumArtists")]
  269. [ProducesResponseType(StatusCodes.Status200OK)]
  270. public ActionResult<QueryResult<BaseItemDto>> GetAlbumArtists(
  271. [FromQuery] double? minCommunityRating,
  272. [FromQuery] int? startIndex,
  273. [FromQuery] int? limit,
  274. [FromQuery] string? searchTerm,
  275. [FromQuery] Guid? parentId,
  276. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  277. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
  278. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
  279. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter[] filters,
  280. [FromQuery] bool? isFavorite,
  281. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,
  282. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] genres,
  283. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] genreIds,
  284. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] officialRatings,
  285. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] tags,
  286. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] int[] years,
  287. [FromQuery] bool? enableUserData,
  288. [FromQuery] int? imageTypeLimit,
  289. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
  290. [FromQuery] string? person,
  291. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] personIds,
  292. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] personTypes,
  293. [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] studios,
  294. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] studioIds,
  295. [FromQuery] Guid? userId,
  296. [FromQuery] string? nameStartsWithOrGreater,
  297. [FromQuery] string? nameStartsWith,
  298. [FromQuery] string? nameLessThan,
  299. [FromQuery] bool? enableImages = true,
  300. [FromQuery] bool enableTotalRecordCount = true)
  301. {
  302. var dtoOptions = new DtoOptions { Fields = fields }
  303. .AddClientFields(Request)
  304. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  305. User? user = null;
  306. BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);
  307. if (userId.HasValue && !userId.Equals(Guid.Empty))
  308. {
  309. user = _userManager.GetUserById(userId.Value);
  310. }
  311. var query = new InternalItemsQuery(user)
  312. {
  313. ExcludeItemTypes = RequestHelpers.GetItemTypeStrings(excludeItemTypes),
  314. IncludeItemTypes = RequestHelpers.GetItemTypeStrings(includeItemTypes),
  315. MediaTypes = mediaTypes,
  316. StartIndex = startIndex,
  317. Limit = limit,
  318. IsFavorite = isFavorite,
  319. NameLessThan = nameLessThan,
  320. NameStartsWith = nameStartsWith,
  321. NameStartsWithOrGreater = nameStartsWithOrGreater,
  322. Tags = tags,
  323. OfficialRatings = officialRatings,
  324. Genres = genres,
  325. GenreIds = genreIds,
  326. StudioIds = studioIds,
  327. Person = person,
  328. PersonIds = personIds,
  329. PersonTypes = personTypes,
  330. Years = years,
  331. MinCommunityRating = minCommunityRating,
  332. DtoOptions = dtoOptions,
  333. SearchTerm = searchTerm,
  334. EnableTotalRecordCount = enableTotalRecordCount
  335. };
  336. if (parentId.HasValue)
  337. {
  338. if (parentItem is Folder)
  339. {
  340. query.AncestorIds = new[] { parentId.Value };
  341. }
  342. else
  343. {
  344. query.ItemIds = new[] { parentId.Value };
  345. }
  346. }
  347. // Studios
  348. if (studios.Length != 0)
  349. {
  350. query.StudioIds = studios.Select(i =>
  351. {
  352. try
  353. {
  354. return _libraryManager.GetStudio(i);
  355. }
  356. catch
  357. {
  358. return null;
  359. }
  360. }).Where(i => i != null).Select(i => i!.Id).ToArray();
  361. }
  362. foreach (var filter in filters)
  363. {
  364. switch (filter)
  365. {
  366. case ItemFilter.Dislikes:
  367. query.IsLiked = false;
  368. break;
  369. case ItemFilter.IsFavorite:
  370. query.IsFavorite = true;
  371. break;
  372. case ItemFilter.IsFavoriteOrLikes:
  373. query.IsFavoriteOrLiked = true;
  374. break;
  375. case ItemFilter.IsFolder:
  376. query.IsFolder = true;
  377. break;
  378. case ItemFilter.IsNotFolder:
  379. query.IsFolder = false;
  380. break;
  381. case ItemFilter.IsPlayed:
  382. query.IsPlayed = true;
  383. break;
  384. case ItemFilter.IsResumable:
  385. query.IsResumable = true;
  386. break;
  387. case ItemFilter.IsUnplayed:
  388. query.IsPlayed = false;
  389. break;
  390. case ItemFilter.Likes:
  391. query.IsLiked = true;
  392. break;
  393. }
  394. }
  395. var result = _libraryManager.GetAlbumArtists(query);
  396. var dtos = result.Items.Select(i =>
  397. {
  398. var (baseItem, itemCounts) = i;
  399. var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  400. if (includeItemTypes.Length != 0)
  401. {
  402. dto.ChildCount = itemCounts.ItemCount;
  403. dto.ProgramCount = itemCounts.ProgramCount;
  404. dto.SeriesCount = itemCounts.SeriesCount;
  405. dto.EpisodeCount = itemCounts.EpisodeCount;
  406. dto.MovieCount = itemCounts.MovieCount;
  407. dto.TrailerCount = itemCounts.TrailerCount;
  408. dto.AlbumCount = itemCounts.AlbumCount;
  409. dto.SongCount = itemCounts.SongCount;
  410. dto.ArtistCount = itemCounts.ArtistCount;
  411. }
  412. return dto;
  413. });
  414. return new QueryResult<BaseItemDto>
  415. {
  416. Items = dtos.ToArray(),
  417. TotalRecordCount = result.TotalRecordCount
  418. };
  419. }
  420. /// <summary>
  421. /// Gets an artist by name.
  422. /// </summary>
  423. /// <param name="name">Studio name.</param>
  424. /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
  425. /// <response code="200">Artist returned.</response>
  426. /// <returns>An <see cref="OkResult"/> containing the artist.</returns>
  427. [HttpGet("{name}")]
  428. [ProducesResponseType(StatusCodes.Status200OK)]
  429. public ActionResult<BaseItemDto> GetArtistByName([FromRoute, Required] string name, [FromQuery] Guid? userId)
  430. {
  431. var dtoOptions = new DtoOptions().AddClientFields(Request);
  432. var item = _libraryManager.GetArtist(name, dtoOptions);
  433. if (userId.HasValue && !userId.Equals(Guid.Empty))
  434. {
  435. var user = _userManager.GetUserById(userId.Value);
  436. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  437. }
  438. return _dtoService.GetBaseItemDto(item, dtoOptions);
  439. }
  440. }
  441. }