$npx -y skills add managedcode/dotnet-skills --skill managedcode-mimetypesUse ManagedCode.MimeTypes when a .NET application needs consistent MIME type detection, extension mapping, and content-type decisions for uploads, downloads, or HTTP responses. USE FOR: integrating ManagedCode.MimeTypes into upload or download flows; mapping file extensions to co
| 1 | # ManagedCode.MimeTypes |
| 2 | |
| 3 | ## Trigger On |
| 4 | |
| 5 | - integrating `ManagedCode.MimeTypes` into upload or download flows |
| 6 | - mapping file extensions to content types in APIs or background processing |
| 7 | - reviewing content-type handling for files, blobs, or attachments |
| 8 | - documenting a reusable MIME-type decision point in a .NET application |
| 9 | |
| 10 | ## Install |
| 11 | |
| 12 | ```bash |
| 13 | dotnet add package ManagedCode.MimeTypes --version 10.0.9 |
| 14 | ``` |
| 15 | |
| 16 | Use `PackageReference` when the repository centralizes dependency versions: |
| 17 | |
| 18 | ```xml |
| 19 | <PackageReference Include="ManagedCode.MimeTypes" Version="10.0.9" /> |
| 20 | ``` |
| 21 | |
| 22 | The current package targets .NET 8, 9, and 10. Keep the version in the repository's existing central package-management file when one is present. |
| 23 | |
| 24 | ## Workflow |
| 25 | |
| 26 | 1. Identify where the application needs stable MIME-type decisions: |
| 27 | - upload validation |
| 28 | - download response headers |
| 29 | - storage metadata |
| 30 | - attachment processing |
| 31 | 2. Centralize content-type mapping instead of scattering ad-hoc string tables across the codebase. |
| 32 | 3. Use one library boundary for extension and MIME lookups. |
| 33 | 4. Validate the extensions and media types that matter to the product. |
| 34 | 5. Document any product-specific overrides separately from the library defaults. |
| 35 | |
| 36 | ## Read MIME Metadata |
| 37 | |
| 38 | Map file names, URLs, and compound extensions through the generated catalog: |
| 39 | |
| 40 | ```csharp |
| 41 | using ManagedCode.MimeTypes; |
| 42 | |
| 43 | var reportType = MimeHelper.GetMimeType("report.pdf"); |
| 44 | var archiveType = MimeHelper.GetMimeType("archive.tar.gz"); |
| 45 | var imageType = MimeHelper.GetMimeType("https://cdn.example.test/avatar.png?v=2"); |
| 46 | var jpegExtensions = MimeHelper.GetExtensions("image/jpeg"); |
| 47 | ``` |
| 48 | |
| 49 | Use registry metadata when the application needs provenance or registration details: |
| 50 | |
| 51 | ```csharp |
| 52 | if (MimeHelper.TryGetMimeTypeInfoByExtension("report.pdf", out var info)) |
| 53 | { |
| 54 | Console.WriteLine($"{info.Mime} registered={info.IsIanaRegistered}"); |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ## Write Application Mappings |
| 59 | |
| 60 | Register product-specific mappings at startup and remove them only when the owning application lifecycle requires it: |
| 61 | |
| 62 | ```csharp |
| 63 | MimeHelper.RegisterMimeType("acme", "application/x-acme"); |
| 64 | var customType = MimeHelper.GetMimeType("invoice.acme"); |
| 65 | |
| 66 | MimeHelper.UnregisterMimeType("acme"); |
| 67 | ``` |
| 68 | |
| 69 | Runtime registrations affect extension and reverse lookup, but do not synthesize full IANA registry metadata. |
| 70 | |
| 71 | ## Validate Upload Content |
| 72 | |
| 73 | Treat the extension and declared content type as claims. Inspect the signature before accepting security-sensitive uploads: |
| 74 | |
| 75 | ```csharp |
| 76 | using var stream = upload.OpenReadStream(); |
| 77 | |
| 78 | if (!MimeHelper.MatchesMimeTypeByContent(stream, upload.ContentType) || |
| 79 | !MimeHelper.MatchesExtensionByContent(upload.FileName, stream)) |
| 80 | { |
| 81 | throw new InvalidOperationException("Upload content does not match its declared type."); |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | `GetMimeTypeByContent` and the `Matches*ByContent` helpers inspect known prefixes and restore the position of seekable streams. They are not full document parsers, malware scanners, or proof that the remainder of a file is valid. |
| 86 | |
| 87 | ## Settings and Tradeoffs |
| 88 | |
| 89 | - Unknown extensions resolve to `MimeHelper.DefaultMimeType`, initially `application/octet-stream`; use `SetDefaultMimeType` only when the whole application owns a different fallback contract. |
| 90 | - Prefer `MimeHelper.Instance` through `IMimeHelper` when dependency injection and test substitution are useful; use static calls for small, deterministic mapping boundaries. |
| 91 | - The `10.0.9` release refreshes the generated MIME database. It adds mappings such as `1clr`, `aion`, `ccr`, `did`, `ignition`, `jumbf`, and `zst`, changes mappings including `3dm`, `bpd`, `frm`, and `wv`, and removes stale entries. Re-run product-specific mapping tests because preferred mappings can change without an API change. |
| 92 | - Never trust MIME classification alone for authorization, file execution, archive extraction, or active-content rendering. |
| 93 | |
| 94 | ```mermaid |
| 95 | flowchart LR |
| 96 | A["File name or extension"] --> B["ManagedCode.MimeTypes lookup"] |
| 97 | B --> C["Resolved MIME type"] |
| 98 | C --> D["Upload validation, storage metadata, or HTTP response"] |
| 99 | ``` |
| 100 | |
| 101 | ## Deliver |
| 102 | |
| 103 | - guidance on where MIME lookup belongs in application code |
| 104 | - recommendations for centralized content-type decisions |
| 105 | - validation expectations for real file types used by the product |
| 106 | |
| 107 | ## Validate |
| 108 | |
| 109 | - M |