Mizuki/Validators/FormFileValidator.cs
2025-01-12 20:17:43 +01:00

26 lines
No EOL
672 B
C#

using FluentValidation;
namespace Mizuki.Validators;
/// <summary>
/// The validator for uploaded form files.
/// </summary>
public class FormFileValidator : AbstractValidator<IFormFile>
{
/// <summary>
/// Constructs the rule set for the FormFileValidator.
/// </summary>
public FormFileValidator()
{
const int MiB = 1024 * 1024;
RuleFor(f => f.Length)
.LessThan(50 * MiB);
RuleFor(f => f.FileName)
.NotEmpty();
RuleFor(f => f.FileName)
.Matches(@"^(?!\s)(?!.*\s$)[^<>:""/\\|?*\x00-\x1F]+\.(?!\.)[^<>:""/\\|?*\x00-\x1F]{1,4}$|^(?!\s)(?!.*\s$)[^<>:""/\\|?*\x00-\x1F]+$");
}
}