Here in Syndicode we see the great future for Elixir. That’s why we spend quite a lot of time investigating its features and tools. And today we are presenting Elixir’s MIME, a read-only and immutable library that embeds the MIME type database, so, users can map MIME (Multipurpose Internet Mail Extensions) types to extensions and vice-versa.
One of the goals of this library is to offer a performant lookup of the MIME database at runtime, that’s why new MIME types can only be added at compile-time via configuration, but we’ll talk about this option later. First, let’s review its public API.
MIME offers a short set of functions, which cover the most relevant cases when you work with MIME types.
extensions(String.t()) :: [String.t()]
Returns the extensions associated with the given MIME type.
type(String.t()) :: String.t()
Returns the MIME type related to the given file extension.
from_path(Path.t()) :: String.t()
Guesses the MIME type based on the path’s extension.
has_type?(String.t()) :: boolean
Returns whether an extension has a MIME type associated.
valid?(String.t()) :: boolean
Returns whether a MIME type is registered.
At the time of this writing, and according to the statistics available from the Hex package manager, the MIME library has 21 dependents projects, among those projects you can find: Plug, Phoenix, Tesla, Swoosh, etc., and have been downloaded almost 6 million times. But more importantly, is how the MIME library is implemented, its code is really concise, it’s around 200 SLOC (Source Lines Of Code) including comments, and embed captivating concepts.
MIME is a short but powerful library, its goal is clear, and in just around 200 SLOC you can see a lot of nice concepts, like meta-programming, file streams, pattern matching, macros, unquote fragments, dynamic module creation, dynamic recompilation at boot-time, among other really cool stuff.