Hex.pmHexdocs.pmGithub.com

MimeDescription

Human-friendly MIME type descriptions for Elixir.

This library provides embedded MIME type descriptions sourced from the freedesktop.org shared-mime-info database. The data is compiled directly into the library at build time, so no runtime fetching is required.

Installation

Add mime_description to your list of dependencies in mix.exs:

def deps do
  [
    {:mime_description, "~> 0.1.0"}
  ]
end

Usage

# Get description for a MIME type
MimeDescription.get("application/pdf")
#=> {:ok, "PDF document"}

MimeDescription.get("text/plain")
#=> {:ok, "plain text document"}

# Handle unknown types
MimeDescription.get("unknown/type")
#=> {:error, :not_found}

# Get with a default fallback
MimeDescription.get_with_default("unknown/type", "Unknown file")
#=> "Unknown file"

# Raise if not found
MimeDescription.get!("application/pdf")
#=> "PDF document"

How It Works

Unlike runtime-based solutions, MimeDescription embeds the MIME type database directly into your compiled application. This means:

The MIME data is sourced from the authoritative freedesktop.org shared-mime-info database and is automatically updated weekly via GitHub Actions.

Updating the MIME Database

The library includes a Mix task to update the embedded MIME data:

# Check for updates and regenerate if needed
mix mime_description.generate

# Force regeneration
mix mime_description.generate --force

This task:

  1. Checks if the upstream database has changed (using ETags)
  2. Downloads the latest XML data if needed
  3. Parses and generates an Elixir module with the embedded data
  4. The generated module is compiled into your application

Automated Updates

The MIME database is automatically updated weekly via GitHub Actions. The workflow:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Credits

MIME type descriptions are sourced from the freedesktop.org shared-mime-info database.