using System;
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
using Jellyfin.Data.Entities;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
///
/// The instant mix controller.
///
[Authorize(Policy = Policies.DefaultAuthorization)]
public class InstantMixController : BaseJellyfinApiController
{
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
private readonly ILibraryManager _libraryManager;
private readonly IMusicManager _musicManager;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
/// Instance of the interface.
/// Instance of the interface.
/// Instance of the interface.
public InstantMixController(
IUserManager userManager,
IDtoService dtoService,
IMusicManager musicManager,
ILibraryManager libraryManager)
{
_userManager = userManager;
_dtoService = dtoService;
_musicManager = musicManager;
_libraryManager = libraryManager;
}
///
/// Creates an instant playlist based on a given song.
///
/// The item id.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/Songs/{id}/InstantMix")]
public ActionResult> GetInstantMixFromSong(
[FromRoute] Guid id,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var item = _libraryManager.GetItemById(id);
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
///
/// Creates an instant playlist based on a given song.
///
/// The item id.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/Albums/{id}/InstantMix")]
public ActionResult> GetInstantMixFromAlbum(
[FromRoute] Guid id,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var album = _libraryManager.GetItemById(id);
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromItem(album, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
///
/// Creates an instant playlist based on a given song.
///
/// The item id.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/Playlists/{id}/InstantMix")]
public ActionResult> GetInstantMixFromPlaylist(
[FromRoute] Guid id,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var playlist = (Playlist)_libraryManager.GetItemById(id);
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromItem(playlist, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
///
/// Creates an instant playlist based on a given song.
///
/// The genre name.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/MusicGenres/{name}/InstantMix")]
public ActionResult> GetInstantMixFromMusicGenre(
[FromRoute] string name,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromGenres(new[] { name }, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
///
/// Creates an instant playlist based on a given song.
///
/// The item id.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/Artists/InstantMix")]
public ActionResult> GetInstantMixFromArtists(
[FromRoute] Guid id,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var item = _libraryManager.GetItemById(id);
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
///
/// Creates an instant playlist based on a given song.
///
/// The item id.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/MusicGenres/InstantMix")]
public ActionResult> GetInstantMixFromMusicGenres(
[FromRoute] Guid id,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var item = _libraryManager.GetItemById(id);
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
///
/// Creates an instant playlist based on a given song.
///
/// The item id.
/// Optional. Filter by user id, and attach user data.
/// Optional. The maximum number of records to return.
/// Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines, TrailerUrls.
/// Optional. Include image information in output.
/// Optional. Include user data.
/// Optional. The max number of images to return, per image type.
/// Optional. The image types to include in the output.
/// A with the playlist items.
[HttpGet("/Items/{id}/InstantMix")]
public ActionResult> GetInstantMixFromItem(
[FromRoute] Guid id,
[FromQuery] Guid userId,
[FromQuery] int? limit,
[FromQuery] string fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery] string? enableImageTypes)
{
var item = _libraryManager.GetItemById(id);
var user = _userManager.GetUserById(userId);
var dtoOptions = new DtoOptions()
.AddItemFields(fields)
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes!);
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
private QueryResult GetResult(List items, User user, int? limit, DtoOptions dtoOptions)
{
var list = items;
var result = new QueryResult
{
TotalRecordCount = list.Count
};
if (limit.HasValue)
{
list = list.Take(limit.Value).ToList();
}
var returnList = _dtoService.GetBaseItemDtos(list, dtoOptions, user);
result.Items = returnList;
return result;
}
}
}