Skip to content

Repository files navigation

FileSignatures

nuget package

A small library for detecting the type of a file based on header signature (also known as magic number) rather than file extension. It is designed with extensibility in mind, so that recognised formats can be added easily.

How do I install it?

FileSignatures is available on NuGet, so can be installed via the Package Manager:

Install-Package FileSignatures

How do I use it?

Create an instance of the FileFormatInspector class, then pass it a stream to your file:

varinspector=newFileFormatInspector();varformat=inspector.DetermineFileFormat(stream);

This will return a FileFormat instance which contains the signature and media type of the recognised format, or null if a matching format could not be determined.

How do I register it with a dependency injection container?

You can either register an instance calling the empty constructor which will register all formats in the FileSignatures assembly:

services.AddSingleton<IFileFormatInspector>(newFileFormatInspector());

Or use FileFormatLocator to scan for the formats you are interested in then pass that to the constructor of FileFormatInspector and register that instance:

varrecognised=FileFormatLocator.GetFormats().OfType<Image>();varinspector=newFileFormatInspector(recognised);services.AddSingleton<IFileFormatInspector>(inspector);

In this example, only formats which derive from Image (jpg, tiff, bmp, etc.) will be detected. Anything else will be ignored.

How do I check for a type of file?

Because the formats are defined as a type hierarchy, you can either check for a specific type if you want to work with a particular format, or the base type if you are interested in multiple formats.

varformat=inspector.DetermineFileFormat(stream);if(formatisPdf){// Just matches Pdf}if(formatisOfficeOpenXml){// Matches Word, Excel, Powerpoint}if(formatisImage){// Matches any image format}

See the examples for a sample web application and a console application which demonstrate how to filter uploads by a particular format and retrieve the signature details for a file.

What formats are recognised?

Currently, the following formats are built-in:

NameMedia-TypeExtension
3GPPvideo/3gpp.3gp
Bitmapimage/bmp.bmp
Excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet.xlsx
Excel 97-2003application/vnd.ms-excel.xls
Exe (Windows)application/octet-stream.exe
GIFimage/gif.gif
JPEGimage/jpeg.jpg
MP4video/mp4.mp4
Open Document Presentationapplication/vnd.oasis.opendocument.presentationn.odp
Open Document Spreadhseetapplication/vnd.oasis.opendocument.spreadsheet.ods
Open Document Textapplication/vnd.oasis.opendocument.text.odt
Outlook Messageapplication/vnd.ms-outlook.msg
PDFapplication/pdf.pdf
PNGimage/png.png
PowerPointapplication/vnd.openxmlformats-officedocument.presentationml.presentation.pptx
Powerpoint 97-2003application/vnd.ms-powerpoint.ppt
QuickTimevideo/quicktime.mov
Rich Text Formatapplication/rtf.rtf
TIFFimage/tiff.tif
Visioapplication/vnd.visio.vsdx
Visio 97-2003application/vnd.visio.vsd
Webpimage/webp.webp
Wordapplication/vnd.openxmlformats-officedocument.wordprocessingml.document.docx
Word templateapplication/vnd.openxmlformats-officedocument.wordprocessingml.template.dotx
Word 97-2003application/msword.doc
Xpsapplication/vnd.ms-xpsdocument.xps
Zipapplication/zip.zip

How do I add additional formats?

Create a new class (or many classes) which inherit from FileFormat to implement a custom format. Next, pass a collection of recognised formats to the constructor of FileFormatInspector, being sure to include your custom format.

The FileFormatLocator class can be used to load all custom formats located within an assembly:

varassembly=typeof(CustomFileFormat).GetTypeInfo().Assembly;// Just the formats defined in the assembly containing CustomFileFormatvarcustomFormats=FileFormatLocator.GetFormats(assembly);// Formats defined in the assembly and all the defaultsvarallFormats=FileFormatLocator.GetFormats(assembly,true);

Using this method, you can continue to create custom formats and they will automatically be included into the recognised formats without any additional configuration.

What is the licence?

This project is licensed under the MIT license.

About

A small library for detecting the type of a file based on header signature (also known as magic number).

Topics

Resources

Stars

322 stars

Watchers

16 watching

Forks

Releases

Packages

Used by

Contributors

Languages