jellyfin-server/Jellyfin.Api/Controllers/VideoAttachmentsController.cs

84 lines
3.2 KiB
C#
Raw Normal View History

using System;
2020-05-19 19:02:02 +00:00
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
2020-06-16 20:15:58 +00:00
using Jellyfin.Api.Constants;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
2020-04-23 16:04:37 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Attachments controller.
/// </summary>
[Route("Videos")]
2020-06-16 20:15:58 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-05-19 19:02:02 +00:00
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>
2020-05-02 23:12:56 +00:00
/// <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}")]
2020-05-19 19:02:02 +00:00
[Produces(MediaTypeNames.Application.Octet)]
2020-04-21 19:57:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 19:57:11 +00:00
public async Task<ActionResult<FileStreamResult>> GetAttachment(
[FromRoute] Guid videoId,
2020-06-27 16:50:44 +00:00
[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);
2020-05-19 19:02:02 +00:00
var contentType = string.IsNullOrWhiteSpace(attachment.MimeType)
? MediaTypeNames.Application.Octet
: attachment.MimeType;
return new FileStreamResult(stream, contentType);
}
catch (ResourceNotFoundException e)
{
2020-05-19 18:58:09 +00:00
return NotFound(e.Message);
}
}
}
}