https://github.com/lofcz/mimetypecore
All the MIME/file extension pairs you will ever need. Comes with optional, magic bytes-based collision resolution.
https://github.com/lofcz/mimetypecore
file-extensions formats mime mime-types
Last synced: about 1 month ago
JSON representation
All the MIME/file extension pairs you will ever need. Comes with optional, magic bytes-based collision resolution.
- Host: GitHub
- URL: https://github.com/lofcz/mimetypecore
- Owner: lofcz
- License: mit
- Created: 2025-05-31T08:57:58.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-06-08T22:59:02.000Z (4 months ago)
- Last Synced: 2025-08-25T08:31:40.639Z (about 1 month ago)
- Topics: file-extensions, formats, mime, mime-types
- Language: C#
- Homepage: https://lofcz.github.io/MimeTypeCore
- Size: 12.2 MB
- Stars: 74
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/MimeTypeCore)
# MimeTypeCore
![]()
Fast MIME type mapping library for the .NET ecosystem. Supports almost any Framework and Core version, includingnetstandard1.2
,net40
, andnet8
. Zero dependencies on targets ≥net46
. Get yourMIME
type or extension and be done with it fast. The mapping is zero-config by default and sourced from authoritative sources, such as IANA and MimeType. About 2,000 extensions and MIME types are mapped.
### ➡️ Try it online: [WASM demo](https://lofcz.github.io/MimeTypeCore)
## ⚡ Getting Started
Install the package:
```powershell
dotnet add package MimeTypeCore
```Get `MIME` type from an extension, or vice versa:
```csharp
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:
```csharp
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`:
```cs
IBrowserFile file; // for example from InputFileChangeEventArgstry
{
// 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`:
```cs
MimeTypeMap.AddMimeTypes([
new KeyValuePair(".js", "text/javascript")
]);
```## 🎯 Examples
- [Blazor Server](https://github.com/lofcz/MimeTypeCore/blob/master/MimeTypeCore/MimeTypeCore.Example.Web/Components/Pages/Home.razor)
- [Blazor Wasm](https://github.com/lofcz/MimeTypeCore/blob/master/MimeTypeCore/MimeTypeCore.Example.Wasm/Pages/Home.razor)
- [MVC](https://github.com/lofcz/MimeTypeCore/blob/master/MimeTypeCore/MimeTypeCore.Example.Mvc/Controllers/HomeController.cs)## 🏵️ Contributing
To contribute, check the [mapping](https://github.com/lofcz/MimeTypeCore/blob/master/MimeTypeCore/MimeTypeCore/MimeTypeMapMapping.cs) file for the hardcoded mappings, and add new entries. Please follow the code style and alphabetical ordering; the included utilities [Inserter](https://github.com/lofcz/MimeTypeCore/tree/master/MimeTypeCore/MimeTypeCore.Inserter) and [Formatter](https://github.com/lofcz/MimeTypeCore/tree/master/MimeTypeCore/MimeTypeCore.Formatter) will help with that. Magic headers can be contributed to [this](https://github.com/lofcz/MimeTypeCore/blob/master/MimeTypeCore/MimeTypeCore/MimeTypeMapMagicBytes.cs) file. If you are touching anything beyond that, provide relevant [test cases](https://github.com/lofcz/MimeTypeCore/tree/master/MimeTypeCore/MimeTypeCore.Tests). Thank you.
## License
This library is licensed under the [MIT](https://github.com/lofcz/FastCloner/blob/next/LICENSE) license. 💜