PommaLabs.MimeTypes 2.9.6

Prefix Reserved
This package has a SemVer 2.0.0 package version: 2.9.6+37a7f7b5.
dotnet add package PommaLabs.MimeTypes --version 2.9.6                
NuGet\Install-Package PommaLabs.MimeTypes -Version 2.9.6                
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.9.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PommaLabs.MimeTypes --version 2.9.6                
#r "nuget: PommaLabs.MimeTypes, 2.9.6"                
#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.9.6

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

MIME Types

License: MIT Donate Docs NuGet version NuGet downloads

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

MIME content type definitions mapped with file extensions and file signatures.

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, a project maintained by Ruby community.

This project also 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.

This library also tries to detect at least, but not only, the file signatures detected by FileSignatures, with the goal of providing a comprehensive solution to detect file MIME types.

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-2024 PommaLabs Team and Contributors

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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

.NET client for Thumbnailer service.

PommaLabs.HtmlArk

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.6 3,914 9/17/2024
2.9.5 422 9/5/2024
2.9.4 1,705 7/30/2024
2.9.3 614 7/5/2024
2.9.2 8,973 5/31/2024
2.9.1 7,836 2/7/2024
2.9.0 101 2/5/2024
2.8.7 7,952 1/30/2024
2.8.6 4,026 11/9/2023
2.8.5 2,482 10/4/2023
2.8.4 2,100 8/10/2023
2.8.3 25,464 2/22/2023
2.8.2 145 2/20/2023
2.8.1 883 1/13/2023
2.8.0 1,669 12/18/2022
2.7.2 6,173 11/4/2022
2.7.1 275 10/29/2022
2.7.0 4,012 6/23/2022
2.6.0 1,268 5/21/2022
2.5.0 11,130 11/21/2021
2.4.3 881 10/9/2021
2.4.2 161 10/9/2021
2.4.1 1,093 9/4/2021
2.4.0 1,313 8/12/2021
2.3.6 1,376 7/6/2021
2.3.5 774 6/5/2021
2.3.4 1,147 5/8/2021
2.3.3 1,866 2/15/2021
2.3.2 3,651 11/8/2020
2.3.1 828 10/17/2020
2.3.0 446 10/3/2020
2.2.0 567 9/12/2020
2.1.2 1,160 9/6/2020
2.1.1 454 9/1/2020
2.0.1 493 8/29/2020
1.4.1 1,204 6/19/2020
1.4.0 1,759 5/9/2020
1.3.2 514 4/26/2020
1.3.0 1,719 3/29/2020
1.2.0 518 3/28/2020
1.1.0 523 3/22/2020
1.0.1 645 3/15/2020