forked fromh2non/filetype
- Notifications
You must be signed in to change notification settings - Fork0
Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature
License
NotificationsYou must be signed in to change notification settings
harisudarsan1/filetype
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Small and dependency freeGo package to infer file and MIME type checking themagic numbers signature.
For SVG file type checking, seego-is-svg package. Python port:filetype.py.
- Supports awide range of file types
- Provides file extension and proper MIME type
- File discovery by extension or MIME type
- File discovery by class (image, video, audio...)
- Provides a bunch of helpers and file matching shortcuts
- Pluggable: add custom new types and matchers
- Simple and semantic API
- Blazing fast, even processing large files
- Only first 262 bytes representing the max file header is required, so you can justpass a slice
- Dependency free (just Go code, no C compilation needed)
- Cross-platform file recognition
go get github.com/h2non/filetype
SeeGodoc reference.
package mainimport ("fmt""io/ioutil""github.com/h2non/filetype")funcmain() {buf,_:=ioutil.ReadFile("sample.jpg")kind,_:=filetype.Match(buf)ifkind==filetype.Unknown {fmt.Println("Unknown file type")return }fmt.Printf("File type: %s. MIME: %s\n",kind.Extension,kind.MIME.Value)}
package mainimport ("fmt""io/ioutil""github.com/h2non/filetype")funcmain() {buf,_:=ioutil.ReadFile("sample.jpg")iffiletype.IsImage(buf) {fmt.Println("File is an image") }else {fmt.Println("Not an image") }}
package mainimport ("fmt""github.com/h2non/filetype")funcmain() {// Check if file is supported by extensioniffiletype.IsSupported("jpg") {fmt.Println("Extension supported") }else {fmt.Println("Extension not supported") }// Check if file is supported by extensioniffiletype.IsMIMESupported("image/jpeg") {fmt.Println("MIME type supported") }else {fmt.Println("MIME type not supported") }}
package mainimport ("fmt""os""github.com/h2non/filetype")funcmain() {// Open a file descriptorfile,_:=os.Open("movie.mp4")// We only have to pass the file header = first 261 byteshead:=make([]byte,261)file.Read(head)iffiletype.IsImage(head) {fmt.Println("File is an image") }else {fmt.Println("Not an image") }}
package mainimport ("fmt""github.com/h2non/filetype")varfooType=filetype.NewType("foo","foo/foo")funcfooMatcher(buf []byte)bool {returnlen(buf)>1&&buf[0]==0x01&&buf[1]==0x02}funcmain() {// Register the new matcher and its typefiletype.AddMatcher(fooType,fooMatcher)// Check if the new type is supported by extensioniffiletype.IsSupported("foo") {fmt.Println("New supported type: foo") }// Check if the new type is supported by MIMEiffiletype.IsMIMESupported("foo/foo") {fmt.Println("New supported MIME type: foo/foo") }// Try to match the filefooFile:= []byte{0x01,0x02}kind,_:=filetype.Match(fooFile)ifkind==filetype.Unknown {fmt.Println("Unknown file type") }else {fmt.Printf("File type matched: %s\n",kind.Extension) }}
- jpg -
image/jpeg
- png -
image/png
- gif -
image/gif
- webp -
image/webp
- cr2 -
image/x-canon-cr2
- tif -
image/tiff
- bmp -
image/bmp
- heif -
image/heif
- jxr -
image/vnd.ms-photo
- psd -
image/vnd.adobe.photoshop
- ico -
image/vnd.microsoft.icon
- dwg -
image/vnd.dwg
- avif -
image/avif
- mp4 -
video/mp4
- m4v -
video/x-m4v
- mkv -
video/x-matroska
- webm -
video/webm
- mov -
video/quicktime
- avi -
video/x-msvideo
- wmv -
video/x-ms-wmv
- mpg -
video/mpeg
- flv -
video/x-flv
- 3gp -
video/3gpp
- mid -
audio/midi
- mp3 -
audio/mpeg
- m4a -
audio/mp4
- ogg -
audio/ogg
- flac -
audio/x-flac
- wav -
audio/x-wav
- amr -
audio/amr
- aac -
audio/aac
- aiff -
audio/x-aiff
- epub -
application/epub+zip
- zip -
application/zip
- tar -
application/x-tar
- rar -
application/vnd.rar
- gz -
application/gzip
- bz2 -
application/x-bzip2
- 7z -
application/x-7z-compressed
- xz -
application/x-xz
- zstd -
application/zstd
- pdf -
application/pdf
- exe -
application/vnd.microsoft.portable-executable
- swf -
application/x-shockwave-flash
- rtf -
application/rtf
- iso -
application/x-iso9660-image
- eot -
application/octet-stream
- ps -
application/postscript
- sqlite -
application/vnd.sqlite3
- nes -
application/x-nintendo-nes-rom
- crx -
application/x-google-chrome-extension
- cab -
application/vnd.ms-cab-compressed
- deb -
application/vnd.debian.binary-package
- ar -
application/x-unix-archive
- Z -
application/x-compress
- lz -
application/x-lzip
- rpm -
application/x-rpm
- elf -
application/x-executable
- dcm -
application/dicom
- doc -
application/msword
- docx -
application/vnd.openxmlformats-officedocument.wordprocessingml.document
- xls -
application/vnd.ms-excel
- xlsx -
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
- ppt -
application/vnd.ms-powerpoint
- pptx -
application/vnd.openxmlformats-officedocument.presentationml.presentation
- woff -
application/font-woff
- woff2 -
application/font-woff
- ttf -
application/font-sfnt
- otf -
application/font-sfnt
- wasm -
application/wasm
- dex -
application/vnd.android.dex
- dey -
application/vnd.android.dey
Measured usingreal files.
Environment: OSX x64 i7 2.7 Ghz
BenchmarkMatchTar-8 1000000 1083 ns/opBenchmarkMatchZip-8 1000000 1162 ns/opBenchmarkMatchJpeg-8 1000000 1280 ns/opBenchmarkMatchGif-8 1000000 1315 ns/opBenchmarkMatchPng-8 1000000 1121 ns/op
MIT - Tomas Aparicio