jellyfin/MediaBrowser.Api/Attachments/AttachmentService.cs

64 lines
2.5 KiB
C#
Raw Normal View History

2019-10-18 04:57:40 -07:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2019-12-26 15:09:00 -07:00
using MediaBrowser.Controller.Configuration;
2019-10-18 04:57:40 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.Attachments
{
[Route("/Videos/{Id}/{MediaSourceId}/Attachments/{Index}", "GET", Summary = "Gets specified attachment.")]
2019-10-18 04:57:40 -07:00
public class GetAttachment
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public Guid Id { get; set; }
[ApiMember(Name = "MediaSourceId", Description = "MediaSourceId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string MediaSourceId { get; set; }
[ApiMember(Name = "Index", Description = "The attachment stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
public int Index { get; set; }
}
public class AttachmentService : BaseApiService
{
private readonly ILibraryManager _libraryManager;
private readonly IAttachmentExtractor _attachmentExtractor;
2019-12-26 15:09:00 -07:00
public AttachmentService(
ILogger<AttachmentService> logger,
IServerConfigurationManager serverConfigurationManager,
IHttpResultFactory httpResultFactory,
ILibraryManager libraryManager,
IAttachmentExtractor attachmentExtractor)
: base(logger, serverConfigurationManager, httpResultFactory)
2019-10-18 04:57:40 -07:00
{
_libraryManager = libraryManager;
_attachmentExtractor = attachmentExtractor;
}
public async Task<object> Get(GetAttachment request)
{
2019-11-04 08:38:53 -07:00
var (attachment, attachmentStream) = await GetAttachment(request).ConfigureAwait(false);
var mime = string.IsNullOrWhiteSpace(attachment.MimeType) ? "application/octet-stream" : attachment.MimeType;
2019-10-18 04:57:40 -07:00
return ResultFactory.GetResult(Request, attachmentStream, mime);
}
private Task<(MediaAttachment, Stream)> GetAttachment(GetAttachment request)
{
var item = _libraryManager.GetItemById(request.Id);
return _attachmentExtractor.GetAttachment(item,
request.MediaSourceId,
request.Index,
CancellationToken.None);
}
}
}