2020-09-01 23:26:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Jellyfin.Api.Attributes;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Filters
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public class FileResponseFilter : IOperationFilter
|
|
|
|
|
{
|
|
|
|
|
private const string SuccessCode = "200";
|
|
|
|
|
private static readonly OpenApiMediaType _openApiMediaType = new OpenApiMediaType
|
|
|
|
|
{
|
|
|
|
|
Schema = new OpenApiSchema
|
|
|
|
|
{
|
2020-12-11 02:05:49 +00:00
|
|
|
|
Type = "string",
|
|
|
|
|
Format = "binary"
|
2020-09-01 23:26:49 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
|
|
|
|
{
|
|
|
|
|
foreach (var attribute in context.ApiDescription.ActionDescriptor.EndpointMetadata)
|
|
|
|
|
{
|
|
|
|
|
if (attribute is ProducesFileAttribute producesFileAttribute)
|
|
|
|
|
{
|
|
|
|
|
// Get operation response values.
|
2020-11-13 16:21:28 +00:00
|
|
|
|
var response = operation.Responses
|
2020-09-01 23:26:49 +00:00
|
|
|
|
.FirstOrDefault(o => o.Key.Equals(SuccessCode, StringComparison.Ordinal));
|
|
|
|
|
|
|
|
|
|
// Operation doesn't have a response.
|
2020-11-13 16:21:28 +00:00
|
|
|
|
if (response.Value == null)
|
2020-09-01 23:26:49 +00:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear existing responses.
|
2020-11-13 16:21:28 +00:00
|
|
|
|
response.Value.Content.Clear();
|
2020-09-01 23:26:49 +00:00
|
|
|
|
|
|
|
|
|
// Add all content-types as file.
|
|
|
|
|
foreach (var contentType in producesFileAttribute.GetContentTypes())
|
|
|
|
|
{
|
2020-11-13 16:21:28 +00:00
|
|
|
|
response.Value.Content.Add(contentType, _openApiMediaType);
|
2020-09-01 23:26:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|