mime_map

Version, currently main branch1 version
  • main branchlatestSep 1, 2026

github.com/shpeckman/mime_map

Crystal shard for mapping MIME types to file names and vice versa, dynamically generated from IANA registry data.

1 stars
1 dependent
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  mime_map:
    github: shpeckman/mime_map
    branch: main

main is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
>= 1.0.0
License
MIT
Author
shpeckman

Dependencies

This version declares no dependencies.

README

mime_map

A blazing fast, lightweight Crystal shard for bidirectional mapping between file extensions and MIME/media types.

mime_map dynamically generates its mappings at installation time using the most up-to-date IANA registries (covering application, audio, font, image, message, model, multipart, text, and video) as well as the comprehensive Apache mime.types fallback list.

Installation

  1. Add the dependency to your shard.yml:
   dependencies:
     mime_map:
       github: shpeckman/mime_map
       version: ~> 1.0.0
  1. Run shards install

Note: The installation process uses a postinstall hook to run make, which fetches the latest MIME type data directly from IANA and Apache and compiles it into static Crystal Hash structures for O(1) performance.

Usage

require "mime_map"

MimeMap.media_type?("json")
MimeMap.media_type?("ZIP")
MimeMap.media_type?("foo")

MimeMap.media_type("csv")

MimeMap.media_type("unknown", "application/octet-stream")

MimeMap.name?("application/json")

MimeMap.extensions("image/jpeg")

MimeMap.from_ext(".png")

MimeMap.from_filename("/var/www/uploads/document.pdf")
MimeMap.from_filename("Makefile")

MimeMap.category?("png")
MimeMap.category?("archive.zip")
MimeMap.category?("invalid-ext")

MimeMap.category("document.pdf")

MimeMap.image?("png")
MimeMap.image?("photo.png")
MimeMap.image?("image/png")
MimeMap.image?("application/json")

MimeMap.audio?("mp3")
MimeMap.application?("document.pdf")
MimeMap.text?("text/html")

Available Category Checkers

The following category checking methods are automatically generated and backed by the MimeMap::Category enum:

  • MimeMap.application?(input)
  • MimeMap.audio?(input)
  • MimeMap.font?(input)
  • MimeMap.image?(input)
  • MimeMap.message?(input)
  • MimeMap.model?(input)
  • MimeMap.multipart?(input)
  • MimeMap.text?(input)
  • MimeMap.video?(input)

Contributing

  1. Fork it (https://github.com/shpeckman/mime_map/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request