FileValidator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FileValidator --version 1.0.0                
NuGet\Install-Package FileValidator -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FileValidator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FileValidator --version 1.0.0                
#r "nuget: FileValidator, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FileValidator as a Cake Addin
#addin nuget:?package=FileValidator&version=1.0.0

// Install FileValidator as a Cake Tool
#tool nuget:?package=FileValidator&version=1.0.0                

FileValidator

This library adds an extension method to FileInfo & IFormFile to validate the file based on thier configuration(s).

Installation

Usage

⚙️Using IFormFile on ActionMethod
  public async Task<IActionResult> OnPostUploadAsync(IFormFile file)
  {
      /*By default it validates all the files like Documents,Images, Videos, Audios*/
      bool isValid = file.IsValidFile();
      if(isValid){
          var filePath = Path.GetTempFileName();
          await file.CopyToAsync(File.OpenWrite(filePath));
          return Ok(new { message = "File uploaded successfully." });
      }
      return BadRequest(new {message = "Invalid file"});
      
  }
⚙️Using the FileInfo Class
var fileInfo = new FileInfo("C:/mypath/document.pdf");
var isValid = fileInfo.IsValidFile();
//If you want to only validate the Images then you can pass the allowed FileType[] parameter.
var checkValidImage = fileInfo.IsValidFile(new FileType[] { FileType.Image })
⚙️Updating the default configuration (add/remove allowed extensions & MIME types)
var fileInfo = new FileInfo("C:/mypath/document.pdf");

//If you want to allow json documents then update the configuration
var fileValidatorConfig = new FileValidatorConfiguration();
fileValidatorConfig.DocumentFileExtensins.Add(".json");
fileValidatorConfig.DocumentMimeTypes.Add("application/json");
FileValidatorExtension.UpdateDefaultConfiguration(fileValidatorConfig);

var isValid = fileInfo.IsValidFile(); //Returns true

//If you want to remove any existing valid extension then you can remove the extension from the configuration
fileValidatorConfig.ImmageFileExtensions.Remove(".gif"); //This will return false on any gif file.
FileValidatorExtension.UpdateDefaultConfiguration(fileValidatorConfig);

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated. Feel free to request for any changes or any additional features.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Name: Muthukumar Thevar

Email: mak.thevar@outlook.com

Project Link: https://github.com/mak-thevar/FileValidator

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 760 5/25/2022
1.0.3 418 5/25/2022
1.0.2 429 5/24/2022
1.0.1 418 5/21/2022
1.0.0 469 5/21/2022