2020-04-19 16:36:05 -07:00
using System ;
2020-08-06 07:17:45 -07:00
using System.ComponentModel.DataAnnotations ;
2020-05-19 12:02:02 -07:00
using System.Net.Mime ;
2020-04-19 16:36:05 -07:00
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 12:02:02 -07:00
public class VideoAttachmentsController : BaseJellyfinApiController
2020-04-19 16:36:05 -07:00
{
private readonly ILibraryManager _libraryManager ;
private readonly IAttachmentExtractor _attachmentExtractor ;
/// <summary>
2020-05-19 07:28:02 -07:00
/// Initializes a new instance of the <see cref="VideoAttachmentsController"/> class.
2020-04-19 16:36:05 -07:00
/// </summary>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="attachmentExtractor">Instance of the <see cref="IAttachmentExtractor"/> interface.</param>
2020-05-19 07:28:02 -07:00
public VideoAttachmentsController (
2020-04-19 16:36:05 -07:00
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 16:12:56 -07: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>
2020-06-20 17:02:07 -07:00
[HttpGet("{videoId}/{mediaSourceId}/Attachments/{index}")]
2020-05-19 12:02:02 -07:00
[Produces(MediaTypeNames.Application.Octet)]
2020-04-21 12:57:11 -07:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-19 16:36:05 -07:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 12:57:11 -07:00
public async Task < ActionResult < FileStreamResult > > GetAttachment (
2020-08-06 07:17:45 -07:00
[FromRoute, Required] Guid videoId ,
[FromRoute, Required] string mediaSourceId ,
[FromRoute, Required] int index )
2020-04-19 16:36:05 -07:00
{
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 12:02:02 -07:00
var contentType = string . IsNullOrWhiteSpace ( attachment . MimeType )
? MediaTypeNames . Application . Octet
: attachment . MimeType ;
2020-04-19 16:36:05 -07:00
return new FileStreamResult ( stream , contentType ) ;
}
catch ( ResourceNotFoundException e )
{
2020-05-19 11:58:09 -07:00
return NotFound ( e . Message ) ;
2020-04-19 16:36:05 -07:00
}
}
}
}