ArtistsController.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. /// 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] string? parentId,
  87. [FromQuery] ItemFields[] fields,
  88. [FromQuery] string? excludeItemTypes,
  89. [FromQuery] string? includeItemTypes,
  90. [FromQuery] ItemFilter[] filters,
  91. [FromQuery] bool? isFavorite,
  92. [FromQuery] string? mediaTypes,
  93. [FromQuery] string? genres,
  94. [FromQuery] string? genreIds,
  95. [FromQuery] string? officialRatings,
  96. [FromQuery] string? tags,
  97. [FromQuery] string? years,
  98. [FromQuery] bool? enableUserData,
  99. [FromQuery] int? imageTypeLimit,
  100. [FromQuery] ImageType[] enableImageTypes,
  101. [FromQuery] string? person,
  102. [FromQuery] string? personIds,
  103. [FromQuery] string? personTypes,
  104. [FromQuery] string? studios,
  105. [FromQuery] string? 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;
  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).ToArray();
  180. }
  181. foreach (var filter in filters)
  182. {
  183. switch (filter)
  184. {
  185. case ItemFilter.Dislikes:
  186. query.IsLiked = false;
  187. break;
  188. case ItemFilter.IsFavorite:
  189. query.IsFavorite = true;
  190. break;
  191. case ItemFilter.IsFavoriteOrLikes:
  192. query.IsFavoriteOrLiked = true;
  193. break;
  194. case ItemFilter.IsFolder:
  195. query.IsFolder = true;
  196. break;
  197. case ItemFilter.IsNotFolder:
  198. query.IsFolder = false;
  199. break;
  200. case ItemFilter.IsPlayed:
  201. query.IsPlayed = true;
  202. break;
  203. case ItemFilter.IsResumable:
  204. query.IsResumable = true;
  205. break;
  206. case ItemFilter.IsUnplayed:
  207. query.IsPlayed = false;
  208. break;
  209. case ItemFilter.Likes:
  210. query.IsLiked = true;
  211. break;
  212. }
  213. }
  214. var result = _libraryManager.GetArtists(query);
  215. var dtos = result.Items.Select(i =>
  216. {
  217. var (baseItem, itemCounts) = i;
  218. var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  219. if (!string.IsNullOrWhiteSpace(includeItemTypes))
  220. {
  221. dto.ChildCount = itemCounts.ItemCount;
  222. dto.ProgramCount = itemCounts.ProgramCount;
  223. dto.SeriesCount = itemCounts.SeriesCount;
  224. dto.EpisodeCount = itemCounts.EpisodeCount;
  225. dto.MovieCount = itemCounts.MovieCount;
  226. dto.TrailerCount = itemCounts.TrailerCount;
  227. dto.AlbumCount = itemCounts.AlbumCount;
  228. dto.SongCount = itemCounts.SongCount;
  229. dto.ArtistCount = itemCounts.ArtistCount;
  230. }
  231. return dto;
  232. });
  233. return new QueryResult<BaseItemDto>
  234. {
  235. Items = dtos.ToArray(),
  236. TotalRecordCount = result.TotalRecordCount
  237. };
  238. }
  239. /// <summary>
  240. /// Gets all album artists from a given item, folder, or the entire library.
  241. /// </summary>
  242. /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>
  243. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  244. /// <param name="limit">Optional. The maximum number of records to return.</param>
  245. /// <param name="searchTerm">Optional. Search term.</param>
  246. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  247. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  248. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  249. /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>
  250. /// <param name="filters">Optional. Specify additional filters to apply.</param>
  251. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  252. /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>
  253. /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>
  254. /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>
  255. /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>
  256. /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>
  257. /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>
  258. /// <param name="enableUserData">Optional, include user data.</param>
  259. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  260. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  261. /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>
  262. /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person ids.</param>
  263. /// <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>
  264. /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>
  265. /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>
  266. /// <param name="userId">User id.</param>
  267. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  268. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  269. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  270. /// <param name="enableImages">Optional, include image information in output.</param>
  271. /// <param name="enableTotalRecordCount">Total record count.</param>
  272. /// <response code="200">Album artists returned.</response>
  273. /// <returns>An <see cref="OkResult"/> containing the album artists.</returns>
  274. [HttpGet("AlbumArtists")]
  275. [ProducesResponseType(StatusCodes.Status200OK)]
  276. public ActionResult<QueryResult<BaseItemDto>> GetAlbumArtists(
  277. [FromQuery] double? minCommunityRating,
  278. [FromQuery] int? startIndex,
  279. [FromQuery] int? limit,
  280. [FromQuery] string? searchTerm,
  281. [FromQuery] string? parentId,
  282. [FromQuery] ItemFields[] fields,
  283. [FromQuery] string? excludeItemTypes,
  284. [FromQuery] string? includeItemTypes,
  285. [FromQuery] ItemFilter[] filters,
  286. [FromQuery] bool? isFavorite,
  287. [FromQuery] string? mediaTypes,
  288. [FromQuery] string? genres,
  289. [FromQuery] string? genreIds,
  290. [FromQuery] string? officialRatings,
  291. [FromQuery] string? tags,
  292. [FromQuery] string? years,
  293. [FromQuery] bool? enableUserData,
  294. [FromQuery] int? imageTypeLimit,
  295. [FromQuery] ImageType[] enableImageTypes,
  296. [FromQuery] string? person,
  297. [FromQuery] string? personIds,
  298. [FromQuery] string? personTypes,
  299. [FromQuery] string? studios,
  300. [FromQuery] string? studioIds,
  301. [FromQuery] Guid? userId,
  302. [FromQuery] string? nameStartsWithOrGreater,
  303. [FromQuery] string? nameStartsWith,
  304. [FromQuery] string? nameLessThan,
  305. [FromQuery] bool? enableImages = true,
  306. [FromQuery] bool enableTotalRecordCount = true)
  307. {
  308. var dtoOptions = new DtoOptions{ Fields = fields }
  309. .AddClientFields(Request)
  310. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  311. User? user = null;
  312. BaseItem parentItem;
  313. if (userId.HasValue && !userId.Equals(Guid.Empty))
  314. {
  315. user = _userManager.GetUserById(userId.Value);
  316. parentItem = string.IsNullOrEmpty(parentId) ? _libraryManager.GetUserRootFolder() : _libraryManager.GetItemById(parentId);
  317. }
  318. else
  319. {
  320. parentItem = string.IsNullOrEmpty(parentId) ? _libraryManager.RootFolder : _libraryManager.GetItemById(parentId);
  321. }
  322. var excludeItemTypesArr = RequestHelpers.Split(excludeItemTypes, ',', true);
  323. var includeItemTypesArr = RequestHelpers.Split(includeItemTypes, ',', true);
  324. var mediaTypesArr = RequestHelpers.Split(mediaTypes, ',', true);
  325. var query = new InternalItemsQuery(user)
  326. {
  327. ExcludeItemTypes = excludeItemTypesArr,
  328. IncludeItemTypes = includeItemTypesArr,
  329. MediaTypes = mediaTypesArr,
  330. StartIndex = startIndex,
  331. Limit = limit,
  332. IsFavorite = isFavorite,
  333. NameLessThan = nameLessThan,
  334. NameStartsWith = nameStartsWith,
  335. NameStartsWithOrGreater = nameStartsWithOrGreater,
  336. Tags = RequestHelpers.Split(tags, ',', true),
  337. OfficialRatings = RequestHelpers.Split(officialRatings, ',', true),
  338. Genres = RequestHelpers.Split(genres, ',', true),
  339. GenreIds = RequestHelpers.GetGuids(genreIds),
  340. StudioIds = RequestHelpers.GetGuids(studioIds),
  341. Person = person,
  342. PersonIds = RequestHelpers.GetGuids(personIds),
  343. PersonTypes = RequestHelpers.Split(personTypes, ',', true),
  344. Years = RequestHelpers.Split(years, ',', true).Select(int.Parse).ToArray(),
  345. MinCommunityRating = minCommunityRating,
  346. DtoOptions = dtoOptions,
  347. SearchTerm = searchTerm,
  348. EnableTotalRecordCount = enableTotalRecordCount
  349. };
  350. if (!string.IsNullOrWhiteSpace(parentId))
  351. {
  352. if (parentItem is Folder)
  353. {
  354. query.AncestorIds = new[] { new Guid(parentId) };
  355. }
  356. else
  357. {
  358. query.ItemIds = new[] { new Guid(parentId) };
  359. }
  360. }
  361. // Studios
  362. if (!string.IsNullOrEmpty(studios))
  363. {
  364. query.StudioIds = studios.Split('|').Select(i =>
  365. {
  366. try
  367. {
  368. return _libraryManager.GetStudio(i);
  369. }
  370. catch
  371. {
  372. return null;
  373. }
  374. }).Where(i => i != null).Select(i => i!.Id).ToArray();
  375. }
  376. foreach (var filter in filters)
  377. {
  378. switch (filter)
  379. {
  380. case ItemFilter.Dislikes:
  381. query.IsLiked = false;
  382. break;
  383. case ItemFilter.IsFavorite:
  384. query.IsFavorite = true;
  385. break;
  386. case ItemFilter.IsFavoriteOrLikes:
  387. query.IsFavoriteOrLiked = true;
  388. break;
  389. case ItemFilter.IsFolder:
  390. query.IsFolder = true;
  391. break;
  392. case ItemFilter.IsNotFolder:
  393. query.IsFolder = false;
  394. break;
  395. case ItemFilter.IsPlayed:
  396. query.IsPlayed = true;
  397. break;
  398. case ItemFilter.IsResumable:
  399. query.IsResumable = true;
  400. break;
  401. case ItemFilter.IsUnplayed:
  402. query.IsPlayed = false;
  403. break;
  404. case ItemFilter.Likes:
  405. query.IsLiked = true;
  406. break;
  407. }
  408. }
  409. var result = _libraryManager.GetAlbumArtists(query);
  410. var dtos = result.Items.Select(i =>
  411. {
  412. var (baseItem, itemCounts) = i;
  413. var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  414. if (!string.IsNullOrWhiteSpace(includeItemTypes))
  415. {
  416. dto.ChildCount = itemCounts.ItemCount;
  417. dto.ProgramCount = itemCounts.ProgramCount;
  418. dto.SeriesCount = itemCounts.SeriesCount;
  419. dto.EpisodeCount = itemCounts.EpisodeCount;
  420. dto.MovieCount = itemCounts.MovieCount;
  421. dto.TrailerCount = itemCounts.TrailerCount;
  422. dto.AlbumCount = itemCounts.AlbumCount;
  423. dto.SongCount = itemCounts.SongCount;
  424. dto.ArtistCount = itemCounts.ArtistCount;
  425. }
  426. return dto;
  427. });
  428. return new QueryResult<BaseItemDto>
  429. {
  430. Items = dtos.ToArray(),
  431. TotalRecordCount = result.TotalRecordCount
  432. };
  433. }
  434. /// <summary>
  435. /// Gets an artist by name.
  436. /// </summary>
  437. /// <param name="name">Studio name.</param>
  438. /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
  439. /// <response code="200">Artist returned.</response>
  440. /// <returns>An <see cref="OkResult"/> containing the artist.</returns>
  441. [HttpGet("{name}")]
  442. [ProducesResponseType(StatusCodes.Status200OK)]
  443. public ActionResult<BaseItemDto> GetArtistByName([FromRoute, Required] string name, [FromQuery] Guid? userId)
  444. {
  445. var dtoOptions = new DtoOptions().AddClientFields(Request);
  446. var item = _libraryManager.GetArtist(name, dtoOptions);
  447. if (userId.HasValue && !userId.Equals(Guid.Empty))
  448. {
  449. var user = _userManager.GetUserById(userId.Value);
  450. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  451. }
  452. return _dtoService.GetBaseItemDto(item, dtoOptions);
  453. }
  454. }
  455. }