FileRequestFilter.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections.Generic;
  2. using Jellyfin.Api.Attributes;
  3. using Microsoft.OpenApi.Models;
  4. using Swashbuckle.AspNetCore.SwaggerGen;
  5. namespace Jellyfin.Server.Filters
  6. {
  7. /// <inheritdoc />
  8. public class FileRequestFilter : IOperationFilter
  9. {
  10. /// <inheritdoc />
  11. public void Apply(OpenApiOperation operation, OperationFilterContext context)
  12. {
  13. foreach (var attribute in context.ApiDescription.ActionDescriptor.EndpointMetadata)
  14. {
  15. if (attribute is AcceptsFileAttribute acceptsFileAttribute)
  16. {
  17. operation.RequestBody = GetRequestBody(acceptsFileAttribute.ContentTypes);
  18. break;
  19. }
  20. }
  21. }
  22. private static OpenApiRequestBody GetRequestBody(IEnumerable<string> contentTypes)
  23. {
  24. var body = new OpenApiRequestBody();
  25. var mediaType = new OpenApiMediaType
  26. {
  27. Schema = new OpenApiSchema
  28. {
  29. Type = "string",
  30. Format = "binary"
  31. }
  32. };
  33. foreach (var contentType in contentTypes)
  34. {
  35. body.Content.Add(contentType, mediaType);
  36. }
  37. return body;
  38. }
  39. }
  40. }