FileBarrier.Core
1.0.5
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
<PackageReference Include="FileBarrier.Core" Version="1.0.5" />
paket add FileBarrier.Core --version 1.0.5
#r "nuget: FileBarrier.Core, 1.0.5"
// 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 forthe 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 extensionscomma-separated, e.g: "txt,pdf,doc,docx"
, otherwise anArgumentNullException
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 extensionscomma-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 extensionscomma-separated, e.g: "text/plain,application/pdf,application/msword"
, otherwise anArgumentNullException
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 anArgumentNullException
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 | Versions 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. |
-
.NETCoreApp 2.1
- Microsoft.AspNetCore.Http (>= 2.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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).