Giter Club home page Giter Club logo

filebarrier.core's Introduction

FileBarrier.Core

FileBarrier.Core is a simple little library (for .NET Core >= 2.1) 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 IFormFile.

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:

  • CheckExtensions: When you want to check for the file extension against a whitelist of the allowed file extensions (provided by you the developer).
  • CheckFileContentTypeToFileExtension: 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).
  • CheckContentType: 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).
  • CheckFileSize: will check if the file size is <= a specific size (provided by you the developer).
  • AllLayers: will check for all above layers.

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

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

  • IsFileAllowed: 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.
    }
}
  • IsAllowed: 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.

filebarrier.core's People

Contributors

abdulhameed-a avatar dependabot[bot] avatar

Stargazers

 avatar  avatar Ali avatar Bader Albarrak avatar Saad Alothman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.