MIME content type definitions mapped with file extensions and file signatures.
$ dotnet add package PommaLabs.MimeTypesProvides a library and registry for information about MIME content type definitions.
This project was inspired by the following open source projects:
Moreover, this project heavily relies on the following open source projects:
Using DotLiquid, this project simply generates a map of mime types and extensions starting from the data gathered by mime-types-data project maintained by Ruby community. Moreover, this project contains a (not exhaustive) map of file signatures, which can be used, along with file name, in order to better understand the mime type of a file.
NuGet package PommaLabs.MimeTypes is available for download:
dotnet add package PommaLabs.MimeTypesInclude the following using statement in your class:
using PommaLabs.MimeTypes;Then, follow examples below to understand available APIs.
Console.WriteLine("txt -> " + MimeTypeMap.GetMimeType("a.txt")); // "txt -> text/plain"Pass in a file name and get a mime type back.
If a file path is passed in instead of a simple file name, it will be automatically deduced.
If no mime type is found then an exception is thrown, unless throwIfMissing is set to false.
In that case, the generic application/octet-stream mime type is returned.
using var fs = File.OpenRead("a.jpg");
Console.WriteLine("jpg -> " + MimeTypeMap.GetMimeType(fs)); // "jpg -> image/jpeg"Pass in a file stream and get a mime type back. This method uses a very comprehensive list to decode file signatures and a few other inspection strategies, which are:
.doc, .msg, .ppt, .vsd, .xls.For text files, a generic text/plain mime type is returned.
Following empiric check is done to determine if a file is textual:
if a file contains two consecutive NULLs in its first 512 bytes, then it is probably binary.
However, when a text/plain mime type is returned,
file name should also be used in order to receive a more specific mime type.
The empiric check is not perfect at all and might yield wrong results.
The same also happens for image/tiff and application/zip,
whose signature is shared among many different mime types.
If no mime type is found then an exception is thrown, unless throwIfMissing is set to false.
In that case, the generic application/octet-stream mime type is returned.
var fn = "a.css";
using var fs = File.OpenRead(fn);
Console.WriteLine("css -> " + MimeTypeMap.GetMimeType(fs, fn)); // "css -> text/css"This family of methods combines a file signature check with a file extension lookup. This is done in order to improve the quality of the result mime type, which benefits of the file extension check when the signature is shared among many mime types.
For example, text files and ZIP archives benefit from this double check.
A CSS file would be detected as text/plain by the signature check,
while extension lookup correctly yields text/css mime type.
If no mime type is found then an exception is thrown, unless throwIfMissing is set to false.
In that case, the generic application/octet-stream mime type is returned.
Console.WriteLine("audio/wav -> " + MimeTypeMap.GetExtension("audio/wav")); // "audio/wav -> .wav"
Console.WriteLine("audio/wav -> " + MimeTypeMap.GetExtensionWithoutDot("audio/wav")); // "audio/wav -> wav"Pass in a mime type and get an extension back. If the mime type is not registered, an error is thrown.
If the mime type is not found then an exception is thrown, unless throwIfMissing is set to false.
In that case, the generic .bin extension is returned.
To get all the extensions linked to a specific mime type:
MimeTypeMap.GetExtensions("image/jpeg"); // [".jpg", ".jpeg", ".jpe"]
MimeTypeMap.GetExtensionsWithoutDot("image/jpeg"); // ["jpg", "jpeg", "jpe"]Console.WriteLine("text/plain -> " + MimeTypeMap.GetEncoding("text/plain")); // "text/plain -> quoted-printable"Pass in a mime type and get its encoding back. Available encodings are:
If no mime type is found then an exception is thrown, unless throwIfMissing is set to false.
In that case, the generic base64 encoding is returned.
Console.WriteLine(MimeTypeMap.Application.Onenote); // "applcation/onenote"
Console.WriteLine(MimeTypeMap.Image.Png); // "image/png"
Console.WriteLine(MimeTypeMap.Video._3gpp); // "video/3gpp"
Console.WriteLine(MimeTypeMap.GetExtension(MimeTypeMap.Application.Onenote)); // ".one"
Console.WriteLine(MimeTypeMap.GetExtension(MimeTypeMap.Image.Png)); // ".png"
Console.WriteLine(MimeTypeMap.GetExtension(MimeTypeMap.Video._3gpp)); // ".3gpp"Subtypes starting with a digit are prefixed with a _ character,
since C# field names cannot start with a digit.
Console.WriteLine(MimeTypeMap.Extensions.One); // ".one"
Console.WriteLine(MimeTypeMap.Extensions.Png); // ".png"
Console.WriteLine(MimeTypeMap.Extensions._3gpp); // ".3gpp"
Console.WriteLine(MimeTypeMap.GetMimeType(MimeTypeMap.Extensions.One)); // "application/onenote"
Console.WriteLine(MimeTypeMap.GetMimeType(MimeTypeMap.Extensions.Png)); // "image/png"
Console.WriteLine(MimeTypeMap.GetMimeType(MimeTypeMap.Extensions._3gpp)); // "video/3gpp"Extensions starting with a digit are prefixed with a _ character,
since C# field names cannot start with a digit. For the same reason,
invalid leading characters are replaced with a _ character.
This library tries to be as much compatible as possible with https://github.com/samuelneff/MimeTypes project, with following caveats:
throwIfMissing is not specified or is true, an exception will be thrown for unknown inputs.
throwIfMissing is false, an empty string will not be returned to unknown inputs:
.bin as default extension.application/octet-stream as default mime type.application/onenote now returns .onepkg instead of .one.
[Source data] and [Apache mime.types] do not map .one extension.application/x-compressed now returns .z instead of .tgz.
[Source data] maps .tgz extension to application/x-gtar mime type.application/x-x509-ca-cert now returns der instead of cer.
[Apache mime.types] maps .cer to application/pkix-cert mime type.audio/x-pn-realaudio-plugin now returns rmp instead of rpm.
Both are valid, but rpm is commonly used by Red Hat Package Manager.video/3gpp2 now returns .3g2 instead of .3gp2.
[Source data] and [Apache mime.types] do not map .3gp2 extension.x-world/x-vrml now returns .wrl instead of .xof.
[Source data] and [Apache mime.types] do not map .xof extension.This library might look similar to MimeTypeList package, but there are a few core differences:
PRs accepted.
Small note: If editing the README, please conform to the standard-readme specification.
MIT © 2020-2021 Alessio Parma