Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

NotificationsYou must be signed in to change notification settings

managedcode/MimeTypes

Repository files navigation

img|300x200

MimeTypes

.NETCoverage StatusnugetCodeQL

VersionPackageDescription
NuGet PackageManagedCode.MimeTypesCore library

Why MimeTypes?

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.

Feature overview

  • Generated extension → MIME map based on the latest mime-db dataset (plus curated compound extensions such astar.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 viaMimeHelper.SetDefaultMimeType, and anIMimeHelper abstraction (MimeHelper.Instance) for DI scenarios.
  • CLI utility to refreshmimeTypes.json directly frommime-db or a custom source.

Quick start

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)

Keeping the database fresh

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.

Installation

dotnet add package ManagedCode.MimeTypes

Contributing

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

Stars

Watchers

Forks

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp