PommaLabs.MimeTypes 2.3.6 Prefix Reserved

There is a newer version of this package available.
See the version list below for details.
Install-Package PommaLabs.MimeTypes -Version 2.3.6
dotnet add package PommaLabs.MimeTypes --version 2.3.6
<PackageReference Include="PommaLabs.MimeTypes" Version="2.3.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PommaLabs.MimeTypes --version 2.3.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PommaLabs.MimeTypes, 2.3.6"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install PommaLabs.MimeTypes as a Cake Addin
#addin nuget:?package=PommaLabs.MimeTypes&version=2.3.6

// Install PommaLabs.MimeTypes as a Cake Tool
#tool nuget:?package=PommaLabs.MimeTypes&version=2.3.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Mime Types

License: MIT Donate Docs NuGet version NuGet downloads

standard-readme compliant GitLab pipeline status Quality gate Code coverage Renovate enabled

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

Table of Contents

Install

NuGet package PommaLabs.MimeTypes is available for download:

dotnet add package PommaLabs.MimeTypes

Usage

Include the following using statement in your class:

using PommaLabs.MimeTypes;

Then, follow examples below to understand available APIs.

Getting the mime type of a file name

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.

Getting the mime type of a file stream

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:

  • OLECF files are opened using OpenMcdf library and a few probe streams are used to identify following file formats: .doc, .msg, .ppt, .vsd, .xls.
  • OpenDocument files are opened as a ZIP archive and "mimetype" file is searched. If it is found, it contains the correct mime type.
  • Office Open XML files are opened as a ZIP archive and specific files are looked for in order to determine the mime type.

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.

Getting the mime type of a file stream and file name

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.

Getting the extension of a mime type

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

Getting the encoding of mime type

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:

  • 7bit
  • 8bit
  • quoted-printable
  • base64

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.

Mime type constants

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.

Extension constants

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.

Compatibility

This library tries to be as much compatible as possible with https://github.com/samuelneff/MimeTypes project, with following caveats:

  • Namespace is different, in order to avoid name clashes.
  • When throwIfMissing is not specified or is true, an exception will be thrown for unknown inputs.
    • This applies to both mime type lookup and extension lookup.
  • When throwIfMissing is false, an empty string will not be returned to unknown inputs:
    • Missing mime types will receive .bin as default extension.
    • Missing extensions will receive application/octet-stream as default mime type.
  • For valid mime types, the same extension will be returned, with following exceptions:
    • 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:

  • This library exposes mime types and extensions as constants which serve the same purpose of an enum. On the other hand, MimeTypeList package uses them as a static map.
  • Mime types and extensions are exposed with UPPER_CASE names.

Maintainers

@pomma89.

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2020-2021 Alessio Parma

  • .NETFramework 4.5.2

  • .NETFramework 4.6.1

  • .NETFramework 4.7.2

  • .NETStandard 2.0

NuGet packages (2)

Showing the top 2 NuGet packages that depend on PommaLabs.MimeTypes:

Package Downloads
PommaLabs.Thumbnailer.Client

.NET client for Thumbnailer service.

PommaLabs.HtmlArk

HtmlArk embeds images, fonts, CSS and JavaScript into an HTML file.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.4.1 359 9/4/2021
2.4.0 700 8/12/2021
2.3.6 594 7/6/2021
2.3.5 460 6/5/2021
2.3.4 290 5/8/2021
2.3.3 1,242 2/15/2021
2.3.2 1,644 11/8/2020
2.3.1 438 10/17/2020
2.3.0 140 10/3/2020
2.2.0 269 9/12/2020
2.1.2 241 9/6/2020
2.1.1 148 9/1/2020
2.0.1 192 8/29/2020
1.4.1 264 6/19/2020
1.4.0 242 5/9/2020
1.3.2 203 4/26/2020
1.3.0 311 3/29/2020
1.2.0 205 3/28/2020
1.1.0 209 3/22/2020
1.0.1 278 3/15/2020