Centralized file validation library: MIME detection via magic numbers, filename policy, size/ext/MIME checks, optional security heuristics (zip safety, executables, image dimensions), SHA-256 hashing, and DI integration.
$ dotnet add package Jezda.Common.FilesCentralizovana biblioteka za validaciju fajlova u mikroservisima Jezda Solutions.
IFormFile i Stream).services.AddFileValidation().// Program.cs
builder.Services.AddFileValidation();
// Endpoint
var overrides = new FileValidationOverrides
{
MaxSizeBytes = 10 * 1024 * 1024,
AllowedExtensions = new[] { "png", "jpg", "jpeg", "pdf" },
AllowedMimeTypes = new[] { "image/png", "image/jpeg", "application/pdf" }
};
var result = await fileValidator.ValidateAsync(req.File, overrides, ct);
if (!result.IsValid)
{
return Results.BadRequest(result.Errors.Select(e => new { e.Code, e.Message }));
}
{
"FileValidation": {
"MaxSizeBytes": 10485760,
"AllowedMimeTypes": ["image/png", "image/jpeg", "application/pdf"],
"AllowedExtensions": ["png", "jpg", "jpeg", "pdf"],
"BlockedExtensions": ["exe", "dll"],
"BlockExecutables": true,
"EnableZipSafetyChecks": true,
"ZipMaxDepth": 5,
"ZipMaxEntries": 1000,
"ZipMaxCompressionRatio": 200.0,
"EnableImageDimensionChecks": false,
"EnableHashing": true,
"EnableMalwareHeuristics": true
}
}
IFileScanner preko DI.