< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.VideoAttachmentsController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/VideoAttachmentsController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 6
Coverable lines: 6
Total lines: 84
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/VideoAttachmentsController.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Net.Mime;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Jellyfin.Api.Attributes;
 7using Jellyfin.Api.Extensions;
 8using Jellyfin.Api.Helpers;
 9using MediaBrowser.Common.Extensions;
 10using MediaBrowser.Controller.Entities;
 11using MediaBrowser.Controller.Library;
 12using MediaBrowser.Controller.MediaEncoding;
 13using Microsoft.AspNetCore.Http;
 14using Microsoft.AspNetCore.Mvc;
 15
 16namespace Jellyfin.Api.Controllers;
 17
 18/// <summary>
 19/// Attachments controller.
 20/// </summary>
 21[Route("Videos")]
 22public 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>
 032    public VideoAttachmentsController(
 033        ILibraryManager libraryManager,
 034        IAttachmentExtractor attachmentExtractor)
 35    {
 036        _libraryManager = libraryManager;
 037        _attachmentExtractor = attachmentExtractor;
 038    }
 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}