This package can be used to detect the MIME type and canonical extension by looking for magic numbers. It works by reading a small amount of data from the file (~256 bytes) and binary pattern matching against it's contents.
Detecting a file's type:
iex> FileType.from_path("profile.png")
{:ok, {"png", "image/png"}}
iex> FileType.from_path("contract.docx")
{:ok, {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}}
Detect a file's type from an IO:
iex> {:ok, file} = File.open("profile.png", [:read, :binary])
{:ok, file}
iex> FileType.from_io(file)
{:ok, {"png", "image/png"}}
The package can be installed by adding file_type
to your list of dependencies in mix.exs
:
def deps do
[
{:file_type, "~> 0.1.0"}
]
end
docx
- Microsoft Word Open XML Documentpptx
- PowerPoint Open XML Presentationxlsx
- Microsoft Excel Open XML Spreadsheetdoc
- Microsoft Word Documentppt
- PowerPoint Presentationxls
- Excel Spreadsheetpdf
- Portable Document Format Fileepub
- Open eBook Filemobi
- Mobipocket eBookodt
- OpenDocument Text Documentods
- OpenDocument Spreadsheetodp
- OpenDocument Presentationrtf
- Rich Text Format File
jpg
- JPEG Imagepng
- Portable Network Graphicapng
- Animated Portable Network Graphicgif
- Graphical Interchange Format Filewebp
- WebP Imageflif
- Free Lossless Image Format Filecr2
- Canon Raw Image Filecr3
- Canon Raw 3 Image Fileorf
- Olympus RAW Filearw
- Sony Digital Camera Imagedng
- Digital Negative Image Filenef
- Nikon Electronic Format RAW Imagerw2
- Panasonic RAW Imageraf
- Fuji RAW Image Filetif
- Tagged Image Filebmp
- Bitmap Image Fileicns
- macOS Icon Resource Filejxr
- JPEG XR Imagepsd
- Adobe Photoshop Documentdmg
- Apple Disk Imageico
- Icon Filebpg
- BPG Imagejp2
- JPEG 2000 Core Image Filejpm
- JPEG 2000 Compound Image File Formatjpx
- JPEG 2000 Image Fileheic
- High Efficiency Image Formatcur
- Windows Cursorktx
- Khronos Textureavif
- AV1 Imagedcm
- DICOM Image
mp4
- MPEG-4 Video Filemkv
- Matroska Video Filewebm
- WebM Video Filemov
- Apple QuickTime Movieavi
- Audio Video Interleave Filempg
- MPEG Video Fileogv
- Ogg Video Fileogm
- Ogg Media Fileflv
- Flash Video Filemts
- AVCHD Video Filemj2
- Motion JPEG 2000 Video Clip3gp
- 3GPP Multimedia File3g2
- 3GPP2 Multimedia Filem4v
- iTunes Video Filem4p
- iTunes Music Store Audio Filef4v
- Flash MP4 Video Filef4p
- Adobe Flash Protected Media File
mp1
- MPEG-1 Layer 1 Audio Filemp2
- MPEG Layer II Compressed Audio Filemp3
- MP3 Audio Fileaac
- Advanced Audio Coding Fileogg
- Ogg Vorbis Audio Fileoga
- Ogg Vorbis Audio Filespx
- Ogg Vorbis Speex Fileopus
- Opus Audio Fileflac
- Free Lossless Audio Codec Filewav
- WAVE Audio Filemid
- MIDI Fileqcp
- PureVoice Audio Fileamr
- Adaptive Multi-Rate Codec Fileaif
- Audio Interchange File Formatape
- Monkey's Audio Lossless Audio Filewv
- WavPack Audio Filempc
- Musepack Compressed Audio Filedsf
- Delusion Digital Sound Filevoc
- Creative Labs Audio Fileac3
- Audio Codec 3 Filem4a
- MPEG-4 Audio Filem4b
- MPEG-4 Audiobook Filef4a
- Adobe Flash Protected Audio Filef4b
- Extension Not Foundit
- Impulse Tracker Modules3m
- ScreamTracker 3 Modulexm
- Fasttracker 2 Extended Module
ttf
- TrueType Fontotf
- OpenType Fontwoff
- Web Open Font Format Filewoff2
- Web Open Font Format 2.0 Fileeot
- Embedded OpenType Font
zip
- Zipped Filetar
- Consolidated Unix File Archiverar
- WinRAR Compressed Archivegz
- Gnu Zipped Archivebz2
- Bzip2 Compressed File7z
- 7-Zip Compressed Filexz
- XZ Compressed Archivear
- Midtown Madness Data FileZ
- Unix Compressed Filelz
- Lzip Compressed Filecfb
- Compound Binary Filecab
- Windows Cabinet Filelzh
- LZH Compressed File
indd
- Adobe InDesign Documentskp
- SketchUp Documentblend
- Blender 3D Data Fileics
- Calendar File
exe
- Windows Executable Filerpm
- Red Hat Package Manager Filexpi
- Cross-platform Installer Packagemsi
- Windows Installer Packagedeb
- Debian Software Package
ogx
- Ogg Vorbis Multiplexed Media Fileswf
- Shockwave Flash Moviesqlite
- SQLite Database Filenes
- Nintendo (NES) ROM Filecrx
- Chrome Extensionmxf
- Material Exchange Format Filewasm
- WebAssembly Binary Filexml
- XML Fileglb
- STK Globe Filepcap
- Packet Capture Datalnk
- Windows Shortcutalias
- macOS Aliasmie
- Meta Information Encapsulationshp
- Shapes Filearrow
- Arrow Columnar Formatps
- PostScript Fileeps
- Encapsulated PostScript Filepgp
- PGP Security Keystl
- Stereolithography File
$ mix benchmark
Most files can be detected with a single binary pattern match. To contribute support for new file type:
- Find an example file. Please make sure you have the rights to use this file.
- Register the fixture in
test/file_type/integration_test.exs
. - Write some code to detect the file's type in
lib/file_type/magic.ex
. - Update the
README
to include a mention of your new file format. - Send a pull request!
Please note that this library is not intended to detect text-based file formats like CSV, JSON, etc.
- file/file - The canonical file-type detection library.
- sindresorhus/file-type - This library is mostly just a port of that library.
- minad/mimemagic - A popular Ruby library for doing the same thing.