FileResponseFilter.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Linq;
  3. using Jellyfin.Api.Attributes;
  4. using Microsoft.OpenApi.Models;
  5. using Swashbuckle.AspNetCore.SwaggerGen;
  6. namespace Jellyfin.Server.Filters
  7. {
  8. /// <inheritdoc />
  9. public class FileResponseFilter : IOperationFilter
  10. {
  11. private const string SuccessCode = "200";
  12. private static readonly OpenApiMediaType _openApiMediaType = new OpenApiMediaType
  13. {
  14. Schema = new OpenApiSchema
  15. {
  16. Type = "file"
  17. }
  18. };
  19. /// <inheritdoc />
  20. public void Apply(OpenApiOperation operation, OperationFilterContext context)
  21. {
  22. foreach (var attribute in context.ApiDescription.ActionDescriptor.EndpointMetadata)
  23. {
  24. if (attribute is ProducesFileAttribute producesFileAttribute)
  25. {
  26. // Get operation response values.
  27. var response = operation.Responses
  28. .FirstOrDefault(o => o.Key.Equals(SuccessCode, StringComparison.Ordinal));
  29. // Operation doesn't have a response.
  30. if (response.Value == null)
  31. {
  32. continue;
  33. }
  34. // Clear existing responses.
  35. response.Value.Content.Clear();
  36. // Add all content-types as file.
  37. foreach (var contentType in producesFileAttribute.GetContentTypes())
  38. {
  39. response.Value.Content.Add(contentType, _openApiMediaType);
  40. }
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. }