View Source Infer

A dependency free library to infer file and MIME type by checking the magic number signature.

An elixir adaption of the infer rust library.

installation

Installation

The package can be installed by adding infer to your list of dependencies in mix.exs:

def deps do
  [
    {:infer, "~> 0.2.6"}
  ]
end

The docs can be found at https://hexdocs.pm/infer.

examples

Examples

infer-get-1

Infer.get/1

Takes the binary file contents as argument and returns the Infer.Type.t() if the file matches one of the supported types. Returns nil otherwise.

iex> binary = File.read!("test/images/sample.png")
iex> Infer.get(binary)
%Infer.Type{extension: "png", matcher: &Infer.Image.png?/1, matcher_type: :image, mime_type: "image/png"}

infer-get_from_path-1

Infer.get_from_path/1

Similar to Infer.get/1, but takes the file path as argument.

iex> Infer.get_from_path("test/images/sample.png")
%Infer.Type{extension: "png", matcher: &Infer.Image.png?/1, matcher_type: :image, mime_type: "image/png"}

infer-is-2

Infer.is?/2

Takes the binary content and the file extension as arguments. Returns whether the file content is of the given extension.

iex> binary = File.read!("test/images/sample.png")
iex> Infer.is?(binary, "png")
true

infer-mime-2

Infer.mime?/2

Takes the binary content and the file extension as arguments. Returns whether the file content is of the given mime type.

iex> binary = File.read!("test/images/sample.png")
iex> Infer.mime?(binary, "image/png")
true

infer-image-1

Infer.image?/1

Takes the binary file contents as argument and returns whether the file is an image or not.

iex> binary = File.read!("test/images/sample.png")
iex> Infer.image?(binary)
true

infer-document-1

Infer.document?/1

Takes the binary file contents as argument and returns whether the file is a document (microsoft office, open office)

iex> binary = File.read!("test/docs/sample.xlsx")
iex> Infer.document?(binary)
true

infer-doc-docx-1

Infer.Doc.docx?/1

Takes the binary file contents as arguments. Returns true if it's Microsoft Word Open XML Format Document (DOCX) data.

iex> binary = File.read!("test/docs/sample.docx")
iex> Infer.Doc.docx?(binary)
true

supported-types

Supported Types

image

Image

MIMEExtension
image/jpegjpg
image/jp2jp2
image/pngpng
image/gifimage/web
image/x-canon-c32cr2
image/tifftif
image/bmpbmp
image/vnd.ms-photojxr
image/vnd.adobe.photoshoppsd
image/vnd.microsoft.iconico
image/heifheif
image/avifavif

video

Video

MIMEExtension
video/mp4mp4
video/x-m4vm4v
video/x-matroskamkv
video/webmwebm
video/quicktimemov
video/x-msvideoavi
video/x-ms-wmvwmv
video/mpegmpg
video/x-flvflv

audio

Audio

MIMEExtension
audio/midimidi
audio/mpegmp3
audio/m4am4a
audio/oggogg
audio/x-flacflac
audio/x-wavwav
audio/amramr
audio/aacaac
audio/x-aiffaiff

document

Document

MIMEExtension
application/msworddoc
application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx
application/vnd.ms-excelxls
application/vnd.openxmlformats-officedocument.spreadsheetml.sheetxlsx
application/vnd.ms-powerpointppt
application/vnd.openxmlformats-officedocument.presentationml.presentationpptx
application/vnd.oasis.opendocument.textodt
application/vnd.oasis.opendocument.spreadsheetods
application/vnd.oasis.opendocument.presentationodp

archive

Archive

MIMEExtension
application/epub+zipepub
application/zipzip
application/x-tartar
application/vnd.rarrar
application/gzipgz
application/x-bzip2bz2
application/x-7z-compressed7z
application/x-xzxz
application/is_pdfpdf
application/x-shockwave-flashswf
application/rtfrtf
application/octet-streameot
application/postscriptps
application/vnd.sqlite3sqlite
application/x-nintendo-nes-romnex
application/x-unix-archivear
application/x-compressedZ
application/x-lziplz
application/x-rpmrpm
application/dicomdcm
application/zstdzst
application/x-ole-storagemsi
application/x-google-chrome-extensioncrx
application/vnd.ms-cab-compressedcab
application/vnd.debian.binary-packagedeb

font

Font

MIMEExtension
application/font-woffwoff
application/font-woff2woff2
application/font-sfntttf
application/font-sfntotf

book

Book

MIMEExtension
application/epub+zipepub
application/x-mobipocket-ebookmobi

application

Application

MIMEExtension
application/wasmwasm
application/x-executableelf
application/vnd.microsoft.portable-executableexe
application/vnd.microsoft.portable-executabledll
application/javaclass
application/x-llvmbc
application/x-mach-binarymach
application/vnd.android.dexdex
application/vnd.android.detdey
application/x-x509-ca-certder
application/x-executableobj

license

License

This project and is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2021 Daniel Khaapamyaki