using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Timeline.Models.Http;
namespace Timeline.Filters
{
///
/// Restrict max content length.
///
public class MaxContentLengthFilter : IResourceFilter
{
///
///
///
/// Max length.
public MaxContentLengthFilter(long maxByteLength)
{
MaxByteLength = maxByteLength;
}
///
/// Max length.
///
public long MaxByteLength { get; set; }
///
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
///
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")));
}
}
}
}