aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/VideoAttachmentsController.cs
blob: 596d2119009ab0739d29ebe6cda3bbf450f6d7b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#nullable enable

using System;
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Jellyfin.Api.Controllers
{
    /// <summary>
    /// Attachments controller.
    /// </summary>
    [Route("Videos")]
    [Authorize]
    public class VideoAttachmentsController : BaseJellyfinApiController
    {
        private readonly ILibraryManager _libraryManager;
        private readonly IAttachmentExtractor _attachmentExtractor;

        /// <summary>
        /// Initializes a new instance of the <see cref="VideoAttachmentsController"/> class.
        /// </summary>
        /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
        /// <param name="attachmentExtractor">Instance of the <see cref="IAttachmentExtractor"/> interface.</param>
        public VideoAttachmentsController(
            ILibraryManager libraryManager,
            IAttachmentExtractor attachmentExtractor)
        {
            _libraryManager = libraryManager;
            _attachmentExtractor = attachmentExtractor;
        }

        /// <summary>
        /// Get video attachment.
        /// </summary>
        /// <param name="videoId">Video ID.</param>
        /// <param name="mediaSourceId">Media Source ID.</param>
        /// <param name="index">Attachment Index.</param>
        /// <response code="200">Attachment retrieved.</response>
        /// <response code="404">Video or attachment not found.</response>
        /// <returns>An <see cref="FileStreamResult"/> containing the attachment stream on success, or a <see cref="NotFoundResult"/> if the attachment could not be found.</returns>
        [HttpGet("{VideoID}/{MediaSourceID}/Attachments/{Index}")]
        [Produces(MediaTypeNames.Application.Octet)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<ActionResult<FileStreamResult>> GetAttachment(
            [FromRoute] Guid videoId,
            [FromRoute] string mediaSourceId,
            [FromRoute] int index)
        {
            try
            {
                var item = _libraryManager.GetItemById(videoId);
                if (item == null)
                {
                    return NotFound();
                }

                var (attachment, stream) = await _attachmentExtractor.GetAttachment(
                        item,
                        mediaSourceId,
                        index,
                        CancellationToken.None)
                    .ConfigureAwait(false);

                var contentType = string.IsNullOrWhiteSpace(attachment.MimeType)
                    ? MediaTypeNames.Application.Octet
                    : attachment.MimeType;

                return new FileStreamResult(stream, contentType);
            }
            catch (ResourceNotFoundException e)
            {
                return NotFound(e.Message);
            }
        }
    }
}