| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 | using System;using System.ComponentModel.DataAnnotations;using System.Linq;using Jellyfin.Api.Constants;using Jellyfin.Api.Extensions;using Jellyfin.Api.Helpers;using Jellyfin.Api.ModelBinders;using Jellyfin.Data.Entities;using Jellyfin.Data.Enums;using MediaBrowser.Controller.Dto;using MediaBrowser.Controller.Entities;using MediaBrowser.Controller.Library;using MediaBrowser.Model.Dto;using MediaBrowser.Model.Entities;using MediaBrowser.Model.Querying;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;namespace Jellyfin.Api.Controllers{    /// <summary>    /// The artists controller.    /// </summary>    [Route("Artists")]    [Authorize(Policy = Policies.DefaultAuthorization)]    public class ArtistsController : BaseJellyfinApiController    {        private readonly ILibraryManager _libraryManager;        private readonly IUserManager _userManager;        private readonly IDtoService _dtoService;        /// <summary>        /// Initializes a new instance of the <see cref="ArtistsController"/> class.        /// </summary>        /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>        /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>        /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>        public ArtistsController(            ILibraryManager libraryManager,            IUserManager userManager,            IDtoService dtoService)        {            _libraryManager = libraryManager;            _userManager = userManager;            _dtoService = dtoService;        }        /// <summary>        /// Gets all artists from a given item, folder, or the entire library.        /// </summary>        /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>        /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>        /// <param name="limit">Optional. The maximum number of records to return.</param>        /// <param name="searchTerm">Optional. Search term.</param>        /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>        /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>        /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>        /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>        /// <param name="filters">Optional. Specify additional filters to apply.</param>        /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>        /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>        /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>        /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>        /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>        /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>        /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>        /// <param name="enableUserData">Optional, include user data.</param>        /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>        /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>        /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>        /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person ids.</param>        /// <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>        /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>        /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>        /// <param name="userId">User id.</param>        /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>        /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>        /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>        /// <param name="enableImages">Optional, include image information in output.</param>        /// <param name="enableTotalRecordCount">Total record count.</param>        /// <response code="200">Artists returned.</response>        /// <returns>An <see cref="OkResult"/> containing the artists.</returns>        [HttpGet]        [ProducesResponseType(StatusCodes.Status200OK)]        public ActionResult<QueryResult<BaseItemDto>> GetArtists(            [FromQuery] double? minCommunityRating,            [FromQuery] int? startIndex,            [FromQuery] int? limit,            [FromQuery] string? searchTerm,            [FromQuery] Guid? parentId,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter[] filters,            [FromQuery] bool? isFavorite,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] genres,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] genreIds,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] officialRatings,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] tags,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] int[] years,            [FromQuery] bool? enableUserData,            [FromQuery] int? imageTypeLimit,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,            [FromQuery] string? person,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] personIds,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] personTypes,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] studios,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] studioIds,            [FromQuery] Guid? userId,            [FromQuery] string? nameStartsWithOrGreater,            [FromQuery] string? nameStartsWith,            [FromQuery] string? nameLessThan,            [FromQuery] bool? enableImages = true,            [FromQuery] bool enableTotalRecordCount = true)        {            var dtoOptions = new DtoOptions { Fields = fields }                .AddClientFields(Request)                .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);            User? user = null;            BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);            if (userId.HasValue && !userId.Equals(Guid.Empty))            {                user = _userManager.GetUserById(userId.Value);            }            var query = new InternalItemsQuery(user)            {                ExcludeItemTypes = RequestHelpers.GetItemTypeStrings(excludeItemTypes),                IncludeItemTypes = RequestHelpers.GetItemTypeStrings(includeItemTypes),                MediaTypes = mediaTypes,                StartIndex = startIndex,                Limit = limit,                IsFavorite = isFavorite,                NameLessThan = nameLessThan,                NameStartsWith = nameStartsWith,                NameStartsWithOrGreater = nameStartsWithOrGreater,                Tags = tags,                OfficialRatings = officialRatings,                Genres = genres,                GenreIds = genreIds,                StudioIds = studioIds,                Person = person,                PersonIds = personIds,                PersonTypes = personTypes,                Years = years,                MinCommunityRating = minCommunityRating,                DtoOptions = dtoOptions,                SearchTerm = searchTerm,                EnableTotalRecordCount = enableTotalRecordCount            };            if (parentId.HasValue)            {                if (parentItem is Folder)                {                    query.AncestorIds = new[] { parentId.Value };                }                else                {                    query.ItemIds = new[] { parentId.Value };                }            }            // Studios            if (studios.Length != 0)            {                query.StudioIds = studios.Select(i =>                {                    try                    {                        return _libraryManager.GetStudio(i);                    }                    catch                    {                        return null;                    }                }).Where(i => i != null).Select(i => i!.Id).ToArray();            }            foreach (var filter in filters)            {                switch (filter)                {                    case ItemFilter.Dislikes:                        query.IsLiked = false;                        break;                    case ItemFilter.IsFavorite:                        query.IsFavorite = true;                        break;                    case ItemFilter.IsFavoriteOrLikes:                        query.IsFavoriteOrLiked = true;                        break;                    case ItemFilter.IsFolder:                        query.IsFolder = true;                        break;                    case ItemFilter.IsNotFolder:                        query.IsFolder = false;                        break;                    case ItemFilter.IsPlayed:                        query.IsPlayed = true;                        break;                    case ItemFilter.IsResumable:                        query.IsResumable = true;                        break;                    case ItemFilter.IsUnplayed:                        query.IsPlayed = false;                        break;                    case ItemFilter.Likes:                        query.IsLiked = true;                        break;                }            }            var result = _libraryManager.GetArtists(query);            var dtos = result.Items.Select(i =>            {                var (baseItem, itemCounts) = i;                var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);                if (includeItemTypes.Length != 0)                {                    dto.ChildCount = itemCounts.ItemCount;                    dto.ProgramCount = itemCounts.ProgramCount;                    dto.SeriesCount = itemCounts.SeriesCount;                    dto.EpisodeCount = itemCounts.EpisodeCount;                    dto.MovieCount = itemCounts.MovieCount;                    dto.TrailerCount = itemCounts.TrailerCount;                    dto.AlbumCount = itemCounts.AlbumCount;                    dto.SongCount = itemCounts.SongCount;                    dto.ArtistCount = itemCounts.ArtistCount;                }                return dto;            });            return new QueryResult<BaseItemDto>            {                Items = dtos.ToArray(),                TotalRecordCount = result.TotalRecordCount            };        }        /// <summary>        /// Gets all album artists from a given item, folder, or the entire library.        /// </summary>        /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>        /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>        /// <param name="limit">Optional. The maximum number of records to return.</param>        /// <param name="searchTerm">Optional. Search term.</param>        /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>        /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>        /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>        /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>        /// <param name="filters">Optional. Specify additional filters to apply.</param>        /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>        /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>        /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>        /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>        /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>        /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>        /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>        /// <param name="enableUserData">Optional, include user data.</param>        /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>        /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>        /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>        /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person ids.</param>        /// <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>        /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>        /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>        /// <param name="userId">User id.</param>        /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>        /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>        /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>        /// <param name="enableImages">Optional, include image information in output.</param>        /// <param name="enableTotalRecordCount">Total record count.</param>        /// <response code="200">Album artists returned.</response>        /// <returns>An <see cref="OkResult"/> containing the album artists.</returns>        [HttpGet("AlbumArtists")]        [ProducesResponseType(StatusCodes.Status200OK)]        public ActionResult<QueryResult<BaseItemDto>> GetAlbumArtists(            [FromQuery] double? minCommunityRating,            [FromQuery] int? startIndex,            [FromQuery] int? limit,            [FromQuery] string? searchTerm,            [FromQuery] Guid? parentId,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter[] filters,            [FromQuery] bool? isFavorite,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] genres,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] genreIds,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] officialRatings,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] tags,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] int[] years,            [FromQuery] bool? enableUserData,            [FromQuery] int? imageTypeLimit,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,            [FromQuery] string? person,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] personIds,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] personTypes,            [FromQuery, ModelBinder(typeof(PipeDelimitedArrayModelBinder))] string[] studios,            [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] studioIds,            [FromQuery] Guid? userId,            [FromQuery] string? nameStartsWithOrGreater,            [FromQuery] string? nameStartsWith,            [FromQuery] string? nameLessThan,            [FromQuery] bool? enableImages = true,            [FromQuery] bool enableTotalRecordCount = true)        {            var dtoOptions = new DtoOptions { Fields = fields }                .AddClientFields(Request)                .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);            User? user = null;            BaseItem parentItem = _libraryManager.GetParentItem(parentId, userId);            if (userId.HasValue && !userId.Equals(Guid.Empty))            {                user = _userManager.GetUserById(userId.Value);            }            var query = new InternalItemsQuery(user)            {                ExcludeItemTypes = RequestHelpers.GetItemTypeStrings(excludeItemTypes),                IncludeItemTypes = RequestHelpers.GetItemTypeStrings(includeItemTypes),                MediaTypes = mediaTypes,                StartIndex = startIndex,                Limit = limit,                IsFavorite = isFavorite,                NameLessThan = nameLessThan,                NameStartsWith = nameStartsWith,                NameStartsWithOrGreater = nameStartsWithOrGreater,                Tags = tags,                OfficialRatings = officialRatings,                Genres = genres,                GenreIds = genreIds,                StudioIds = studioIds,                Person = person,                PersonIds = personIds,                PersonTypes = personTypes,                Years = years,                MinCommunityRating = minCommunityRating,                DtoOptions = dtoOptions,                SearchTerm = searchTerm,                EnableTotalRecordCount = enableTotalRecordCount            };            if (parentId.HasValue)            {                if (parentItem is Folder)                {                    query.AncestorIds = new[] { parentId.Value };                }                else                {                    query.ItemIds = new[] { parentId.Value };                }            }            // Studios            if (studios.Length != 0)            {                query.StudioIds = studios.Select(i =>                {                    try                    {                        return _libraryManager.GetStudio(i);                    }                    catch                    {                        return null;                    }                }).Where(i => i != null).Select(i => i!.Id).ToArray();            }            foreach (var filter in filters)            {                switch (filter)                {                    case ItemFilter.Dislikes:                        query.IsLiked = false;                        break;                    case ItemFilter.IsFavorite:                        query.IsFavorite = true;                        break;                    case ItemFilter.IsFavoriteOrLikes:                        query.IsFavoriteOrLiked = true;                        break;                    case ItemFilter.IsFolder:                        query.IsFolder = true;                        break;                    case ItemFilter.IsNotFolder:                        query.IsFolder = false;                        break;                    case ItemFilter.IsPlayed:                        query.IsPlayed = true;                        break;                    case ItemFilter.IsResumable:                        query.IsResumable = true;                        break;                    case ItemFilter.IsUnplayed:                        query.IsPlayed = false;                        break;                    case ItemFilter.Likes:                        query.IsLiked = true;                        break;                }            }            var result = _libraryManager.GetAlbumArtists(query);            var dtos = result.Items.Select(i =>            {                var (baseItem, itemCounts) = i;                var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);                if (includeItemTypes.Length != 0)                {                    dto.ChildCount = itemCounts.ItemCount;                    dto.ProgramCount = itemCounts.ProgramCount;                    dto.SeriesCount = itemCounts.SeriesCount;                    dto.EpisodeCount = itemCounts.EpisodeCount;                    dto.MovieCount = itemCounts.MovieCount;                    dto.TrailerCount = itemCounts.TrailerCount;                    dto.AlbumCount = itemCounts.AlbumCount;                    dto.SongCount = itemCounts.SongCount;                    dto.ArtistCount = itemCounts.ArtistCount;                }                return dto;            });            return new QueryResult<BaseItemDto>            {                Items = dtos.ToArray(),                TotalRecordCount = result.TotalRecordCount            };        }        /// <summary>        /// Gets an artist by name.        /// </summary>        /// <param name="name">Studio name.</param>        /// <param name="userId">Optional. Filter by user id, and attach user data.</param>        /// <response code="200">Artist returned.</response>        /// <returns>An <see cref="OkResult"/> containing the artist.</returns>        [HttpGet("{name}")]        [ProducesResponseType(StatusCodes.Status200OK)]        public ActionResult<BaseItemDto> GetArtistByName([FromRoute, Required] string name, [FromQuery] Guid? userId)        {            var dtoOptions = new DtoOptions().AddClientFields(Request);            var item = _libraryManager.GetArtist(name, dtoOptions);            if (userId.HasValue && !userId.Equals(Guid.Empty))            {                var user = _userManager.GetUserById(userId.Value);                return _dtoService.GetBaseItemDto(item, dtoOptions, user);            }            return _dtoService.GetBaseItemDto(item, dtoOptions);        }    }}
 |