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
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 )
MediaType stores all the parsed values of a MIME type within a shared-mime-info package.
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.
MatchFile is an *os.File convenience wrapper for MatchReader.
MatchFilePath is a file path convenience wrapper for MatchReader.
MatchGlob determines the MIME type of the file using exclusively its filename.
MatchMagic determines the MIME type of the file in byte slice form. For an io.Reader wrapper see MatchReader (blank filename).
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.
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.
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.
MatchXMLReader is an io.Reader wrapper for MatchXML that can be supplied with a limit on the data to read.
IsExtension checks if the extension ext is associated with the MIME type. The extension should begin with a leading dot, as in ".html".
MediaType returns the MIME type in the format of the MIME spec.