Small and dependency free Go package to infer file and MIME type checking the magic numbers signature.
For SVG file type checking, see go-is-svg package. Python port: filetype.py.
go get github.com/h2non/filetype
See Godoc reference.
package main
import (
"fmt"
"io/ioutil"
"github.com/h2non/filetype"
)
func main() {
buf, _ := ioutil.ReadFile("sample.jpg")
kind, _ := filetype.Match(buf)
if kind == filetype.Unknown {
fmt.Println("Unknown file type")
return
}
fmt.Printf("File type: %s. MIME: %s\n", kind.Extension, kind.MIME.Value)
}
package main
import (
"fmt"
"io/ioutil"
"github.com/h2non/filetype"
)
func main() {
buf, _ := ioutil.ReadFile("sample.jpg")
if filetype.IsImage(buf) {
fmt.Println("File is an image")
} else {
fmt.Println("Not an image")
}
}
package main
import (
"fmt"
"github.com/h2non/filetype"
)
func main() {
// Check if file is supported by extension
if filetype.IsSupported("jpg") {
fmt.Println("Extension supported")
} else {
fmt.Println("Extension not supported")
}
// Check if file is supported by extension
if filetype.IsMIMESupported("image/jpeg") {
fmt.Println("MIME type supported")
} else {
fmt.Println("MIME type not supported")
}
}
package main
import (
"fmt"
"io/ioutil"
"github.com/h2non/filetype"
)
func main() {
// Open a file descriptor
file, _ := os.Open("movie.mp4")
// We only have to pass the file header = first 261 bytes
head := make([]byte, 261)
file.Read(head)
if filetype.IsImage(head) {
fmt.Println("File is an image")
} else {
fmt.Println("Not an image")
}
}
package main
import (
"fmt"
"github.com/h2non/filetype"
)
var fooType = filetype.NewType("foo", "foo/foo")
func fooMatcher(buf []byte) bool {
return len(buf) > 1 && buf[0] == 0x01 && buf[1] == 0x02
}
func main() {
// Register the new matcher and its type
filetype.AddMatcher(fooType, fooMatcher)
// Check if the new type is supported by extension
if filetype.IsSupported("foo") {
fmt.Println("New supported type: foo")
}
// Check if the new type is supported by MIME
if filetype.IsMIMESupported("foo/foo") {
fmt.Println("New supported MIME type: foo/foo")
}
// Try to match the file
fooFile := []byte{0x01, 0x02}
kind, _ := filetype.Match(fooFile)
if kind == filetype.Unknown {
fmt.Println("Unknown file type")
} else {
fmt.Printf("File type matched: %s\n", kind.Extension)
}
}
image/jpegimage/pngimage/gifimage/webpimage/x-canon-cr2image/tiffimage/bmpimage/heifimage/vnd.ms-photoimage/vnd.adobe.photoshopimage/vnd.microsoft.iconimage/vnd.dwgvideo/mp4video/x-m4vvideo/x-matroskavideo/webmvideo/quicktimevideo/x-msvideovideo/x-ms-wmvvideo/mpegvideo/x-flvvideo/3gppaudio/midiaudio/mpegaudio/m4aaudio/oggaudio/x-flacaudio/x-wavaudio/amraudio/aacaudio/x-aiffapplication/epub+zipapplication/zipapplication/x-tarapplication/vnd.rarapplication/gzipapplication/x-bzip2application/x-7z-compressedapplication/x-xzapplication/zstdapplication/pdfapplication/vnd.microsoft.portable-executableapplication/x-shockwave-flashapplication/rtfapplication/x-iso9660-imageapplication/octet-streamapplication/postscriptapplication/vnd.sqlite3application/x-nintendo-nes-romapplication/x-google-chrome-extensionapplication/vnd.ms-cab-compressedapplication/vnd.debian.binary-packageapplication/x-unix-archiveapplication/x-compressapplication/x-lzipapplication/x-rpmapplication/x-executableapplication/dicomapplication/mswordapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentapplication/vnd.ms-excelapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.ms-powerpointapplication/vnd.openxmlformats-officedocument.presentationml.presentationapplication/font-woffapplication/font-woffapplication/font-sfntapplication/font-sfntapplication/wasmapplication/vnd.android.dexapplication/vnd.android.deyMeasured using real files.
Environment: OSX x64 i7 2.7 Ghz
BenchmarkMatchTar-8 1000000 1083 ns/op
BenchmarkMatchZip-8 1000000 1162 ns/op
BenchmarkMatchJpeg-8 1000000 1280 ns/op
BenchmarkMatchGif-8 1000000 1315 ns/op
BenchmarkMatchPng-8 1000000 1121 ns/op
MIT - Tomas Aparicio
$ claude mcp add filetype \
-- python -m otcore.mcp_server <graph>