ArtistsController.cs 24 KB

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