/signature

A general library for obtaining the factual file format

Primary LanguageC#MIT LicenseMIT

Signature

English | 中文

Background

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).

References

Install

  • Install-Package
PM> Install-Package Thinkershare.Signature
  • dotnet.exe
$ dotnet add package Thinkershare.Signature

Usage

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"]

Badge

MIT License Document Signature

Maintainers

@rocketrobin
@thinkershare

Contributing

Submit Issue
Pull Request

This project follows the Contributor Covenant Code of Conduct

Contributors

License

MIT © thinkershare