Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.08 KB

README.md

File metadata and controls

40 lines (32 loc) · 1.08 KB

FileTypeValidator

FileTypeValidator do not allow users to inject invalid file. It validate files by its content types which is more accurate. Also it is easy to use. After using FileTypeValidator we can validate file by contact types such as Image,Audio,Video and document/text.

Configuration

Di configuration for dotnet 6 project.

  var builder = WebApplication.CreateBuilder(args);
  builder.Services.UseFileTypeValidator();

Di configuration for dotnet 5 project C# 10.

  public void ConfigureServices(IServiceCollection services) 
  {
      services.UseFileTypeValidator();
   }
        

Usage

Dependency injection

    private readonly IFileTypeValidator _fileTypeValidator;

    public HomeController(IFileTypeValidator fileTypeValidator)
    {
        _fileTypeValidator = fileTypeValidator;
    }
    if (!_fileTypeValidator.IsAudio(fileName))
        {
            throw new Exception("Invalid file for audio");
        }

Available methods to validate file content are IsImage,IsVideo,IsAudio,IsDocument and IsFile.