{"data":{"slug":"github-xberg-io-xberg-ai-rulez-skills-mime-detection-routing-efc65fc8e16a","title":"MIME Detection & Routing","summary":"Detect and route MIME types for accurate content handling and extraction.","url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-mime-detection-routing-efc65fc8e16a","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-xberg-io-xberg-ai-rulez-skills-mime-detection-routing-efc65fc8e16a","markdown_url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-mime-detection-routing-efc65fc8e16a.md","download_url":"https:\/\/skillshunter.net\/skills\/github-xberg-io-xberg-ai-rulez-skills-mime-detection-routing-efc65fc8e16a\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"The MIME detection process relies on file extensions, magic bytes, or registry-based routing. Ensure the registry is updated before adding new formats.","instructions":"---\ntype: Playbook\ntitle: Mime Detection Routing\ndescription: \"MIME detection and routing for misrouted files or new formats.\"\npaths:\n  - \"crates\/xberg\/src\/core\/mime.rs\"\n  - \"crates\/xberg\/src\/core\/mime\/**\"\n  - \"crates\/xberg\/src\/extractors\/mod.rs\"\nx-ai-rulez:\n  kind: skill\n  id: mime-detection-routing\n  metadata:\n    name: mime-detection-routing\n---\n\n# MIME Detection & Routing\n\n## Detection Flow\n\n```text\nPolicy -> content and\/or extension evidence -> validate_mime_type -> registry.get(mime) -> extractor\n```\n\n## Key Functions\n\n| Function | Location | Behaviour |\n| --- | --- | --- |\n| `detect_mime_type(path, check_exists: bool)` | `core\/mime.rs` | **Path-based only** \u2014 never reads bytes. Lowercased extension \u2192 `EXT_TO_MIME`, then tree-sitter extension detection (feature `tree-sitter`), then `mime_guess::from_path`. `check_exists` gates a file-existence check, not content inspection. |\n| `detect_mime_type_from_bytes(bytes)` | `core\/mime.rs` | Magic-number detection via the `infer` crate. The only content-sniffing entry point. |\n| `validate_mime_type(mime)` | `core\/mime.rs` | Parses the media type, matches its case-insensitive essence against `SUPPORTED_MIME_TYPES`, and returns the registered MIME spelling. Parameters such as `charset` do not affect extractor routing. It does **not** consult the extractor registry. |\n\n## The FORMATS registry is the single source of truth\n\n`FORMATS: &[FormatEntry { extensions, mime_type, aliases }]` in `core\/mime.rs`.\n`EXT_TO_MIME` and `SUPPORTED_MIME_TYPES` are `LazyLock`s **derived** from it by iteration \u2014\nthere is no `m.insert` call site to add to, and hand-editing either is impossible.\n\nThe full registry publishes 106 formats, 140 unique extensions, and 53 aliases, verified by\n`scripts\/sync_supported_counts.py verify`. The published count constants describe that static\nregistry; runtime availability is its intersection with registered extractors. Extension lookup is\ncase-insensitive (the extension is lowercased before the map hit).\n\n## Registry Selection\n\n```rust\nlet registry = get_document_extractor_registry();          \/\/ plugins\/registry\/mod.rs\nlet guard = registry.read()?;\nlet extractor: Arc<dyn DocumentExtractor> = guard.get(mime_type)?;  \/\/ Result, not Option\n```\n\n`DocumentExtractorRegistry::get` (`plugins\/registry\/extractor.rs`) returns the\nhighest-`priority()` extractor for the MIME type, and returns `Err` \u2014 not `None` \u2014 when none\nmatches.\n\n## Wildcard Support\n\nAn extractor may register a family: `\"image\/*\"` matches `image\/png`, `image\/jpeg`, and so on\n(prefix match on a registered type ending in `\/*`).\n\n## Adding a New Format\n\n1. Add one `FormatEntry` to `FORMATS` in `crates\/xberg\/src\/core\/mime.rs`. `EXT_TO_MIME` and\n   `SUPPORTED_MIME_TYPES` update automatically.\n2. Run `scripts\/sync_supported_counts.py sync` to update published count claims, then run its\n   `verify` command.\n3. Implement `InternalDocumentExtractor` (not `DocumentExtractor` \u2014 see\n   `plugin-architecture-patterns`) with `supported_mime_types()` returning the MIME.\n4. Register in `crates\/xberg\/src\/extractors\/mod.rs::register_default_extractors()`.\n\n## Critical Rules\n\n1. Call `validate_mime_type()` before extraction \u2014 but do not treat it as proof an extractor exists.\n2. Extension lookup is case-insensitive.\n3. `detect_mime_type` inspects no content. Extraction defaults to `PreferContent`, which performs\n   bounded content inspection and falls back to a supported extension. Use `ContentOnly` when the\n   filename must be ignored; use `TrustExtension` only for trusted sources.\n4. A specific explicit MIME type is authoritative. `application\/octet-stream` is the exception: it\n   is a generic placeholder and triggers policy-based detection.\n5. Never edit `EXT_TO_MIME` or `SUPPORTED_MIME_TYPES` \u2014 edit `FORMATS`.\n","use_cases":["Route files based on their MIME type for content extraction.","Detect misrouted files or new formats using MIME detection.","Validate MIME types against a registry of supported formats."],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/xberg-io\/xberg\/blob\/ffbf7971796a126fc52f6bb8fb40418772071f61\/.ai-rulez\/skills\/mime-detection-routing\/SKILL.md","source_commit":"ffbf7971796a126fc52f6bb8fb40418772071f61","license":null,"version":null,"listing_approved_at":"2026-11-16","created_at":"2026-11-15T13:34:36.000000Z","updated_at":"2026-11-16T05:16:54.000000Z","seo_title":"MIME Detection & Routing for Content Handling","seo_description":"Detect MIME types and route content for accurate extraction and processing. Supports file extensions, magic bytes, and registry-based routing.","seo_keywords":["MIME detection","MIME routing","content extraction","file format detection","MIME validation"]}}