MimeDescription (MimeDescription v0.12.0)

Copy Markdown View Source

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, so no runtime fetching is required.

Usage

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

# Handle MIME types with parameters (charset, boundary, etc.)
MimeDescription.get_from_header("text/plain; charset=utf-8")
#=> {:ok, "Plain text document"}

# Get description with fallback
MimeDescription.get("unknown/type")
#=> {:error, :not_found}

Updating the MIME database

The MIME data is embedded at compile time. To update it with the latest from freedesktop.org, run:

mix mime_description.generate

This task will check if the remote database has changed and regenerate the embedded data module if needed.

Summary

Functions

Extract the base MIME type from a header value, removing any parameters.

Get the human-friendly description for a MIME type.

Get the human-friendly description for a MIME type, raising if not found.

Get the human-friendly description for a MIME type from a raw header value.

Get the human-friendly description for a MIME type from a raw header value, returning the cleaned MIME type as a fallback if not found.

Get the description for a MIME type with a default fallback.

Functions

extract_mime_type(mime_header)

@spec extract_mime_type(String.t()) :: String.t()

Extract the base MIME type from a header value, removing any parameters.

Examples

iex> MimeDescription.extract_mime_type("text/plain; charset=utf-8")
"text/plain"

iex> MimeDescription.extract_mime_type("application/pdf;name=document.pdf")
"application/pdf"

iex> MimeDescription.extract_mime_type("  text/html ; charset=ISO-8859-1  ")
"text/html"

get(mime_type)

@spec get(String.t()) :: {:ok, String.t()} | {:error, :not_found}

Get the human-friendly description for a MIME type.

Returns {:ok, description} if found, or {:error, :not_found} if not found.

Examples

iex> MimeDescription.get("application/pdf")
{:ok, "PDF document"}

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

iex> MimeDescription.get("unknown/type")
{:error, :not_found}

get!(mime_type)

@spec get!(String.t()) :: String.t()

Get the human-friendly description for a MIME type, raising if not found.

Examples

iex> MimeDescription.get!("application/pdf")
"PDF document"

iex> MimeDescription.get!("unknown/type")
** (KeyError) MIME type not found: "unknown/type"

get_from_header(mime_header)

@spec get_from_header(String.t()) :: {:ok, String.t()} | {:error, :not_found}

Get the human-friendly description for a MIME type from a raw header value.

This function handles MIME types that may include parameters like charset, boundary, etc. It extracts the base MIME type and looks up its description.

Examples

iex> MimeDescription.get_from_header("text/plain; charset=utf-8")
{:ok, "Plain text document"}

iex> MimeDescription.get_from_header("application/pdf; name=document.pdf")
{:ok, "PDF document"}

iex> MimeDescription.get_from_header("text/html;charset=ISO-8859-1")
{:ok, "HTML document"}

iex> MimeDescription.get_from_header("multipart/form-data; boundary=----WebKitFormBoundary")
{:ok, "multipart message"}

iex> MimeDescription.get_from_header("unknown/type; param=value")
{:error, :not_found}

get_from_header_with_fallback(mime_header, default \\ nil)

@spec get_from_header_with_fallback(String.t(), String.t() | nil) :: String.t()

Get the human-friendly description for a MIME type from a raw header value, returning the cleaned MIME type as a fallback if not found.

This is useful when you want to display something reasonable even for unknown types.

Examples

iex> MimeDescription.get_from_header_with_fallback("text/plain; charset=utf-8")
"Plain text document"

iex> MimeDescription.get_from_header_with_fallback("application/x-custom; param=value")
"application/x-custom"

iex> MimeDescription.get_from_header_with_fallback("unknown/type; charset=utf-8", "Unknown file")
"Unknown file"

get_with_default(mime_type, default \\ "Unknown file type")

@spec get_with_default(String.t(), String.t()) :: String.t()

Get the description for a MIME type with a default fallback.

Examples

iex> MimeDescription.get_with_default("application/pdf", "Unknown")
"PDF document"

iex> MimeDescription.get_with_default("unknown/type", "Unknown")
"Unknown"