2020-04-19 23:36:05 +00:00
using System ;
2020-05-19 19:02:02 +00:00
using System.Net.Mime ;
2020-04-19 23:36:05 +00:00
using System.Threading ;
using System.Threading.Tasks ;
2020-06-16 20:15:58 +00:00
using Jellyfin.Api.Constants ;
2020-04-19 23:36:05 +00:00
using MediaBrowser.Common.Extensions ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.MediaEncoding ;
2020-04-23 16:04:37 +00:00
using Microsoft.AspNetCore.Authorization ;
2020-04-19 23:36:05 +00:00
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
2020-04-19 23:36:05 +00:00
{
private readonly ILibraryManager _libraryManager ;
private readonly IAttachmentExtractor _attachmentExtractor ;
/// <summary>
2020-05-19 14:28:02 +00:00
/// Initializes a new instance of the <see cref="VideoAttachmentsController"/> class.
2020-04-19 23:36:05 +00: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 14:28:02 +00:00
public VideoAttachmentsController (
2020-04-19 23:36:05 +00: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 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>
2020-06-21 00:02:07 +00:00
[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)]
2020-04-19 23:36:05 +00:00
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 19:57:11 +00:00
public async Task < ActionResult < FileStreamResult > > GetAttachment (
2020-04-19 23:36:05 +00:00
[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 ) ;
2020-05-19 19:02:02 +00:00
var contentType = string . IsNullOrWhiteSpace ( attachment . MimeType )
? MediaTypeNames . Application . Octet
: attachment . MimeType ;
2020-04-19 23:36:05 +00:00
return new FileStreamResult ( stream , contentType ) ;
}
catch ( ResourceNotFoundException e )
{
2020-05-19 18:58:09 +00:00
return NotFound ( e . Message ) ;
2020-04-19 23:36:05 +00:00
}
}
}
}