mimetype

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2019 License: MIT Imports: 4 Imported by: 1,035

README

mimetype

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

No bindings, all written in pure go

Build Status Documentation Go report card Go report card 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) {...}

When detecting from a ReadSeeker interface, such as os.File, make sure to reset the offset of the reader to the beginning if needed:

_, err = file.Seek(io.SeekStart, 0)

Extend

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

  • create the matching function
    foobar := func(input []byte) bool {
    	return bytes.HasPrefix(input, []byte("foobar"))
    }
    
  • 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 Contributing if you consider the missing mime type should be included in the library by default.

Supported mimes

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

Contributing

See CONTRIBUTING.md.

Documentation

Overview

Package mimetype uses magic number signatures to detect the mime type and extension of a file.

Index

Constants

This section is empty.

Variables

View Source
var (
	Gzip   = NewNode("application/gzip", "gz", matchers.Gzip)
	SevenZ = NewNode("application/x-7z-compressed", "7z", matchers.SevenZ)
	Zip    = NewNode("application/zip", "zip", matchers.Zip, Xlsx, Docx, Pptx, Epub, Jar)
	Tar    = NewNode("application/x-tar", "tar", matchers.Tar)
	Pdf    = NewNode("application/pdf", "pdf", matchers.Pdf)
	Xlsx   = NewNode("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx", matchers.Xlsx)
	Docx   = NewNode("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx", matchers.Docx)
	Pptx   = NewNode("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx", matchers.Pptx)
	Epub   = NewNode("application/epub+zip", "epub", matchers.Epub)
	Jar    = NewNode("application/jar", "jar", matchers.Jar, Apk)
	Apk    = NewNode("application/vnd.android.package-archive", "apk", matchers.False)
	Doc    = NewNode("application/msword", "doc", matchers.Doc)
	Ppt    = NewNode("application/vnd.ms-powerpoint", "ppt", matchers.Ppt)
	Xls    = NewNode("application/vnd.ms-excel", "xls", matchers.Xls)
	Ps     = NewNode("application/postscript", "ps", matchers.Ps)
	Psd    = NewNode("application/x-photoshop", "psd", matchers.Psd)
	Ogg    = NewNode("application/ogg", "ogg", matchers.Ogg)

	Txt = NewNode("text/plain", "txt", matchers.Txt,
		Html, Svg, Xml, Php, Js, Lua, Perl, Python, Json, Rtf, Tcl)
	Xml = NewNode("text/xml; charset=utf-8", "xml", matchers.Xml,
		X3d, Kml, Collada, Gml, Gpx, Tcx)
	Json = NewNode("application/json", "json", matchers.Json)
	Html = NewNode("text/html; charset=utf-8", "html", matchers.Html)
	Php  = NewNode("text/x-php; charset=utf-8", "php", matchers.Php)
	Rtf  = NewNode("text/rtf", "rtf", matchers.Rtf)

	Js     = NewNode("application/javascript", "js", matchers.Js)
	Lua    = NewNode("text/x-lua", "lua", matchers.Lua)
	Perl   = NewNode("text/x-perl", "pl", matchers.Perl)
	Python = NewNode("application/x-python", "py", matchers.Python)
	Tcl    = NewNode("text/x-tcl", "tcl", matchers.Tcl)

	Svg     = NewNode("image/svg+xml", "svg", matchers.Svg)
	X3d     = NewNode("model/x3d+xml", "x3d", matchers.False)
	Kml     = NewNode("application/vnd.google-earth.kml+xml", "kml", matchers.Kml)
	Collada = NewNode("model/vnd.collada+xml", "dae", matchers.Collada)
	Gml     = NewNode("application/gml+xml", "gml", matchers.Gml)
	Gpx     = NewNode("application/gpx+xml", "gpx", matchers.Gpx)
	Tcx     = NewNode("application/vnd.garmin.tcx+xml", "tcx", matchers.Tcx)

	Png  = NewNode("image/png", "png", matchers.Png)
	Jpg  = NewNode("image/jpeg", "jpg", matchers.Jpg)
	Gif  = NewNode("image/gif", "gif", matchers.Gif)
	Webp = NewNode("image/webp", "webp", matchers.Webp)
	Tiff = NewNode("image/tiff", "tiff", matchers.Tiff)
	Bmp  = NewNode("image/bmp", "bmp", matchers.Bmp)
	Ico  = NewNode("image/x-icon", "ico", matchers.Ico)

	Mp3      = NewNode("audio/mpeg", "mp3", matchers.Mp3)
	Flac     = NewNode("audio/flac", "flac", matchers.Flac)
	Midi     = NewNode("audio/midi", "midi", matchers.Midi)
	Ape      = NewNode("audio/ape", "ape", matchers.Ape)
	MusePack = NewNode("audio/musepack", "mpc", matchers.MusePack)
	Wav      = NewNode("audio/wav", "wav", matchers.Wav)
	Aiff     = NewNode("audio/aiff", "aiff", matchers.Aiff)
	Au       = NewNode("audio/basic", "au", matchers.Au)
	Amr      = NewNode("audio/amr", "amr", matchers.Amr)
	AMp4     = NewNode("audio/mp4", "mp4", matchers.AMp4)
	M4a      = NewNode("audio/x-m4a", "m4a", matchers.M4a)

	Mp4       = NewNode("video/mp4", "mp4", matchers.Mp4)
	WebM      = NewNode("video/webm", "webm", matchers.WebM)
	Mpeg      = NewNode("video/mpeg", "mpeg", matchers.Mpeg)
	QuickTime = NewNode("video/quicktime", "mov", matchers.QuickTime)
	ThreeGP   = NewNode("video/3gpp", "3gp", matchers.ThreeGP)
	ThreeG2   = NewNode("video/3gpp2", "3g2", matchers.ThreeG2)
	Avi       = NewNode("video/x-msvideo", "avi", matchers.Avi)
	Flv       = NewNode("video/x-flv", "flv", matchers.Flv)
	Mkv       = NewNode("video/x-matroska", "mkv", matchers.Mkv)

	Class = NewNode("application/x-java-applet; charset=binary", "class", matchers.Class)
	Swf   = NewNode("application/x-shockwave-flash", "swf", matchers.Swf)
	Crx   = NewNode("application/x-chrome-extension", "crx", matchers.Crx)

	Woff  = NewNode("font/woff", "woff", matchers.Woff)
	Woff2 = NewNode("font/woff2", "woff2", matchers.Woff2)

	Wasm = NewNode("application/wasm", "wasm", matchers.Wasm)
)

The list of nodes appended to the Root node

View Source
var Root = NewNode("application/octet-stream", "", matchers.True,
	SevenZ, Zip, Tar, Pdf, Doc, Xls, Ppt, Ps, Psd, Ogg,
	Png, Jpg, Gif, Webp, Tiff, Bmp, Ico,
	Mp3, Flac, Midi, Ape, MusePack, Amr, Wav, Aiff, Au,
	Mpeg, QuickTime, Mp4, WebM, ThreeGP, ThreeG2, Avi, Flv, Mkv, AMp4, M4a,
	Txt, Gzip, Class, Swf, Crx, Woff, Woff2, Wasm,
)

Root is a matcher which passes for any slice of bytes. When a matcher passes the check, the children matchers are tried in order to find a more accurate mime type.

Functions

func Detect

func Detect(in []byte) (mime, extension string)

Detect returns the mime type and extension of the provided byte slice.

func DetectFile

func DetectFile(file string) (mime, extension string, err error)

DetectFile returns the mime type and extension of the provided file.

func DetectReader

func DetectReader(r io.Reader) (mime, extension string, err error)

DetectReader returns the mime type and extension of the byte slice read from the provided reader.

Types

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represents a node in the matchers tree structure. It holds the mime type, the extension and the function to check whether a byte slice has the mime type.

func NewNode

func NewNode(mime, extension string, matchFunc matchFunc, children ...*Node) *Node

NewNode creates a new Node.

func (*Node) Append

func (n *Node) Append(cs ...*Node)

Append adds a new node to the matchers tree. When a node's matching function passes the check, the node's children are also checked in order to find a more accurate mime type for the input.

func (*Node) Extension

func (n *Node) Extension() string

Extension returns the file extension associated with the node.

func (*Node) Mime

func (n *Node) Mime() string

Mime returns the mime type associated with the node.

func (*Node) Tree

func (n *Node) Tree() string

Tree returns a string representation of the matchers tree.

Directories

Path Synopsis
Package matchers holds the matching functions used to find mime types.
Package matchers holds the matching functions used to find mime types.
json
JSON value parser state machine.
JSON value parser state machine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL