jellyfin/Jellyfin.Api/Controllers/VideoAttachmentsController.cs

83 lines
3.2 KiB
C#
Raw Normal View History

using System;
2020-08-06 14:17:45 +00:00
using System.ComponentModel.DataAnnotations;
2020-09-02 01:27:57 +00:00
using System.IO;
2020-05-19 19:02:02 +00:00
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Attachments controller.
/// </summary>
[Route("Videos")]
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(
2020-08-06 14:17:45 +00:00
[FromRoute, Required] Guid videoId,
[FromRoute, Required] string mediaSourceId,
[FromRoute, Required] 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);
}
}
}
}