tree_magic v0.1.0 TreeMagic

Binding to tree_magic, providing MIME information for files.

Link to this section Summary

Functions

Get the MIME type of a file, given its path.

Get the MIME type of a string.

Determines if a MIME is an alias of another MIME

Check if the given filepath matches the given MIME type.

Checks if the given string matches the given MIME type.

Link to this section Types

Link to this type

mime_type()

mime_type() :: binary()

Link to this section Functions

Link to this function

from_filepath(path)

from_filepath(Path.t()) :: {:ok, mime_type()} | {:error, term()}

Get the MIME type of a file, given its path.

Will return an error if the file is missing.

iex> TreeMagic.from_filepath("test/fixtures/150.png")

iex> TreeMagic.from_filepath("test/fixtures/VeryImportant.odt")

iex> TreeMagic.from_filepath("missing.png")

Link to this function

from_u8(bytes)

from_u8(binary()) :: mime_type()

Get the MIME type of a string.

iex> bytes = File.read!("test/fixtures/150.png") iex> TreeMagic.from_u8(bytes) "image/png" iex> bytes = File.read!("test/fixtures/VeryImportant.odt") iex> TreeMagic.from_u8(bytes) "application/vnd.oasis.opendocument.text"

Link to this function

is_alias(mime1, mime2)

is_alias(mime_type(), mime_type()) :: boolean()

Determines if a MIME is an alias of another MIME

If this returns true, that means the two MIME types are equivalent. If this returns false, either one of the MIME types are missing, or they are different.

iex> TreeMagic.is_alias("application/zip", "application/x-zip-compressed") true

Link to this function

match_filepath(mimetype, path)

match_filepath(mime_type(), Path.t()) :: boolean()

Check if the given filepath matches the given MIME type.

If the given MIME type is not known or if the file could not be read, false will be returned.

iex> TreeMagic.match_filepath("image/png", "test/fixtures/150.png") true iex> TreeMagic.match_filepath("application/zip", "test/fixtures/150.png") false iex> TreeMagic.match_filepath("image/png", "missing.png") false

Link to this function

match_u8(mimetype, bytes)

match_u8(mime_type(), binary()) :: boolean()

Checks if the given string matches the given MIME type.

Returns true or false if it matches or not. If the given MIME type is not known, the function will always return false. If mimetype is an alias of a known MIME, the file will be checked agains that MIME.

iex> bytes = File.read!("test/fixtures/150.png") iex> TreeMagic.match_u8("image/png", bytes) true iex> TreeMagic.match_u8("application/zip", bytes) false