summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-10-29 19:40:47 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-10-29 19:40:47 -0400
commitef90bdbde5b473d866c74053a7dd23fbd0691b53 (patch)
treedb8067999193769a53eacc1b5fa225cfd1a3e45f /src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
parent3d8bdacc8cb5c9d5514e052d5d4c1d5f2dbc6e9e (diff)
parent650d729bc3e58ec1ecbb554805f8600f63b59a13 (diff)
downloadSMAPI-ef90bdbde5b473d866c74053a7dd23fbd0691b53.tar.gz
SMAPI-ef90bdbde5b473d866c74053a7dd23fbd0691b53.tar.bz2
SMAPI-ef90bdbde5b473d866c74053a7dd23fbd0691b53.zip
Merge branch 'add-log-parser' into develop
Diffstat (limited to 'src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs')
-rw-r--r--src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs b/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
new file mode 100644
index 00000000..68ead3c2
--- /dev/null
+++ b/src/SMAPI.Web/Framework/AllowLargePostsAttribute.cs
@@ -0,0 +1,52 @@
+using System;
+using Microsoft.AspNetCore.Http.Features;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace StardewModdingAPI.Web.Framework
+{
+ /// <summary>A filter which increases the maximum request size for an endpoint.</summary>
+ /// <remarks>Derived from <a href="https://stackoverflow.com/a/38360093/262123"/>.</remarks>
+ [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
+ public class AllowLargePostsAttribute : Attribute, IAuthorizationFilter, IOrderedFilter
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The underlying form options.</summary>
+ private readonly FormOptions FormOptions;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The attribute order.</summary>
+ public int Order { get; set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ public AllowLargePostsAttribute()
+ {
+ this.FormOptions = new FormOptions
+ {
+ ValueLengthLimit = 200 * 1024 * 1024 // 200MB
+ };
+ }
+
+ /// <summary>Called early in the filter pipeline to confirm request is authorized.</summary>
+ /// <param name="context">The authorisation filter context.</param>
+ public void OnAuthorization(AuthorizationFilterContext context)
+ {
+ IFeatureCollection features = context.HttpContext.Features;
+ IFormFeature formFeature = features.Get<IFormFeature>();
+
+ if (formFeature?.Form == null)
+ {
+ // Request form has not been read yet, so set the limits
+ features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, this.FormOptions));
+ }
+ }
+ }
+}