PlaylistsController.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Api.Attributes;
  7. using Jellyfin.Api.Extensions;
  8. using Jellyfin.Api.Helpers;
  9. using Jellyfin.Api.ModelBinders;
  10. using Jellyfin.Api.Models.PlaylistDtos;
  11. using MediaBrowser.Controller.Dto;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.Playlists;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.Playlists;
  17. using MediaBrowser.Model.Querying;
  18. using Microsoft.AspNetCore.Authorization;
  19. using Microsoft.AspNetCore.Http;
  20. using Microsoft.AspNetCore.Mvc;
  21. using Microsoft.AspNetCore.Mvc.ModelBinding;
  22. namespace Jellyfin.Api.Controllers;
  23. /// <summary>
  24. /// Playlists controller.
  25. /// </summary>
  26. [Authorize]
  27. public class PlaylistsController : BaseJellyfinApiController
  28. {
  29. private readonly IPlaylistManager _playlistManager;
  30. private readonly IDtoService _dtoService;
  31. private readonly IUserManager _userManager;
  32. private readonly ILibraryManager _libraryManager;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="PlaylistsController"/> class.
  35. /// </summary>
  36. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  37. /// <param name="playlistManager">Instance of the <see cref="IPlaylistManager"/> interface.</param>
  38. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  39. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  40. public PlaylistsController(
  41. IDtoService dtoService,
  42. IPlaylistManager playlistManager,
  43. IUserManager userManager,
  44. ILibraryManager libraryManager)
  45. {
  46. _dtoService = dtoService;
  47. _playlistManager = playlistManager;
  48. _userManager = userManager;
  49. _libraryManager = libraryManager;
  50. }
  51. /// <summary>
  52. /// Creates a new playlist.
  53. /// </summary>
  54. /// <remarks>
  55. /// For backwards compatibility parameters can be sent via Query or Body, with Query having higher precedence.
  56. /// Query parameters are obsolete.
  57. /// </remarks>
  58. /// <param name="name">The playlist name.</param>
  59. /// <param name="ids">The item ids.</param>
  60. /// <param name="userId">The user id.</param>
  61. /// <param name="mediaType">The media type.</param>
  62. /// <param name="createPlaylistRequest">The create playlist payload.</param>
  63. /// <returns>
  64. /// A <see cref="Task" /> that represents the asynchronous operation to create a playlist.
  65. /// The task result contains an <see cref="OkResult"/> indicating success.
  66. /// </returns>
  67. [HttpPost]
  68. [ProducesResponseType(StatusCodes.Status200OK)]
  69. public async Task<ActionResult<PlaylistCreationResult>> CreatePlaylist(
  70. [FromQuery, ParameterObsolete] string? name,
  71. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder)), ParameterObsolete] IReadOnlyList<Guid> ids,
  72. [FromQuery, ParameterObsolete] Guid? userId,
  73. [FromQuery, ParameterObsolete] string? mediaType,
  74. [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] CreatePlaylistDto? createPlaylistRequest)
  75. {
  76. if (ids.Count == 0)
  77. {
  78. ids = createPlaylistRequest?.Ids ?? Array.Empty<Guid>();
  79. }
  80. userId ??= createPlaylistRequest?.UserId ?? default;
  81. userId = RequestHelpers.GetUserId(User, userId);
  82. var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest
  83. {
  84. Name = name ?? createPlaylistRequest?.Name,
  85. ItemIdList = ids,
  86. UserId = userId.Value,
  87. MediaType = mediaType ?? createPlaylistRequest?.MediaType
  88. }).ConfigureAwait(false);
  89. return result;
  90. }
  91. /// <summary>
  92. /// Adds items to a playlist.
  93. /// </summary>
  94. /// <param name="playlistId">The playlist id.</param>
  95. /// <param name="ids">Item id, comma delimited.</param>
  96. /// <param name="userId">The userId.</param>
  97. /// <response code="204">Items added to playlist.</response>
  98. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  99. [HttpPost("{playlistId}/Items")]
  100. [ProducesResponseType(StatusCodes.Status204NoContent)]
  101. public async Task<ActionResult> AddToPlaylist(
  102. [FromRoute, Required] Guid playlistId,
  103. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] Guid[] ids,
  104. [FromQuery] Guid? userId)
  105. {
  106. userId = RequestHelpers.GetUserId(User, userId);
  107. await _playlistManager.AddToPlaylistAsync(playlistId, ids, userId.Value).ConfigureAwait(false);
  108. return NoContent();
  109. }
  110. /// <summary>
  111. /// Moves a playlist item.
  112. /// </summary>
  113. /// <param name="playlistId">The playlist id.</param>
  114. /// <param name="itemId">The item id.</param>
  115. /// <param name="newIndex">The new index.</param>
  116. /// <response code="204">Item moved to new index.</response>
  117. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  118. [HttpPost("{playlistId}/Items/{itemId}/Move/{newIndex}")]
  119. [ProducesResponseType(StatusCodes.Status204NoContent)]
  120. public async Task<ActionResult> MoveItem(
  121. [FromRoute, Required] string playlistId,
  122. [FromRoute, Required] string itemId,
  123. [FromRoute, Required] int newIndex)
  124. {
  125. await _playlistManager.MoveItemAsync(playlistId, itemId, newIndex).ConfigureAwait(false);
  126. return NoContent();
  127. }
  128. /// <summary>
  129. /// Removes items from a playlist.
  130. /// </summary>
  131. /// <param name="playlistId">The playlist id.</param>
  132. /// <param name="entryIds">The item ids, comma delimited.</param>
  133. /// <response code="204">Items removed.</response>
  134. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  135. [HttpDelete("{playlistId}/Items")]
  136. [ProducesResponseType(StatusCodes.Status204NoContent)]
  137. public async Task<ActionResult> RemoveFromPlaylist(
  138. [FromRoute, Required] string playlistId,
  139. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] entryIds)
  140. {
  141. await _playlistManager.RemoveFromPlaylistAsync(playlistId, entryIds).ConfigureAwait(false);
  142. return NoContent();
  143. }
  144. /// <summary>
  145. /// Gets the original items of a playlist.
  146. /// </summary>
  147. /// <param name="playlistId">The playlist id.</param>
  148. /// <param name="userId">User id.</param>
  149. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  150. /// <param name="limit">Optional. The maximum number of records to return.</param>
  151. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  152. /// <param name="enableImages">Optional. Include image information in output.</param>
  153. /// <param name="enableUserData">Optional. Include user data.</param>
  154. /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
  155. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  156. /// <response code="200">Original playlist returned.</response>
  157. /// <response code="404">Playlist not found.</response>
  158. /// <returns>The original playlist items.</returns>
  159. [HttpGet("{playlistId}/Items")]
  160. public ActionResult<QueryResult<BaseItemDto>> GetPlaylistItems(
  161. [FromRoute, Required] Guid playlistId,
  162. [FromQuery, Required] Guid userId,
  163. [FromQuery] int? startIndex,
  164. [FromQuery] int? limit,
  165. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  166. [FromQuery] bool? enableImages,
  167. [FromQuery] bool? enableUserData,
  168. [FromQuery] int? imageTypeLimit,
  169. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
  170. {
  171. var playlist = (Playlist)_libraryManager.GetItemById(playlistId);
  172. if (playlist is null)
  173. {
  174. return NotFound();
  175. }
  176. var user = userId.Equals(default)
  177. ? null
  178. : _userManager.GetUserById(userId);
  179. var items = playlist.GetManageableItems().ToArray();
  180. var count = items.Length;
  181. if (startIndex.HasValue)
  182. {
  183. items = items.Skip(startIndex.Value).ToArray();
  184. }
  185. if (limit.HasValue)
  186. {
  187. items = items.Take(limit.Value).ToArray();
  188. }
  189. var dtoOptions = new DtoOptions { Fields = fields }
  190. .AddClientFields(User)
  191. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  192. var dtos = _dtoService.GetBaseItemDtos(items.Select(i => i.Item2).ToList(), dtoOptions, user);
  193. for (int index = 0; index < dtos.Count; index++)
  194. {
  195. dtos[index].PlaylistItemId = items[index].Item1.Id;
  196. }
  197. var result = new QueryResult<BaseItemDto>(
  198. startIndex,
  199. count,
  200. dtos);
  201. return result;
  202. }
  203. }