VideoAttachmentsController.cs 2.9 KB

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