aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Filters/MaxContentLengthFilter.cs
blob: 0127971aa559b7a522c0a97f540ed57652ddf3b9 (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 Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Timeline.Models.Http;

namespace Timeline.Filters
{
    /// <summary>
    /// Restrict max content length.
    /// </summary>
    public class MaxContentLengthFilter : IResourceFilter
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="maxByteLength">Max length.</param>
        public MaxContentLengthFilter(long maxByteLength)
        {
            MaxByteLength = maxByteLength;
        }

        /// <summary>
        /// Max length.
        /// </summary>
        public long MaxByteLength { get; set; }

        /// <inheritdoc/>
        public void OnResourceExecuted(ResourceExecutedContext context)
        {
        }

        /// <inheritdoc/>
        public void OnResourceExecuting(ResourceExecutingContext context)
        {
            var contentLength = context.HttpContext.Request.ContentLength;
            if (contentLength != null && contentLength > MaxByteLength)
            {
                context.Result = new BadRequestObjectResult(
                    new CommonResponse(ErrorCodes.Common.Content.TooBig,
                        string.Format(Resource.MessageContentLengthTooBig, MaxByteLength + "B")));
            }
        }
    }
}