2020-10-26 21:24:40 +02:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
namespace MalwareMultiScan.Api.Attributes
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Validate uploaded file size for the max file size defined in settings.
|
|
|
|
/// </summary>
|
|
|
|
public class MaxFileSizeAttribute : ValidationAttribute
|
|
|
|
{
|
|
|
|
/// <inheritdoc />
|
|
|
|
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
|
|
|
{
|
|
|
|
var maxSize = validationContext
|
|
|
|
.GetRequiredService<IConfiguration>()
|
|
|
|
.GetValue<long>("MaxFileSize");
|
2020-10-29 16:09:56 +02:00
|
|
|
|
2020-10-26 21:24:40 +02:00
|
|
|
var formFile = (IFormFile) value;
|
2020-10-29 16:09:56 +02:00
|
|
|
|
2020-10-26 21:24:40 +02:00
|
|
|
if (formFile == null || formFile.Length > maxSize)
|
|
|
|
return new ValidationResult($"File exceeds the maximum size of {maxSize} bytes");
|
2020-10-29 16:09:56 +02:00
|
|
|
|
2020-10-26 21:24:40 +02:00
|
|
|
return ValidationResult.Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|