FileType (file_type v0.1.0) View Source

Detect the MIME type of a file based on it's content.

Link to this section Summary

Functions

Format an error returned by this library.

Determines a MIME type from an IO device.

This is the same as from_io/1, except that it will open and close a file for you.

Link to this section Types

Specs

error() :: File.posix() | :unrecognized

Specs

ext() :: binary()

Specs

mime() :: binary()

Specs

result() :: {:ok, t()} | {:error, error()}

Specs

t() :: {ext(), mime()}

Link to this section Functions

Specs

format_error(error()) :: binary()

Format an error returned by this library.

Examples

iex> FileType.format_error(:unrecognized)
"does not match any known format"

iex> FileType.format_error(:enoent)
"no such file or directory"

Specs

from_io(IO.device()) :: result()

Determines a MIME type from an IO device.

Examples

iex> {:ok, io} = File.open("profile.png", [:read, :binary])
{:ok, #PID<0.109.0>}

iex> FileType.from_io(io)
{:ok, {"png", "image/png"}}

Specs

from_path(Path.t()) :: result()

This is the same as from_io/1, except that it will open and close a file for you.

Examples

iex> FileType.from_path("profile.png")
{:ok, {"png", "image/png"}}

iex> FileType.from_path("contract.docx")
{:ok, {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}}

iex> FileType.from_path("example.txt")
{:error, :unrecognized}

iex> FileType.from_path("does-not-exist.png")
{:error, :enoent}