AttachmentsController.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Common.Extensions;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Controller.Net;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. namespace Jellyfin.Api.Controllers
  11. {
  12. /// <summary>
  13. /// Attachments controller.
  14. /// </summary>
  15. [Route("Videos")]
  16. [Authenticated]
  17. public class AttachmentsController : Controller
  18. {
  19. private readonly ILibraryManager _libraryManager;
  20. private readonly IAttachmentExtractor _attachmentExtractor;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="AttachmentsController"/> 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 AttachmentsController(
  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. /// <returns>Attachment.</returns>
  40. [HttpGet("{VideoID}/{MediaSourceID}/Attachments/{Index}")]
  41. [Produces("application/octet-stream")]
  42. [ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
  43. [ProducesResponseType(StatusCodes.Status404NotFound)]
  44. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  45. public async Task<IActionResult> GetAttachment(
  46. [FromRoute] Guid videoId,
  47. [FromRoute] string mediaSourceId,
  48. [FromRoute] int index)
  49. {
  50. try
  51. {
  52. var item = _libraryManager.GetItemById(videoId);
  53. if (item == null)
  54. {
  55. return NotFound();
  56. }
  57. var (attachment, stream) = await _attachmentExtractor.GetAttachment(
  58. item,
  59. mediaSourceId,
  60. index,
  61. CancellationToken.None)
  62. .ConfigureAwait(false);
  63. var contentType = "application/octet-stream";
  64. if (string.IsNullOrWhiteSpace(attachment.MimeType))
  65. {
  66. contentType = attachment.MimeType;
  67. }
  68. return new FileStreamResult(stream, contentType);
  69. }
  70. catch (ResourceNotFoundException e)
  71. {
  72. return StatusCode(StatusCodes.Status404NotFound, e.Message);
  73. }
  74. catch (Exception e)
  75. {
  76. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  77. }
  78. }
  79. }
  80. }