Documentation ¶
Overview ¶
Package mimemagic implements MIME sniffing using pre-compiled glob patterns, magic number signatures, xml document namespaces, and tree magic for mounted volumes, generated from the XDG shared-mime-info database.
To generate your own database simply remove the leading space, point to the directory with freedesktop.org package files (freedesktop.org.xml, if it exists, is always processed first and Override.xml is always processed last), and run go generate:
go:generate go run github.com/zRedShift/mimemagic/cmd/parser /usr/share/mime/packages
To use the default freedesktop.org.xml file provided in this package:
go:generate go run github.com/zRedShift/mimemagic/cmd/parser cmd/parser
globs.go is generated unformatted so it's a good idea to run this for your OCD
go:generate go fmt globs.go
Index ¶
- Constants
- type MediaType
- func Match(data []byte, filename string, preference ...int) MediaType
- func MatchFile(f *os.File, limAndPref ...int) (MediaType, error)
- func MatchFilePath(path string, limAndPref ...int) (m MediaType, err error)
- func MatchGlob(filename string) MediaType
- func MatchMagic(data []byte) MediaType
- func MatchReader(r io.Reader, filename string, limAndPref ...int) (MediaType, error)
- func MatchTreeMagic(path string) (MediaType, error)
- func MatchXML(data []byte) MediaType
- func MatchXMLReader(r io.Reader, limit int) MediaType
Constants ¶
const ( // Default behaviour relies on MatchGlob if it returns a sole // match, else it defers to the first magic match. Default = iota // Magic prefers MatchMagic in case of a contention. Magic // Glob prefers MatchGlob in case of a contention. Glob )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MediaType ¶
type MediaType struct {
Media, Subtype, Comment, Acronym, ExpandedAcronym, Icon, GenericIcon string
Alias, SubClassOf, Extensions []string
// contains filtered or unexported fields
}
MediaType stores all the parsed values of a MIME type within a shared-mime-info package.
func Match ¶
Match determines the MIME type of the file in a byte slice form with a given filename. Anonymous buffers should use MatchMagic. Preference is an optional value that allows to prioritize glob/magic matching in case of a contention. Contention is when both magic and glob matches are found, but they can't be reconciled via aliases or subclasses.
func MatchFilePath ¶
MatchFilePath is a file path convenience wrapper for MatchReader.
func MatchMagic ¶
MatchMagic determines the MIME type of the file in byte slice form. For an io.Reader wrapper see MatchReader (blank filename).
func MatchReader ¶
MatchReader is an io.Reader wrapper for Match that can be supplied with a filename, a limit on the data to read and whether to prefer any of the matching methods in case of a contention. Negative or non-existent values of limit will read the file up until the longest magic signature in the database.
func MatchTreeMagic ¶
MatchTreeMagic determines if the path or the directory of the file supplied in the path matches any common mounted volume signatures and returns their x-content MIME type. Return inode/directory MediaType in the case of a negative identification for a directory, and application/octet-stream in the case of a file.
func MatchXML ¶
MatchXML determines the MIME type of the xml file in a byte slice form. Returns application/octet-stream in case the file isn't a valid xml and application/xml if the identification comes back negative.
func MatchXMLReader ¶
MatchXMLReader is an io.Reader wrapper for MatchXML that can be supplied with a limit on the data to read.
func (MediaType) IsExtension ¶
IsExtension checks if the extension ext is associated with the MIME type. The extension should begin with a leading dot, as in ".html".