VideoAttachmentsController.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Net.Mime;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Jellyfin.Api.Constants;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.MediaEncoding;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. namespace Jellyfin.Api.Controllers
  13. {
  14. /// <summary>
  15. /// Attachments controller.
  16. /// </summary>
  17. [Route("Videos")]
  18. [Authorize(Policy = Policies.DefaultAuthorization)]
  19. public class VideoAttachmentsController : BaseJellyfinApiController
  20. {
  21. private readonly ILibraryManager _libraryManager;
  22. private readonly IAttachmentExtractor _attachmentExtractor;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="VideoAttachmentsController"/> class.
  25. /// </summary>
  26. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  27. /// <param name="attachmentExtractor">Instance of the <see cref="IAttachmentExtractor"/> interface.</param>
  28. public VideoAttachmentsController(
  29. ILibraryManager libraryManager,
  30. IAttachmentExtractor attachmentExtractor)
  31. {
  32. _libraryManager = libraryManager;
  33. _attachmentExtractor = attachmentExtractor;
  34. }
  35. /// <summary>
  36. /// Get video attachment.
  37. /// </summary>
  38. /// <param name="videoId">Video ID.</param>
  39. /// <param name="mediaSourceId">Media Source ID.</param>
  40. /// <param name="index">Attachment Index.</param>
  41. /// <response code="200">Attachment retrieved.</response>
  42. /// <response code="404">Video or attachment not found.</response>
  43. /// <returns>An <see cref="FileStreamResult"/> containing the attachment stream on success, or a <see cref="NotFoundResult"/> if the attachment could not be found.</returns>
  44. [HttpGet("{videoId}/{mediaSourceId}/Attachments/{index}")]
  45. [Produces(MediaTypeNames.Application.Octet)]
  46. [ProducesResponseType(StatusCodes.Status200OK)]
  47. [ProducesResponseType(StatusCodes.Status404NotFound)]
  48. public async Task<ActionResult<FileStreamResult>> GetAttachment(
  49. [FromRoute] Guid videoId,
  50. [FromRoute] string mediaSourceId,
  51. [FromRoute] int index)
  52. {
  53. try
  54. {
  55. var item = _libraryManager.GetItemById(videoId);
  56. if (item == null)
  57. {
  58. return NotFound();
  59. }
  60. var (attachment, stream) = await _attachmentExtractor.GetAttachment(
  61. item,
  62. mediaSourceId,
  63. index,
  64. CancellationToken.None)
  65. .ConfigureAwait(false);
  66. var contentType = string.IsNullOrWhiteSpace(attachment.MimeType)
  67. ? MediaTypeNames.Application.Octet
  68. : attachment.MimeType;
  69. return new FileStreamResult(stream, contentType);
  70. }
  71. catch (ResourceNotFoundException e)
  72. {
  73. return NotFound(e.Message);
  74. }
  75. }
  76. }
  77. }