/mimetype

A golang library for detecting mime types and extensions based on magic numbers

Primary LanguageGoMIT LicenseMIT

mimetype

A package for detecting mime types and extensions based on magic numbers

No bindings, all written in pure go

Build Status Documentation License

Install

go get github.com/gabriel-vasile/mimetype

Use

The library exposes three functions you can use in order to detect a file type. See Godoc for full reference.

func Detect(in []byte) (mime, extension string) {...}
func DetectReader(r io.Reader) (mime, extension string, err error) {...}
func DetectFile(file string) (mime, extension string, err error) {...}

Extend

If, for example, you need to detect the "text/foobar" mime, for text files containing the string "foobar" as their first line:

  • create the matching function
    foobar := func(input []byte) bool {
    	return bytes.HasPrefix(input, []byte("foobar\n"))
    }
  • create the mime type node
    foobarNode := mimetype.NewNode("text/foobar", "fbExt", foobar)
  • append the new node in the tree
    mimetype.Txt.Append(foobarNode)
  • detect
    mime, extension := mimetype.Detect([]byte("foobar\nfoo foo bar"))

See TestAppend for a working example. See Contribute if you consider the missing mime type should be included in the library by default.

Supported mimes

Application

7Z, Zip, Pdf, Xlsx, Docx, Pptx, Epub, Jar, Apk, Doc, Ppt, Xls, Ps, Psd, Ogg, JavaScript, Python, Lua, Perl

Image

Png, Jpg, Gif, Webp Tiff

Audio

Mp3, Flac, Midi, Ape, MusePack, Wav, Aiff, Au, Amr

Video

Mp4, WebM, Mpeg, Quicktime, ThreeGP, Avi, Flv, Mkv

Text

Txt, Html, Xml, Php, Json

Structure

mimetype uses an hierarchical structure to keep the matching functions. This reduces the number of calls needed for detecting the file type. The reason behind this choice is that there are file formats used as containers for other file formats. For example, Microsoft office files are just zip archives, containing specific metadata files.

structure

Contribute

See CONTRIBUTING.md