Version | Package | Description |
---|---|---|
ManagedCode.MimeTypes | Core library |
MIME (Multipurpose Internet Mail Extensions) values describe the media type of a payload. They appear in HTTP headers, file upload workflows, messaging protocols and countless integrations. Unfortunately the canonical values are long strings, which makes code prone to typos and hard to validate.
ManagedCode.MimeTypes
ships a generated helper with more than 1,200 extensions sourced from the jshttp/mime-db
project, Apache's canonical mime.types
list and curated overrides, smart heuristics for multi-part extensions (such as .tar.gz
), runtime registration APIs and rich helpers for detecting and
categorising data by content.
- Generated extension → MIME map based on the latest mime-db dataset (plus curated compound extensions such as
tar.gz
,d.ts
,ps1
, …). - Rich overrides for lightweight markup and diagram DSLs (AsciiDoc, BibTeX, Org-Mode, PlantUML, Mermaid, Typst, TikZ, …) tailored for AI/document pipelines.
- Reverse lookup API that returns the extensions known for a given MIME value.
- Runtime registration/unregistration so applications can plug in custom corporate formats.
- Content sniffing for common file signatures (PDF, PNG, JPEG, GIF, WebP, MP4, ZIP/OOXML, ODF, APK, etc.) with graceful handling of short or empty streams.
- Extended categorisation enum covering document, audio/video, script, binary, multipart and message families with convenience predicates.
- Safe-by-default mutation model powered by immutable dictionaries, configurable fallback MIME via
MimeHelper.SetDefaultMimeType
, and anIMimeHelper
abstraction (MimeHelper.Instance
) for DI scenarios. - CLI utility to refresh
mimeTypes.json
directly frommime-db
or a custom source.
using ManagedCode.MimeTypes;
// Extension based lookup (handles multi-part extensions automatically)
var gzip = MimeHelper.GetMimeType("archive.tar.gz"); // application/gzip
var typeScript = MimeHelper.GetMimeType("module.d.ts"); // application/typescript
// Content-based detection
using var stream = File.OpenRead("report.pdf");
var detected = MimeHelper.GetMimeTypeByContent(stream); // application/pdf
// Categorisation helpers
if (MimeHelper.IsDocument(detected))
{
// do something useful
}
// Reverse lookup
var jpegExtensions = MimeHelper.GetExtensions("image/jpeg"); // .jpeg, .jpg, .jpe
// Runtime registration (and clean-up)
MimeHelper.RegisterMimeType("acme", "application/x-acme");
var custom = MimeHelper.GetMimeType("invoice.acme");
MimeHelper.UnregisterMimeType("acme");
// Override the fallback MIME and use the DI-friendly adapter
MimeHelper.SetDefaultMimeType(MimeHelper.JSON);
IMimeHelper helper = MimeHelper.Instance;
var fallback = helper.GetMimeType("file.unknownext"); // application/json (custom fallback)
A small console utility is included to synchronise mimeTypes.json
with upstream datasets and our curated overrides. The repository also
ships a scheduled GitHub Actions workflow that runs the sync tool weekly and opens a pull request whenever new MIME definitions are published.
# Update the data file in-place
dotnet run --project ManagedCode.MimeTypes.Sync
# Provide custom sources or output
DOTNET_CLI_TELEMETRY_OPTOUT=1 dotnet run --project ManagedCode.MimeTypes.Sync -- \
--source https://example.com/primary-mime-db.json \
--add-source https://example.com/additional-mime-map.json \
--output ./artifacts/mimeTypes.json
# Start with a clean slate and prefer local overrides
dotnet run --project ManagedCode.MimeTypes.Sync -- --reset-sources --prefer-remote
Running the tool re-generates the JSON file, which in turn updates the generated helper during the next build.
dotnet add package ManagedCode.MimeTypes
Issues and PRs are welcome! Run dotnet test
before sending a contribution, and feel free to use the sync utility to keep the MIME
catalogue current.