PlaylistsController.cs 8.4 KB

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