MagicNumber is the module to determine a file's type from its magic number.
It is currently heavily under development, The public API should not be considered stable.
The package can be installed from Hex as:
Add magic_number to your list of dependencies in mix.exs
:
def deps do
[{:magic_number, "~> 0.0.4"}]
end
Ensure magic_number is started before your application:
def application do
[applications: [:magic_number]]
end
% iex -S mix
...
iex(1)> content = File.read!("sample.gif")
<<71, 73, 70, 56, 55, 97, 64, 0, 64, 0, 145, 0, 0, 0, 0, 0, 134, 195, 81, 255, 255, 255, 0, 0, 0, 33, 249, 4, 9, 0, 0, 3, 0, 33, 255, 11, 73, 67, 67, 82, 71, 66, 71, 49, 48, 49, 50, 255, 0, 0, ...>>
iex(2)> MagicNumber.detect(content)
{:ok, {:image, :gif}}
- image/jpeg
- image/png
- image/gif
- image/tiff
- application/pdf
- application/zip
- application/gzip
MIT