AttachmentsController.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #nullable enable
  2. using System;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.MediaEncoding;
  8. using MediaBrowser.Controller.Net;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. namespace Jellyfin.Api.Controllers
  12. {
  13. /// <summary>
  14. /// Attachments controller.
  15. /// </summary>
  16. [Route("Videos")]
  17. [Authenticated]
  18. public class AttachmentsController : Controller
  19. {
  20. private readonly ILibraryManager _libraryManager;
  21. private readonly IAttachmentExtractor _attachmentExtractor;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="AttachmentsController"/> class.
  24. /// </summary>
  25. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  26. /// <param name="attachmentExtractor">Instance of the <see cref="IAttachmentExtractor"/> interface.</param>
  27. public AttachmentsController(
  28. ILibraryManager libraryManager,
  29. IAttachmentExtractor attachmentExtractor)
  30. {
  31. _libraryManager = libraryManager;
  32. _attachmentExtractor = attachmentExtractor;
  33. }
  34. /// <summary>
  35. /// Get video attachment.
  36. /// </summary>
  37. /// <param name="videoId">Video ID.</param>
  38. /// <param name="mediaSourceId">Media Source ID.</param>
  39. /// <param name="index">Attachment Index.</param>
  40. /// <returns>Attachment.</returns>
  41. [HttpGet("{VideoID}/{MediaSourceID}/Attachments/{Index}")]
  42. [Produces("application/octet-stream")]
  43. [ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
  44. [ProducesResponseType(StatusCodes.Status404NotFound)]
  45. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  46. public async Task<IActionResult> GetAttachment(
  47. [FromRoute] Guid videoId,
  48. [FromRoute] string mediaSourceId,
  49. [FromRoute] int index)
  50. {
  51. try
  52. {
  53. var item = _libraryManager.GetItemById(videoId);
  54. if (item == 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 = "application/octet-stream";
  65. if (string.IsNullOrWhiteSpace(attachment.MimeType))
  66. {
  67. contentType = attachment.MimeType;
  68. }
  69. return new FileStreamResult(stream, contentType);
  70. }
  71. catch (ResourceNotFoundException e)
  72. {
  73. return StatusCode(StatusCodes.Status404NotFound, e.Message);
  74. }
  75. }
  76. }
  77. }