PlaylistsController.cs 8.4 KB

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