mimetype – github.com/gabriel-vasile/mimetype Index | Files | Directories

package mimetype

import "github.com/gabriel-vasile/mimetype"

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

Index

Variables

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)
	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, Xml, Php, Js, Lua, Perl, Python, Json, Rtf)
	Xml = NewNode("text/xml; charset=utf-8", "xml", matchers.Xml,
		Svg, X3d, Kml, Collada, Gml, Gpx)
	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)

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

	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)

	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/3gp", "3gp", matchers.ThreeGP)
	Avi       = NewNode("video/x-msvideo", "avi", matchers.Avi)
	Flv       = NewNode("video/x-flv", "flv", matchers.Flv)
	Mkv       = NewNode("video/x-matroska", "mkv", matchers.Mkv)
)

The list of nodes appended to the Root node

var Root = NewNode("application/octet-stream", "", matchers.True,
	SevenZ, Zip, 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, Avi, Flv, Mkv,
	Txt, Gzip,
)

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

Source Files

mime.go node.go tree.go

Directories

PathSynopsis
matchersPackage matchers holds the matching functions used to find mime types.
matchers/jsonJSON value parser state machine.
Version
v0.1.0
Published
Jan 30, 2019
Platform
windows/amd64
Imports
4 packages
Last checked
4 hours ago

Tools for package owners.