FileBarrier.Core 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package FileBarrier.Core --version 1.0.5
NuGet\Install-Package FileBarrier.Core -Version 1.0.5
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="FileBarrier.Core" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FileBarrier.Core --version 1.0.5
#r "nuget: FileBarrier.Core, 1.0.5"
#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 FileBarrier.Core as a Cake Addin
#addin nuget:?package=FileBarrier.Core&version=1.0.5

// Install FileBarrier.Core as a Cake Tool
#tool nuget:?package=FileBarrier.Core&version=1.0.5

FileBarrier.Core

FileBarrier.Core is a simple little library (for <b>.NET Core >= 2.1</b>) built to help you to check if the uploaded file is meeting the validation requirements you have by providing whitelist to it. It is an extension method on <b>IFormFile</b>.

Getting Started

Download the package form NuGet FileBarrier.Core. Current release is 1.0.5.

Usage

This library gives you different ways to check if the file is valid or not. You can check for:

  • <b>CheckExtensions</b>: When you want to check for the file extension against a whitelist of the allowed file extensions (provided by you the developer).
  • <b>CheckFileContentTypeToFileExtension</b>: This will check if the MIME-type of the provided file is correct for the file extension, then it will check against a whitelist of the allowed MIME-types (provided by you the developer).
  • <b>CheckContentType</b>: will check if the MIME-type of the provided file is in the a whitelist of the allowed MIME-types (provided by you the developer).
  • <b>CheckFileSize</b>: will check if the file size is ⇐ a specific size (provided by you the developer).
  • <b>AllLayers</b>: will check for all above layers.

Things you need to provide depends on the layers you want:

  • If you want to use the <b>CheckExtensions</b> layer: then you need to provide allowedExtensions <b>a string contains the allowed extensions comma-separated, e.g: "txt,pdf,doc,docx", otherwise an ArgumentNullException will be thrown.</b>
  • If you want to use the <b>CheckFileContentTypeToFileExtension</b> layer: then you need to provide allowedContentTypes <b>a string contains the allowed extensions comma-separated, e.g: "text/plain,application/pdf,application/msword".</b>
  • If you want to use the <b>CheckContentType</b> layer: then you need to provide allowedContentTypes <b>a string contains the allowed extensions comma-separated, e.g: "text/plain,application/pdf,application/msword", otherwise an ArgumentNullException will be thrown.</b>
  • If you want to use the <b>CheckFileSize</b> layer: then you need to provide maxSingleFileSize <b>the maximun file sieze, otherwise an ArgumentNullException will be thrown.</b>
  • If you want to check for <b>AllLayers</b>: then you need to provide allowedExtensions, allowedContentTypes, and maxSingleFileSize.

After providing the required information, then you can check if the file is allowed or not by calling either:

  • <b>IsFileAllowed</b>: returns boolean to indicates if the file is allowed or not against the selected layers. E.g.:
...
private const string ALLOWED_MIME_TYPES = "image/png,image/jpeg";
private const string ALLOWED_EXTENSIONS = "png,jpeg";
public IFormFile File { get; set; }
...

public void OnlyPngOrJpeg(){
    var isAllowed = File.IsFileAllowed(ALLOWED_EXTENSIONS, ALLOWED_MIME_TYPES, null, FileCheckLayers.CheckExtensions, FileCheckLayers.CheckExtensions, FileCheckLayers.CheckFileContentTypeToFileExtension, FileCheckLayers.CheckContentType);
    if(!isAllowed) {
      //do whaterver you need to do.
    }
}

  • <b>IsAllowed</b>: returns BarrierResponse to give you more information about on which layer the checking has occurred (if possible). E.g.:
...
private const string ALLOWED_MIME_TYPES = "image/png,image/jpeg";
private const string ALLOWED_EXTENSIONS = "png,jpeg";
public IFormFile File { get; set; }
...

public void OnlyPngOrJpeg(){
    var response = File.IsAllowed(ALLOWED_EXTENSIONS, ALLOWED_MIME_TYPES, null, FileCheckLayers.CheckExtensions, FileCheckLayers.CheckExtensions, FileCheckLayers.CheckFileContentTypeToFileExtension, FileCheckLayers.CheckContentType);
    if(!response.IsAllowed) {
      //do whaterver you need to do, e.g.:
      if(response.ErrorType == ErrorType.InvalidExtension)
        throw new Exception("Invalid file, only PNG or JPEG files are allowed.");
      
      //or e.g.:
      throw new Exception(response.ErrorMessage);
    }
}

Models

The BarrierResponse:


    public class BarrierResponse
    {
        public string ErrorMessage { get; set; }
        public bool IsAllowed { get; set; } = true;
        public ErrorType? ErrorType { get; set; }
        
        ...
    }

The ErrorType:


    public enum ErrorType
    {
        InvalidExtension = 0,
        ContentTypeToExtensionMismatch,
        InvalidContentType,
        FileSizeExceeded,
        Unknown
    }

Acknowledgements

The MimeType mapper is mainly built by @samuelneff: https://github.com/samuelneff/MimeTypeMap.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 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
6.0.0 460 1/30/2023
3.1.0 266 1/30/2023
1.0.7 278 1/30/2023
1.0.6 681 6/29/2020
1.0.5 773 3/12/2019
1.0.4 592 3/7/2019
1.0.3 785 9/22/2018

A cross-platform base framework, useful for all projects that dealing with uploading files to their .Net Core application.

- Fix MimeTypes duplicate records.
- Added Barrier Response to give you more information about at which layer the error occurred (use in IsAllowed function).