PlaylistsController.cs 9.5 KB

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