PlaylistsController.cs 9.6 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.Helpers;
  10. using Jellyfin.Api.ModelBinders;
  11. using Jellyfin.Api.Models.PlaylistDtos;
  12. using MediaBrowser.Controller.Dto;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Controller.Playlists;
  15. using MediaBrowser.Model.Dto;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.Playlists;
  18. using MediaBrowser.Model.Querying;
  19. using Microsoft.AspNetCore.Authorization;
  20. using Microsoft.AspNetCore.Http;
  21. using Microsoft.AspNetCore.Mvc;
  22. using Microsoft.AspNetCore.Mvc.ModelBinding;
  23. namespace Jellyfin.Api.Controllers
  24. {
  25. /// <summary>
  26. /// Playlists controller.
  27. /// </summary>
  28. [Authorize(Policy = Policies.DefaultAuthorization)]
  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. /// <returns>
  66. /// A <see cref="Task" /> that represents the asynchronous operation to create a playlist.
  67. /// The task result contains an <see cref="OkResult"/> indicating success.
  68. /// </returns>
  69. [HttpPost]
  70. [ProducesResponseType(StatusCodes.Status200OK)]
  71. public async Task<ActionResult<PlaylistCreationResult>> CreatePlaylist(
  72. [FromQuery, ParameterObsolete] string? name,
  73. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder)), ParameterObsolete] IReadOnlyList<Guid> ids,
  74. [FromQuery, ParameterObsolete] Guid? userId,
  75. [FromQuery, ParameterObsolete] string? mediaType,
  76. [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] CreatePlaylistDto? createPlaylistRequest)
  77. {
  78. if (ids.Count == 0)
  79. {
  80. ids = createPlaylistRequest?.Ids ?? Array.Empty<Guid>();
  81. }
  82. var result = await _playlistManager.CreatePlaylist(new PlaylistCreationRequest
  83. {
  84. Name = name ?? createPlaylistRequest?.Name,
  85. ItemIdList = ids,
  86. UserId = userId ?? createPlaylistRequest?.UserId ?? default,
  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. await _playlistManager.AddToPlaylistAsync(playlistId, ids, userId ?? Guid.Empty).ConfigureAwait(false);
  107. return NoContent();
  108. }
  109. /// <summary>
  110. /// Moves a playlist item.
  111. /// </summary>
  112. /// <param name="playlistId">The playlist id.</param>
  113. /// <param name="itemId">The item id.</param>
  114. /// <param name="newIndex">The new index.</param>
  115. /// <response code="204">Item moved to new index.</response>
  116. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  117. [HttpPost("{playlistId}/Items/{itemId}/Move/{newIndex}")]
  118. [ProducesResponseType(StatusCodes.Status204NoContent)]
  119. public async Task<ActionResult> MoveItem(
  120. [FromRoute, Required] string playlistId,
  121. [FromRoute, Required] string itemId,
  122. [FromRoute, Required] int newIndex)
  123. {
  124. await _playlistManager.MoveItemAsync(playlistId, itemId, newIndex).ConfigureAwait(false);
  125. return NoContent();
  126. }
  127. /// <summary>
  128. /// Removes items from a playlist.
  129. /// </summary>
  130. /// <param name="playlistId">The playlist id.</param>
  131. /// <param name="entryIds">The item ids, comma delimited.</param>
  132. /// <response code="204">Items removed.</response>
  133. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  134. [HttpDelete("{playlistId}/Items")]
  135. [ProducesResponseType(StatusCodes.Status204NoContent)]
  136. public async Task<ActionResult> RemoveFromPlaylist(
  137. [FromRoute, Required] string playlistId,
  138. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] entryIds)
  139. {
  140. await _playlistManager.RemoveFromPlaylistAsync(playlistId, entryIds).ConfigureAwait(false);
  141. return NoContent();
  142. }
  143. /// <summary>
  144. /// Gets the original items of a playlist.
  145. /// </summary>
  146. /// <param name="playlistId">The playlist id.</param>
  147. /// <param name="userId">User id.</param>
  148. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  149. /// <param name="limit">Optional. The maximum number of records to return.</param>
  150. /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
  151. /// <param name="enableImages">Optional. Include image information in output.</param>
  152. /// <param name="enableUserData">Optional. Include user data.</param>
  153. /// <param name="imageTypeLimit">Optional. The max number of images to return, per image type.</param>
  154. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  155. /// <response code="200">Original playlist returned.</response>
  156. /// <response code="404">Playlist not found.</response>
  157. /// <returns>The original playlist items.</returns>
  158. [HttpGet("{playlistId}/Items")]
  159. public ActionResult<QueryResult<BaseItemDto>> GetPlaylistItems(
  160. [FromRoute, Required] Guid playlistId,
  161. [FromQuery, Required] Guid userId,
  162. [FromQuery] int? startIndex,
  163. [FromQuery] int? limit,
  164. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
  165. [FromQuery] bool? enableImages,
  166. [FromQuery] bool? enableUserData,
  167. [FromQuery] int? imageTypeLimit,
  168. [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
  169. {
  170. var playlist = (Playlist)_libraryManager.GetItemById(playlistId);
  171. if (playlist == null)
  172. {
  173. return NotFound();
  174. }
  175. var user = !userId.Equals(Guid.Empty) ? _userManager.GetUserById(userId) : null;
  176. var items = playlist.GetManageableItems().ToArray();
  177. var count = items.Length;
  178. if (startIndex.HasValue)
  179. {
  180. items = items.Skip(startIndex.Value).ToArray();
  181. }
  182. if (limit.HasValue)
  183. {
  184. items = items.Take(limit.Value).ToArray();
  185. }
  186. var dtoOptions = new DtoOptions { Fields = fields }
  187. .AddClientFields(Request)
  188. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  189. var dtos = _dtoService.GetBaseItemDtos(items.Select(i => i.Item2).ToList(), dtoOptions, user);
  190. for (int index = 0; index < dtos.Count; index++)
  191. {
  192. dtos[index].PlaylistItemId = items[index].Item1.Id;
  193. }
  194. var result = new QueryResult<BaseItemDto>
  195. {
  196. Items = dtos,
  197. TotalRecordCount = count
  198. };
  199. return result;
  200. }
  201. }
  202. }