2020-11-21 06:26:03 -07:00
using System ;
2020-09-05 16:11:44 -07:00
using System.ComponentModel.DataAnnotations ;
2020-07-04 09:50:16 -07:00
using System.Linq ;
2020-07-06 08:36:24 -07:00
using Jellyfin.Api.Constants ;
2020-07-04 09:50:16 -07:00
using Jellyfin.Api.Extensions ;
using Jellyfin.Api.Helpers ;
2020-11-09 14:53:23 -07:00
using Jellyfin.Api.ModelBinders ;
2020-07-04 09:50:16 -07:00
using Jellyfin.Data.Entities ;
2021-02-10 17:09:23 -07:00
using Jellyfin.Data.Enums ;
2020-07-04 09:50:16 -07:00
using MediaBrowser.Controller.Dto ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Model.Dto ;
2020-10-09 16:52:39 -07:00
using MediaBrowser.Model.Entities ;
2020-07-04 09:50:16 -07:00
using MediaBrowser.Model.Querying ;
2020-07-06 08:43:34 -07:00
using Microsoft.AspNetCore.Authorization ;
2020-07-04 09:50:16 -07:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
using Genre = MediaBrowser . Controller . Entities . Genre ;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// The genres controller.
/// </summary>
2020-07-06 08:36:24 -07:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-07-04 09:50:16 -07:00
public class GenresController : BaseJellyfinApiController
{
private readonly IUserManager _userManager ;
private readonly ILibraryManager _libraryManager ;
private readonly IDtoService _dtoService ;
/// <summary>
/// Initializes a new instance of the <see cref="GenresController"/> class.
/// </summary>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
public GenresController (
IUserManager userManager ,
ILibraryManager libraryManager ,
IDtoService dtoService )
{
_userManager = userManager ;
_libraryManager = libraryManager ;
_dtoService = dtoService ;
}
/// <summary>
/// Gets all genres from a given item, folder, or the entire library.
/// </summary>
/// <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">The search term.</param>
/// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
2020-10-09 16:35:08 -07:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-07-04 09:50:16 -07:00
/// <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 in based on item type. This allows multiple, comma delimited.</param>
/// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</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="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>
2021-04-28 17:33:30 -07:00
/// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited.</param>
/// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
2020-07-04 09:50:16 -07:00
/// <param name="enableImages">Optional, include image information in output.</param>
/// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
/// <response code="200">Genres returned.</response>
/// <returns>An <see cref="OkResult"/> containing the queryresult of genres.</returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult < QueryResult < BaseItemDto > > GetGenres (
[FromQuery] int? startIndex ,
[FromQuery] int? limit ,
2020-07-07 08:10:51 -07:00
[FromQuery] string? searchTerm ,
2020-12-01 11:07:41 -07:00
[FromQuery] Guid ? parentId ,
2020-11-09 14:59:04 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields [ ] fields ,
2021-02-10 17:09:23 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind [ ] excludeItemTypes ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind [ ] includeItemTypes ,
2020-07-04 09:50:16 -07:00
[FromQuery] bool? isFavorite ,
[FromQuery] int? imageTypeLimit ,
2020-11-09 14:53:23 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType [ ] enableImageTypes ,
2020-07-07 08:10:51 -07:00
[FromQuery] Guid ? userId ,
[FromQuery] string? nameStartsWithOrGreater ,
[FromQuery] string? nameStartsWith ,
[FromQuery] string? nameLessThan ,
2021-04-28 17:33:30 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string [ ] sortBy ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder [ ] sortOrder ,
2020-07-04 09:50:16 -07:00
[FromQuery] bool? enableImages = true ,
[FromQuery] bool enableTotalRecordCount = true )
{
2020-10-29 10:36:45 -07:00
var dtoOptions = new DtoOptions { Fields = fields }
2022-10-06 04:57:47 -07:00
. AddClientFields ( User )
2020-11-05 04:27:22 -07:00
. AddAdditionalDtoOptions ( enableImages , false , imageTypeLimit , enableImageTypes ) ;
2020-07-04 09:50:16 -07:00
2022-02-21 06:15:09 -07:00
User ? user = userId is null | | userId . Value . Equals ( default )
? null
: _userManager . GetUserById ( userId . Value ) ;
2020-07-04 09:50:16 -07:00
2020-11-05 04:27:22 -07:00
var parentItem = _libraryManager . GetParentItem ( parentId , userId ) ;
2020-07-04 09:50:16 -07:00
var query = new InternalItemsQuery ( user )
{
2021-12-11 19:31:30 -07:00
ExcludeItemTypes = excludeItemTypes ,
IncludeItemTypes = includeItemTypes ,
2020-07-04 09:50:16 -07:00
StartIndex = startIndex ,
Limit = limit ,
IsFavorite = isFavorite ,
NameLessThan = nameLessThan ,
NameStartsWith = nameStartsWith ,
NameStartsWithOrGreater = nameStartsWithOrGreater ,
DtoOptions = dtoOptions ,
SearchTerm = searchTerm ,
2021-04-28 17:33:30 -07:00
EnableTotalRecordCount = enableTotalRecordCount ,
OrderBy = RequestHelpers . GetOrderBy ( sortBy , sortOrder )
2020-07-04 09:50:16 -07:00
} ;
2020-12-01 11:07:41 -07:00
if ( parentId . HasValue )
2020-07-04 09:50:16 -07:00
{
if ( parentItem is Folder )
{
2020-12-01 11:07:41 -07:00
query . AncestorIds = new [ ] { parentId . Value } ;
2020-07-04 09:50:16 -07:00
}
else
{
2020-12-01 11:07:41 -07:00
query . ItemIds = new [ ] { parentId . Value } ;
2020-07-04 09:50:16 -07:00
}
}
2020-11-05 04:27:22 -07:00
QueryResult < ( BaseItem , ItemCounts ) > result ;
if ( parentItem is ICollectionFolder parentCollectionFolder
2020-11-05 04:53:23 -07:00
& & ( string . Equals ( parentCollectionFolder . CollectionType , CollectionType . Music , StringComparison . Ordinal )
| | string . Equals ( parentCollectionFolder . CollectionType , CollectionType . MusicVideos , StringComparison . Ordinal ) ) )
2020-07-04 09:50:16 -07:00
{
2020-11-05 04:27:22 -07:00
result = _libraryManager . GetMusicGenres ( query ) ;
2020-07-04 09:50:16 -07:00
}
2020-11-05 04:27:22 -07:00
else
2020-07-04 09:50:16 -07:00
{
2020-11-05 04:27:22 -07:00
result = _libraryManager . GetGenres ( query ) ;
2020-07-04 09:50:16 -07:00
}
2020-11-16 20:29:46 -07:00
var shouldIncludeItemTypes = includeItemTypes . Length ! = 0 ;
2020-11-05 04:27:22 -07:00
return RequestHelpers . CreateQueryResult ( result , dtoOptions , _dtoService , shouldIncludeItemTypes , user ) ;
2020-07-04 09:50:16 -07:00
}
/// <summary>
/// Gets a genre, by name.
/// </summary>
/// <param name="genreName">The genre name.</param>
/// <param name="userId">The user id.</param>
/// <response code="200">Genres returned.</response>
/// <returns>An <see cref="OkResult"/> containing the genre.</returns>
[HttpGet("{genreName}")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-09-06 08:07:27 -07:00
public ActionResult < BaseItemDto > GetGenre ( [ FromRoute , Required ] string genreName , [ FromQuery ] Guid ? userId )
2020-07-04 09:50:16 -07:00
{
var dtoOptions = new DtoOptions ( )
2022-10-06 04:57:47 -07:00
. AddClientFields ( User ) ;
2020-07-04 09:50:16 -07:00
2022-02-21 06:15:09 -07:00
Genre ? item ;
if ( genreName . Contains ( BaseItem . SlugChar , StringComparison . OrdinalIgnoreCase ) )
2020-07-04 09:50:16 -07:00
{
2022-02-21 06:15:09 -07:00
item = GetItemFromSlugName < Genre > ( _libraryManager , genreName , dtoOptions , BaseItemKind . Genre ) ;
2020-07-04 09:50:16 -07:00
}
else
{
item = _libraryManager . GetGenre ( genreName ) ;
}
2022-02-21 06:15:09 -07:00
item ? ? = new Genre ( ) ;
2020-07-04 09:50:16 -07:00
2022-02-21 06:15:09 -07:00
if ( userId is null | | userId . Value . Equals ( default ) )
{
return _dtoService . GetBaseItemDto ( item , dtoOptions ) ;
2020-07-04 09:50:16 -07:00
}
2022-02-21 06:15:09 -07:00
var user = _userManager . GetUserById ( userId . Value ) ;
return _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2020-07-04 09:50:16 -07:00
}
2021-12-11 19:31:30 -07:00
private T ? GetItemFromSlugName < T > ( ILibraryManager libraryManager , string name , DtoOptions dtoOptions , BaseItemKind baseItemKind )
2020-07-04 09:50:16 -07:00
where T : BaseItem , new ( )
{
var result = libraryManager . GetItemList ( new InternalItemsQuery
{
Name = name . Replace ( BaseItem . SlugChar , '&' ) ,
2021-12-11 19:31:30 -07:00
IncludeItemTypes = new [ ] { baseItemKind } ,
2020-07-04 09:50:16 -07:00
DtoOptions = dtoOptions
} ) . OfType < T > ( ) . FirstOrDefault ( ) ;
result ? ? = libraryManager . GetItemList ( new InternalItemsQuery
{
Name = name . Replace ( BaseItem . SlugChar , '/' ) ,
2021-12-11 19:31:30 -07:00
IncludeItemTypes = new [ ] { baseItemKind } ,
2020-07-04 09:50:16 -07:00
DtoOptions = dtoOptions
} ) . OfType < T > ( ) . FirstOrDefault ( ) ;
result ? ? = libraryManager . GetItemList ( new InternalItemsQuery
{
Name = name . Replace ( BaseItem . SlugChar , '?' ) ,
2021-12-11 19:31:30 -07:00
IncludeItemTypes = new [ ] { baseItemKind } ,
2020-07-04 09:50:16 -07:00
DtoOptions = dtoOptions
} ) . OfType < T > ( ) . FirstOrDefault ( ) ;
return result ;
}
}
}