using System;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Filters;
namespace StardewModdingAPI.Web.Framework
{
/// A filter which increases the maximum request size for an endpoint.
/// Derived from .
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AllowLargePostsAttribute : Attribute, IAuthorizationFilter, IOrderedFilter
{
/*********
** Fields
*********/
/// The underlying form options.
private readonly FormOptions FormOptions;
/*********
** Accessors
*********/
/// The attribute order.
public int Order { get; set; }
/*********
** Public methods
*********/
/// Construct an instance.
public AllowLargePostsAttribute()
{
this.FormOptions = new FormOptions
{
ValueLengthLimit = 200 * 1024 * 1024 // 200MB
};
}
/// Called early in the filter pipeline to confirm request is authorized.
/// The authorisation filter context.
public void OnAuthorization(AuthorizationFilterContext context)
{
IFeatureCollection features = context.HttpContext.Features;
IFormFeature formFeature = features.Get();
if (formFeature?.Form == null)
{
// Request form has not been read yet, so set the limits
features.Set(new FormFeature(context.HttpContext.Request, this.FormOptions));
}
}
}
}