aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Filters/FileRequestFilter.cs
blob: 69e10994f4cdc4a9f0f2287d299a72c92c18f779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Collections.Generic;
using Jellyfin.Api.Attributes;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Jellyfin.Server.Filters
{
    /// <inheritdoc />
    public class FileRequestFilter : IOperationFilter
    {
        /// <inheritdoc />
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            foreach (var attribute in context.ApiDescription.ActionDescriptor.EndpointMetadata)
            {
                if (attribute is AcceptsFileAttribute acceptsFileAttribute)
                {
                    operation.RequestBody = GetRequestBody(acceptsFileAttribute.GetContentTypes());
                    break;
                }
            }
        }

        private static OpenApiRequestBody GetRequestBody(IEnumerable<string> contentTypes)
        {
            var body = new OpenApiRequestBody();
            var mediaType = new OpenApiMediaType
            {
                Schema = new OpenApiSchema
                {
                    Type = "string",
                    Format = "binary"
                }
            };
            foreach (var contentType in contentTypes)
            {
                body.Content.Add(contentType, mediaType);
            }

            return body;
        }
    }
}