2020-11-21 13:26:03 +00:00
|
|
|
using System;
|
2020-12-10 18:36:31 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-06 14:17:45 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-06-08 19:14:41 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2021-01-20 23:24:15 +00:00
|
|
|
using Jellyfin.Api.Attributes;
|
2020-06-22 13:44:11 +00:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-06-20 22:03:19 +00:00
|
|
|
using Jellyfin.Api.Extensions;
|
2020-11-09 21:59:04 +00:00
|
|
|
using Jellyfin.Api.ModelBinders;
|
2020-06-08 19:14:41 +00:00
|
|
|
using Jellyfin.Api.Models.PlaylistDtos;
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
|
|
|
using MediaBrowser.Model.Dto;
|
2020-10-09 23:52:39 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2020-06-08 19:14:41 +00:00
|
|
|
using MediaBrowser.Model.Playlists;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-12-10 18:36:31 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2020-06-08 19:14:41 +00:00
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Playlists controller.
|
|
|
|
/// </summary>
|
2020-06-22 13:44:11 +00:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-06-08 19:14:41 +00:00
|
|
|
public class PlaylistsController : BaseJellyfinApiController
|
|
|
|
{
|
|
|
|
private readonly IPlaylistManager _playlistManager;
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="PlaylistsController"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
|
|
|
|
/// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param>
|
|
|
|
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
|
|
|
|
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
|
|
|
public PlaylistsController(
|
|
|
|
IDtoService dtoService,
|
|
|
|
IPlaylistManager playlistManager,
|
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager)
|
|
|
|
{
|
|
|
|
_dtoService = dtoService;
|
|
|
|
_playlistManager = playlistManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a new playlist.
|
|
|
|
/// </summary>
|
2020-12-10 18:36:31 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// For backwards compatibility parameters can be sent via Query or Body, with Query having higher precedence.
|
2021-01-20 23:24:15 +00:00
|
|
|
/// Query parameters are obsolete.
|
2020-12-10 18:36:31 +00:00
|
|
|
/// </remarks>
|
|
|
|
/// <param name="name">The playlist name.</param>
|
|
|
|
/// <param name="ids">The item ids.</param>
|
|
|
|
/// <param name="userId">The user id.</param>
|
|
|
|
/// <param name="mediaType">The media type.</param>
|
2020-06-08 19:14:41 +00:00
|
|
|
/// <param name="createPlaylistRequest">The create playlist payload.</param>
|
|
|
|
/// <returns>
|
|
|
|
/// A <see cref="Task" /> that represents the asynchronous operation to create a playlist.
|
|
|
|
/// The task result contains an <see cref="OkResult"/> indicating success.
|
|
|
|
/// </returns>
|
|
|
|
[HttpPost]
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public async Task<ActionResult<PlaylistCreationResult>> CreatePlaylist(
|
2021-01-20 23:24:15 +00:00
|
|
|
[FromQuery, ParameterObsolete] string? name,
|
|
|
|
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder)), ParameterObsolete] IReadOnlyList<Guid> ids,
|
|
|
|
[FromQuery, ParameterObsolete] Guid? userId,
|
|
|
|
[FromQuery, ParameterObsolete] string? mediaType,
|
2020-12-10 18:36:31 +00:00
|
|
|
[FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] CreatePlaylistDto? createPlaylistRequest)
|
2020-06-08 19:14:41 +00:00
|
|
|
{
|
2020-12-10 18:36:31 +00:00
|
|
|
if (ids.Count == 0)
|
|
|
|
{
|
|
|
|
ids = createPlaylistRequest?.Ids ?? Array.Empty<Guid>();
|
|
|
|
}
|
|
|
|
|
2020-06-08 19:14:41 +00:00
|
|
|
var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest
|
|
|
|
{
|
2020-12-10 18:36:31 +00:00
|
|
|
Name = name ?? createPlaylistRequest?.Name,
|
|
|
|
ItemIdList = ids,
|
|
|
|
UserId = userId ?? createPlaylistRequest?.UserId ?? default,
|
|
|
|
MediaType = mediaType ?? createPlaylistRequest?.MediaType
|
2020-06-08 19:14:41 +00:00
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds items to a playlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistId">The playlist id.</param>
|
|
|
|
/// <param name="ids">Item id, comma delimited.</param>
|
|
|
|
/// <param name="userId">The userId.</param>
|
2020-06-20 22:03:19 +00:00
|
|
|
/// <response code="204">Items added to playlist.</response>
|
|
|
|
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
|
2020-06-08 19:14:41 +00:00
|
|
|
[HttpPost("{playlistId}/Items")]
|
2020-06-20 22:03:19 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-08-21 20:01:19 +00:00
|
|
|
public async Task<ActionResult> AddToPlaylist(
|
2020-09-06 15:07:27 +00:00
|
|
|
[FromRoute, Required] Guid playlistId,
|
2020-11-17 03:29:46 +00:00
|
|
|
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] ids,
|
2020-07-07 15:10:51 +00:00
|
|
|
[FromQuery] Guid? userId)
|
2020-06-08 19:14:41 +00:00
|
|
|
{
|
2020-11-17 03:29:46 +00:00
|
|
|
await _playlistManager.AddToPlaylistAsync(playlistId, ids, userId ?? Guid.Empty).ConfigureAwait(false);
|
2020-06-20 22:03:19 +00:00
|
|
|
return NoContent();
|
2020-06-08 19:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Moves a playlist item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistId">The playlist id.</param>
|
|
|
|
/// <param name="itemId">The item id.</param>
|
|
|
|
/// <param name="newIndex">The new index.</param>
|
2020-06-20 22:03:19 +00:00
|
|
|
/// <response code="204">Item moved to new index.</response>
|
|
|
|
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
|
2020-06-08 19:14:41 +00:00
|
|
|
[HttpPost("{playlistId}/Items/{itemId}/Move/{newIndex}")]
|
2020-06-20 22:03:19 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-08-21 20:01:19 +00:00
|
|
|
public async Task<ActionResult> MoveItem(
|
2020-09-08 00:45:06 +00:00
|
|
|
[FromRoute, Required] string playlistId,
|
|
|
|
[FromRoute, Required] string itemId,
|
2020-09-06 15:07:27 +00:00
|
|
|
[FromRoute, Required] int newIndex)
|
2020-06-08 19:14:41 +00:00
|
|
|
{
|
2020-08-21 20:01:19 +00:00
|
|
|
await _playlistManager.MoveItemAsync(playlistId, itemId, newIndex).ConfigureAwait(false);
|
2020-06-20 22:03:19 +00:00
|
|
|
return NoContent();
|
2020-06-08 19:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes items from a playlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistId">The playlist id.</param>
|
|
|
|
/// <param name="entryIds">The item ids, comma delimited.</param>
|
2020-06-20 22:03:19 +00:00
|
|
|
/// <response code="204">Items removed.</response>
|
|
|
|
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
|
2020-06-08 19:14:41 +00:00
|
|
|
[HttpDelete("{playlistId}/Items")]
|
2020-06-20 22:03:19 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-11-17 03:29:46 +00:00
|
|
|
public async Task<ActionResult> RemoveFromPlaylist(
|
|
|
|
[FromRoute, Required] string playlistId,
|
|
|
|
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] entryIds)
|
2020-06-08 19:14:41 +00:00
|
|
|
{
|
2020-11-17 03:29:46 +00:00
|
|
|
await _playlistManager.RemoveFromPlaylistAsync(playlistId, entryIds).ConfigureAwait(false);
|
2020-06-20 22:03:19 +00:00
|
|
|
return NoContent();
|
2020-06-08 19:14:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the original items of a playlist.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playlistId">The playlist id.</param>
|
|
|
|
/// <param name="userId">User id.</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>
|
2020-10-09 23:35:08 +00:00
|
|
|
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
|
2020-06-08 19:14:41 +00:00
|
|
|
/// <param name="enableImages">Optional. Include image information in output.</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>
|
|
|
|
/// <response code="200">Original playlist returned.</response>
|
|
|
|
/// <response code="404">Playlist not found.</response>
|
|
|
|
/// <returns>The original playlist items.</returns>
|
|
|
|
[HttpGet("{playlistId}/Items")]
|
|
|
|
public ActionResult<QueryResult<BaseItemDto>> GetPlaylistItems(
|
2020-09-06 15:07:27 +00:00
|
|
|
[FromRoute, Required] Guid playlistId,
|
2020-09-08 00:45:06 +00:00
|
|
|
[FromQuery, Required] Guid userId,
|
|
|
|
[FromQuery] int? startIndex,
|
|
|
|
[FromQuery] int? limit,
|
2020-11-09 21:59:04 +00:00
|
|
|
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
|
2020-09-08 00:45:06 +00:00
|
|
|
[FromQuery] bool? enableImages,
|
|
|
|
[FromQuery] bool? enableUserData,
|
|
|
|
[FromQuery] int? imageTypeLimit,
|
2020-11-09 21:53:23 +00:00
|
|
|
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
|
2020-06-08 19:14:41 +00:00
|
|
|
{
|
|
|
|
var playlist = (Playlist)_libraryManager.GetItemById(playlistId);
|
|
|
|
if (playlist == null)
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:15:09 +00:00
|
|
|
var user = userId.Equals(default)
|
|
|
|
? null
|
|
|
|
: _userManager.GetUserById(userId);
|
2020-06-08 19:14:41 +00:00
|
|
|
|
|
|
|
var items = playlist.GetManageableItems().ToArray();
|
|
|
|
|
|
|
|
var count = items.Length;
|
|
|
|
|
|
|
|
if (startIndex.HasValue)
|
|
|
|
{
|
|
|
|
items = items.Skip(startIndex.Value).ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit.HasValue)
|
|
|
|
{
|
|
|
|
items = items.Take(limit.Value).ToArray();
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:36:45 +00:00
|
|
|
var dtoOptions = new DtoOptions { Fields = fields }
|
2020-06-20 22:03:19 +00:00
|
|
|
.AddClientFields(Request)
|
|
|
|
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
|
2020-06-08 19:14:41 +00:00
|
|
|
|
|
|
|
var dtos = _dtoService.GetBaseItemDtos(items.Select(i => i.Item2).ToList(), dtoOptions, user);
|
|
|
|
|
|
|
|
for (int index = 0; index < dtos.Count; index++)
|
|
|
|
{
|
|
|
|
dtos[index].PlaylistItemId = items[index].Item1.Id;
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:46:17 +00:00
|
|
|
var result = new QueryResult<BaseItemDto>(
|
|
|
|
startIndex,
|
|
|
|
count,
|
|
|
|
dtos);
|
2020-06-08 19:14:41 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|