A tiny library for inferring MIME types from magic bytes
The package can be installed
by adding magick_bytes
to your list of dependencies in mix.exs
:
def deps do
[
{:magick_bytes, "~> 0.2.0"}
]
end
To guess the MIME type of a file, just pass its path as an argument to the mime/1 fn. MagickBytes will open the file, read from it and try to guess the MIME type:
MagickBytes.mime("/path/to/my/document.pdf")
> {:ok, "application/pdf"}
If the file that you are trying to match is not covered by this library or is otherwise a custom format, you can set up your own matcher:
defmodule ObscureFileMatcher do
use MagickBytes.CustomMatcher,
# Matches a byte signature (magic bytes)
signature: <<1, 2, 3, 4, 5, 6>>,
# Matches an ASCII string in the file
string: "EBML",
# The mime string to be returned if
# a match is found
mime: "application/obscure_format"
end
You can then pass to your custom matcher a group of bytes and it will try a match:
ObscureFileMatcher.mime(<<1, 2, 3, 4, 5, 6, 69, 66, 77, 76>>)
> "application/obscure_format"
ObscureFileMatcher.mime(<<42, 13, 69>>)
> false
All conditions are checked and must pass to return a match.
Remember that every magic is illusion and can be trivially spoofed. In other words, never do anything like this:
case MagickBytes.mime(secret_file) do
{:ok, "application/secret_file"} -> give_password()
_ -> unauthorized()
end