A simple library to detect mimetype of a file, stream or buffer array via magic numbers in the header content. It contains over 480 samples that as hardcoded in the library.
Detect mimetype of an extension string or mimetype string like “rar”, “exe”,” image/gif” …. It is possible to add more mimetypes in the library to detect files related to them. In fact, this library finds the mimetype via magic numbers.
Install-Package TwentyDevs.MimeTypeDetective
dotnet add package TwentyDevs.MimeTypeDetective
to detect a mimetype of a file with the address :
MimeTypeDetection.GetMimeType(filePath, Path.GetExtension(filePath));
In the example, passed the extension to method because some files like fdf, pdf, zip, and office files(xlsx, Docx, … ) have the same magic numbers, then detection must be done by extension. Also, it is possible to Detect the mimetype of streams and buffers (byte array) with the method.
To get the mimetype of an extension :
MimeTypeDetection.GetMimeTypeByExtension("rar");
To get other information about the particular mimetype :
MimeTypeDetection.GetMimeTypeinfoByMimetypeString("image/gif");
Except for mimetypes that are defined in the library you can add other mimetype to the mimetypes collection.
MimeTypes.Add
(
fileContent,
headerOffset,
headerSize,
extension,
mimeType,
description,
mustBeDetectByExtention
);
There is a sample in the source code to add a new mimetype and then use it to detect a file (SVG File).