mimemagic: github.com/zRedShift/mimemagic Index | Files | Directories

package mimemagic

import "github.com/zRedShift/mimemagic"

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


Package Files

doc.go glob.go globs.go magic.go magicsigs.go mediatype.go mediatypes.go namespace.go namespaces.go treemagic.go treemagicsigs.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.
    // Glob prefers MatchGlob in case of a contention.

type MediaType Uses

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 Uses

func Match(data []byte, filename string, preference ...int) MediaType

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 MatchFile Uses

func MatchFile(f *os.File, limAndPref ...int) (MediaType, error)

MatchFile is an *os.File convenience wrapper for MatchReader.

func MatchFilePath Uses

func MatchFilePath(path string, limAndPref ...int) (m MediaType, err error)

MatchFilePath is a file path convenience wrapper for MatchReader.

func MatchGlob Uses

func MatchGlob(filename string) MediaType

MatchGlob determines the MIME type of the file using exclusively its filename.

func MatchMagic Uses

func MatchMagic(data []byte) MediaType

MatchMagic determines the MIME type of the file in byte slice form. For an io.Reader wrapper see MatchReader (blank filename).

func MatchReader Uses

func MatchReader(r io.Reader, filename string, limAndPref ...int) (MediaType, error)

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 Uses

func MatchTreeMagic(path string) (MediaType, error)

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 Uses

func MatchXML(data []byte) MediaType

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 Uses

func MatchXMLReader(r io.Reader, limit int) MediaType

MatchXMLReader is an io.Reader wrapper for MatchXML that can be supplied with a limit on the data to read.

func (MediaType) IsExtension Uses

func (m MediaType) IsExtension(ext string) bool

IsExtension checks if the extension ext is associated with the MIME type. The extension should begin with a leading dot, as in ".html".

func (MediaType) MediaType Uses

func (m MediaType) MediaType() string

MediaType returns the MIME type in the format of the MIME spec.



Package mimemagic imports 9 packages (graph) and is imported by 4 packages. Updated 2018-10-29. Refresh now. Tools for package owners.