PlaylistsController.cs 8.6 KB

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