English | 中文
In web application, we accept files uploaded by users,
and many times need to know the type of files uploaded by users.
Usually we can assume the type of file uploaded by the user by the file extension,
but many times the file extension name is untrusted.
Therefore, I developed this library,
which is a general library that reads the file header to obtain the
true type and MIME TYPE of the file(only depend .NET Standard 2.1
).
Install-Package
PM> Install-Package Thinkershare.Signature
dotnet.exe
$ dotnet add package Thinkershare.Signature
The following is a simple usage example, please refer Signature Documentation to the details
- Obtaining MIME TYPE for file extension string
var extension = ".jpg";
var result = extension.GetMimeType();
//Result: "image/jpeg"
- Obtaining the factual file format of a file
var signature = new Signature();
signature.AddRecords(Record.FrequentRecords);
signature.AddRecords(Record.UnfrequentRecords);
var head = new byte[] { 0xff, 0xd8, 0xff, 0xdb };
var result = signature.Match(head);
//Result: ["jpg","jpeg"]
- Obtaining the factual file format of a file(express mode)
using Thinkershare.Signature.Extensions;
var head = new byte[] { 0xff, 0xd8, 0xff, 0xdb };
var result = head.GetExtension(head);
//Result: "jpg"
var result = head.GetExtensions(head);
//Result: ["jpg","jpeg"]
This project follows the Contributor Covenant Code of Conduct
MIT © thinkershare