SkillAgentSearch skills...

MimeTypeCore

All the MIME/file extension pairs you will ever need. Comes with optional, magic bytes-based collision resolution.

Install / Use

/learn @lofcz/MimeTypeCore
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

MimeTypeCore

MimeTypeCore

<img align="left" width="128" height="128" alt="Te Reo Icon" src="https://github.com/user-attachments/assets/250152a4-cbcd-409b-9290-36a2dd7c77f8" /> Fast MIME type mapping library for the .NET ecosystem. Supports almost any Framework and Core version, including <code>netstandard1.2</code>, <code>net40</code>, and <code>net8</code>. Zero dependencies on targets ≥ <code>net46</code>. Get your <code>MIME</code> type or extension and be done with it <i>fast</i>. The mapping is zero-config by default and sourced from authoritative sources, such as <a href="https://www.iana.org/assignments/media-types/media-types.xhtml">IANA</a> and <a href="https://mimetype.io/all-types">MimeType</a>. About 2,000 extensions and MIME types are mapped.

<br/><br/>

➡️ Try it online: WASM demo

⚡ Getting Started

Install the package:

dotnet add package MimeTypeCore

Get MIME type from an extension, or vice versa:

using MimeTypeCore;
MimeTypeMap.TryGetMimeType(".png", out string mimeTypePng); // image/png
MimeTypeMap.TryGetExtension("image/png", out string extension); // .png

⭐ That's it! Please consider starring this repository if you find it helpful.

🔮 Collisions

Sometimes, one extension can have multiple MIME types associated. For example, .ts might be text/typescript, or video/mpeg (ts stands for Transport Stream in this case). To resolve the collision, provide Stream to the file, so the header can be sampled for a known sequence of magic bytes:

using FileStream streamVideo = File.Open("files/video.ts", FileMode.Open);
using FileStream streamTypescript = File.Open("files/typescript.ts", FileMode.Open);

MimeTypeMap.TryGetMimeType(streamVideo, out string mimeTypeVideo); // video/mpeg
MimeTypeMap.TryGetMimeType(streamTypescript, out string mimeTypeTypescript); // text/typescript

🌐 Browser

When dealing with user-provided files, whether from Blazor or MVC, your input is likely to be IBrowserFile or IFormFile. These streams don't support synchronous reading, use MimeTypeMap.TryGetMimeTypeAsync:

IBrowserFile file; // for example from InputFileChangeEventArgs

try
{
    // 500 MB, use reasonable limits out there
    await using Stream stream = file.OpenReadStream(512_000_000);

    // null if not recognized, your MIME type otherwise
    string? mimeType = await MimeTypeMap.TryGetMimeTypeAsync(args.File.Name, stream);
}
catch (Exception e) // the file size is probably over the OpenReadStream limit
{
    
}

💡 Custom MIME types

If you need to register custom MIME/file extension pairs, use MimeTypeMap.AddMimeTypes:

MimeTypeMap.AddMimeTypes([
    new KeyValuePair<string, string>(".js", "text/javascript")
]);

🎯 Examples

🏵️ Contributing

To contribute, check the mapping file for the hardcoded mappings, and add new entries. Please follow the code style and alphabetical ordering; the included utilities Inserter and Formatter will help with that. Magic headers can be contributed to this file. If you are touching anything beyond that, provide relevant test cases. Thank you.

License

This library is licensed under the MIT license. 💜

Related Skills

View on GitHub
GitHub Stars83
CategoryDevelopment
Updated1mo ago
Forks1

Languages

C#

Security Score

100/100

Audited on Feb 23, 2026

No findings