FileTypeInterrogator 1.0.0

netstandard library for detecting file types by 'magic numbers', similar to the `file` command in Linux/Unix. Useful for validating file uploads instead of trusting file extensions.

Install-Package FileTypeInterrogator -Version 1.0.0
dotnet add package FileTypeInterrogator --version 1.0.0
<PackageReference Include="FileTypeInterrogator" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FileTypeInterrogator --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

FileTypeInterrogator

netstandard library for detecting file types by 'magic numbers', similar to the file command in Linux/Unix. Useful for validating file uploads instead of trusting file extensions.

Usage

IFileTypeInterrogator interrogator = new FileTypeInterrogator();

byte[] fileBytes = File.ReadAllBytes("pdfFile.pdf");

FileTypeInfo fileTypeInfo = interrogator.DetectType(fileBytes);

Console.WriteLine("Name = " + fileTypeInfo.Name);
Console.WriteLine("Extension = " + fileTypeInfo.FileType);
Console.WriteLine("Mime Type = " + fileTypeInfo.MimeType);

// The following output would be displayed:
// Name = Portable Document Format File
// Extension = pdf
// Mime Type = application/pdf

Notice that IFileTypeInterrogator was assigned, meaning custom implementations are welcomed. File definitions are provided in the source and will be regularly updated, feel free to submit an issue or pull request to add other signatures. To quickly provide support for a new signature, create an instance of CustomFileTypeInterrogator:

IFileTypeInterrogator interrogator = new CustomFileTypeInterrogator("path_to_custom_definitions_file", Encoding.UTF8);

FileTypeInfo fileTypeInfo = interrogator.DetectType(...);

It is best to create a single instance and manage it's lifetime through an IOC/DI Container.

FileTypeInterrogator

netstandard library for detecting file types by 'magic numbers', similar to the file command in Linux/Unix. Useful for validating file uploads instead of trusting file extensions.

Usage

IFileTypeInterrogator interrogator = new FileTypeInterrogator();

byte[] fileBytes = File.ReadAllBytes("pdfFile.pdf");

FileTypeInfo fileTypeInfo = interrogator.DetectType(fileBytes);

Console.WriteLine("Name = " + fileTypeInfo.Name);
Console.WriteLine("Extension = " + fileTypeInfo.FileType);
Console.WriteLine("Mime Type = " + fileTypeInfo.MimeType);

// The following output would be displayed:
// Name = Portable Document Format File
// Extension = pdf
// Mime Type = application/pdf

Notice that IFileTypeInterrogator was assigned, meaning custom implementations are welcomed. File definitions are provided in the source and will be regularly updated, feel free to submit an issue or pull request to add other signatures. To quickly provide support for a new signature, create an instance of CustomFileTypeInterrogator:

IFileTypeInterrogator interrogator = new CustomFileTypeInterrogator("path_to_custom_definitions_file", Encoding.UTF8);

FileTypeInfo fileTypeInfo = interrogator.DetectType(...);

It is best to create a single instance and manage it's lifetime through an IOC/DI Container.

Release Notes

Update to use flat file definitions, unit tested

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 87 7/8/2019