- Notifications
You must be signed in to change notification settings - Fork0
MimeType, provides a convenient way to work with MIME types in C#. It defines a set of properties for each MIME type, allowing you to use properties instead of strings in your code.
License
managedcode/MimeTypes
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
| 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 uploadworkflows, messaging protocols and countless integrations. Unfortunately the canonical values are long strings, which makes codeprone to typos and hard to validate.
ManagedCode.MimeTypes ships a generated helper with more than1,200 extensions sourced from thejshttp/mime-dbproject,Apache's canonicalmime.types list and curated overrides, smart heuristics for multi-part extensions (such as.tar.gz), runtime registration APIs and rich helpers for detecting andcategorising 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 anIMimeHelperabstraction (MimeHelper.Instance) for DI scenarios. - CLI utility to refresh
mimeTypes.jsondirectly frommime-dbor a custom source.
usingManagedCode.MimeTypes;// Extension based lookup (handles multi-part extensions automatically)vargzip=MimeHelper.GetMimeType("archive.tar.gz");// application/gzipvartypeScript=MimeHelper.GetMimeType("module.d.ts");// application/typescript// Content-based detectionusingvarstream=File.OpenRead("report.pdf");vardetected=MimeHelper.GetMimeTypeByContent(stream);// application/pdf// Categorisation helpersif(MimeHelper.IsDocument(detected)){// do something useful}// Reverse lookupvarjpegExtensions=MimeHelper.GetExtensions("image/jpeg");// .jpeg, .jpg, .jpe// Runtime registration (and clean-up)MimeHelper.RegisterMimeType("acme","application/x-acme");varcustom=MimeHelper.GetMimeType("invoice.acme");MimeHelper.UnregisterMimeType("acme");// Override the fallback MIME and use the DI-friendly adapterMimeHelper.SetDefaultMimeType(MimeHelper.JSON);IMimeHelperhelper=MimeHelper.Instance;varfallback=helper.GetMimeType("file.unknownext");// application/json (custom fallback)
A small console utility is included to synchronisemimeTypes.json with upstream datasets and our curated overrides. The repository alsoships 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-placedotnet run --project ManagedCode.MimeTypes.Sync# Provide custom sources or outputDOTNET_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 overridesdotnet 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! Rundotnet test before sending a contribution, and feel free to use the sync utility to keep the MIMEcatalogue current.
About
MimeType, provides a convenient way to work with MIME types in C#. It defines a set of properties for each MIME type, allowing you to use properties instead of strings in your code.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.
