PommaLabs.MimeTypes 2.7.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
This package has a SemVer 2.0.0 package version: 2.7.0+c6194cd.
There is a newer version of this package available.
See the version list below for details.
dotnet add package PommaLabs.MimeTypes --version 2.7.0
NuGet\Install-Package PommaLabs.MimeTypes -Version 2.7.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PommaLabs.MimeTypes" Version="2.7.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PommaLabs.MimeTypes --version 2.7.0
#r "nuget: PommaLabs.MimeTypes, 2.7.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.7.0

// Install PommaLabs.MimeTypes as a Cake Tool
#tool nuget:?package=PommaLabs.MimeTypes&version=2.7.0

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

MRs accepted.

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

Editing

Visual Studio Code, with Remote Containers extension, is the recommended way to work on this project.

A development container has been configured with all required tools.

Visual Studio Community is also supported and an updated solution file, mime-types.sln, has been provided.

Restoring dependencies

When starting the development container, dependencies should be automatically restored.

Anyway, dependencies can be restored with following command:

dotnet restore

Running tests

Tests can be run with following command:

dotnet test

Tests can also be run with following command, which collects coverage information:

./build.sh --target run-tests

Updating autogenerated files

When source data is updated, autogenerated project files need to be manually updated in order to get new or changed mime types.

Following command updates all autogenerated files:

./build.sh --target update-maps

License

MIT © 2020-2022 Alessio Parma

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

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

Package Downloads
PommaLabs.Thumbnailer.Client The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

.NET client for Thumbnailer service.

PommaLabs.HtmlArk The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

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

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on PommaLabs.MimeTypes:

Repository Stars
bcssov/IronyModManager
Mod Manager for Paradox Games. Official Discord: https://discord.gg/t9JmY8KFrV
Version Downloads Last updated
2.9.1 1,893 2/7/2024
2.9.0 81 2/5/2024
2.8.7 610 1/30/2024
2.8.6 3,488 11/9/2023
2.8.5 1,350 10/4/2023
2.8.4 2,077 8/10/2023
2.8.3 18,048 2/22/2023
2.8.2 121 2/20/2023
2.8.1 861 1/13/2023
2.8.0 1,645 12/18/2022
2.7.2 5,087 11/4/2022
2.7.1 253 10/29/2022
2.7.0 3,781 6/23/2022
2.6.0 1,239 5/21/2022
2.5.0 9,051 11/21/2021
2.4.3 851 10/9/2021
2.4.2 136 10/9/2021
2.4.1 1,073 9/4/2021
2.4.0 1,251 8/12/2021
2.3.6 1,226 7/6/2021
2.3.5 743 6/5/2021
2.3.4 986 5/8/2021
2.3.3 1,821 2/15/2021
2.3.2 3,524 11/8/2020
2.3.1 798 10/17/2020
2.3.0 420 10/3/2020
2.2.0 529 9/12/2020
2.1.2 1,089 9/6/2020
2.1.1 423 9/1/2020
2.0.1 465 8/29/2020
1.4.1 1,145 6/19/2020
1.4.0 1,659 5/9/2020
1.3.2 484 4/26/2020
1.3.0 1,628 3/29/2020
1.2.0 488 3/28/2020
1.1.0 490 3/22/2020
1.0.1 601 3/15/2020