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