PlaylistsController.cs 9.1 KB

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