VideoAttachmentsController.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. {
  14. /// <summary>
  15. /// Attachments controller.
  16. /// </summary>
  17. [Route("Videos")]
  18. public class VideoAttachmentsController : BaseJellyfinApiController
  19. {
  20. private readonly ILibraryManager _libraryManager;
  21. private readonly IAttachmentExtractor _attachmentExtractor;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="VideoAttachmentsController"/> 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 VideoAttachmentsController(
  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. /// <response code="200">Attachment retrieved.</response>
  41. /// <response code="404">Video or attachment not found.</response>
  42. /// <returns>An <see cref="FileStreamResult"/> containing the attachment stream on success, or a <see cref="NotFoundResult"/> if the attachment could not be found.</returns>
  43. [HttpGet("{videoId}/{mediaSourceId}/Attachments/{index}")]
  44. [ProducesFile(MediaTypeNames.Application.Octet)]
  45. [ProducesResponseType(StatusCodes.Status200OK)]
  46. [ProducesResponseType(StatusCodes.Status404NotFound)]
  47. public async Task<ActionResult> GetAttachment(
  48. [FromRoute, Required] Guid videoId,
  49. [FromRoute, Required] string mediaSourceId,
  50. [FromRoute, Required] int index)
  51. {
  52. try
  53. {
  54. var item = _libraryManager.GetItemById(videoId);
  55. if (item == null)
  56. {
  57. return NotFound();
  58. }
  59. var (attachment, stream) = await _attachmentExtractor.GetAttachment(
  60. item,
  61. mediaSourceId,
  62. index,
  63. CancellationToken.None)
  64. .ConfigureAwait(false);
  65. var contentType = string.IsNullOrWhiteSpace(attachment.MimeType)
  66. ? MediaTypeNames.Application.Octet
  67. : attachment.MimeType;
  68. return new FileStreamResult(stream, contentType);
  69. }
  70. catch (ResourceNotFoundException e)
  71. {
  72. return NotFound(e.Message);
  73. }
  74. }
  75. }
  76. }