mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-25 22:01:16 +00:00
finalize refactoring & ready for the PR
This commit is contained in:
28
MalwareMultiScan.Api/Attributes/MaxFileSizeAttribute.cs
Normal file
28
MalwareMultiScan.Api/Attributes/MaxFileSizeAttribute.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
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");
|
||||
|
||||
var formFile = (IFormFile) value;
|
||||
|
||||
if (formFile == null || formFile.Length > maxSize)
|
||||
return new ValidationResult($"File exceeds the maximum size of {maxSize} bytes");
|
||||
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user