AttachmentService.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.MediaEncoding;
  12. using MediaBrowser.Controller.Net;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Providers;
  17. using MediaBrowser.Model.Services;
  18. using Microsoft.Extensions.Logging;
  19. using MimeTypes = MediaBrowser.Model.Net.MimeTypes;
  20. namespace MediaBrowser.Api.Attachments
  21. {
  22. [Route("/Videos/{Id}/{MediaSourceId}/Attachments/{Index}/{Filename}", "GET", Summary = "Gets specified attachment.")]
  23. public class GetAttachment
  24. {
  25. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  26. public Guid Id { get; set; }
  27. [ApiMember(Name = "MediaSourceId", Description = "MediaSourceId", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  28. public string MediaSourceId { get; set; }
  29. [ApiMember(Name = "Index", Description = "The attachment stream index", IsRequired = true, DataType = "int", ParameterType = "path", Verb = "GET")]
  30. public int Index { get; set; }
  31. [ApiMember(Name = "Filename", Description = "The attachment filename", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  32. public string Filename { get; set; }
  33. }
  34. public class AttachmentService : BaseApiService
  35. {
  36. private readonly ILibraryManager _libraryManager;
  37. private readonly IAttachmentExtractor _attachmentExtractor;
  38. public AttachmentService(ILibraryManager libraryManager, IAttachmentExtractor attachmentExtractor)
  39. {
  40. _libraryManager = libraryManager;
  41. _attachmentExtractor = attachmentExtractor;
  42. }
  43. public async Task<object> Get(GetAttachment request)
  44. {
  45. var item = (Video)_libraryManager.GetItemById(request.Id);
  46. var (attachment, attachmentStream) = await GetAttachment(request).ConfigureAwait(false);
  47. var mime = string.IsNullOrWhiteSpace(attachment.MIMEType) ? "application/octet-stream" : attachment.MIMEType;
  48. return ResultFactory.GetResult(Request, attachmentStream, mime);
  49. }
  50. private Task<(MediaAttachment, Stream)> GetAttachment(GetAttachment request)
  51. {
  52. var item = _libraryManager.GetItemById(request.Id);
  53. return _attachmentExtractor.GetAttachment(item,
  54. request.MediaSourceId,
  55. request.Index,
  56. CancellationToken.None);
  57. }
  58. }
  59. }