Documentation
¶
Overview ¶
Package mime implements the freedesktop.org Shared MIME-info Database specification in pure Go (CGO-free): the piece a file manager or Spotlight-style finder needs to answer "what type is this file?".
It resolves a canonical MIME type from a file's name (glob matching), from its content (magic sniffing), or from both together following the spec's glob-versus-magic tiebreak. It also resolves aliases and answers subclass ("is-a") queries.
The database is read from the on-disk Shared MIME-info directories ($XDG_DATA_HOME/mime and $XDG_DATA_DIRS/mime), either from the generated files an update-mime-database run produces (globs2, magic, aliases, subclasses) or directly from the source packages/*.xml. Base-directory resolution reuses github.com/adrg/xdg, matching the rest of the go-freedesktop family.
Spec: https://specifications.freedesktop.org/shared-mime-info-spec/latest/
Index ¶
- Constants
- Variables
- func IsSubclassOf(t, parent string) bool
- func TypeByContent(data []byte) string
- func TypeByName(name string) string
- func TypeByNameAndContent(name string, data []byte) string
- func Unalias(t string) string
- type Database
- func (db *Database) AddPackagesDir(dir string) error
- func (db *Database) AddXML(r io.Reader) error
- func (db *Database) Aliases(t string) []string
- func (db *Database) IsSubclassOf(t, parent string) bool
- func (db *Database) Parents(t string) []string
- func (db *Database) TypeByContent(data []byte) string
- func (db *Database) TypeByName(name string) string
- func (db *Database) TypeByNameAndContent(name string, data []byte) string
- func (db *Database) TypesByName(name string) []string
- func (db *Database) Unalias(t string) string
Examples ¶
Constants ¶
const ( // OctetStream is the fallback for content that does not look like text. OctetStream = "application/octet-stream" // PlainText is the fallback for content that looks like text. PlainText = "text/plain" // ZeroSize is the type reported for an empty (zero-byte) file. ZeroSize = "application/x-zerosize" )
Well-known canonical types the spec singles out for fallbacks.
Variables ¶
var ErrBadMagic = errors.New("mime: malformed magic database")
ErrBadMagic reports a malformed binary magic file.
Functions ¶
func IsSubclassOf ¶
IsSubclassOf is IsSubclassOf on the Default database.
func TypeByContent ¶
TypeByContent is TypeByContent on the Default database.
func TypeByName ¶
TypeByName is TypeByName on the Default database.
func TypeByNameAndContent ¶
TypeByNameAndContent is TypeByNameAndContent on the Default database.
Types ¶
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database is a parsed Shared MIME-info database. The zero value is not ready for use; build one with New, Load, or LoadDir. A Database is safe for concurrent reads once fully built.
Example ¶
ExampleDatabase shows a file manager resolving a file's type from its name, its content, and both together, using a small database built from a Shared MIME-info XML package.
package main
import (
"fmt"
"strings"
"github.com/go-freedesktop/mime"
)
func main() {
db := mime.New()
_ = db.AddXML(strings.NewReader(`
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="text/plain"><glob pattern="*.txt"/></mime-type>
<mime-type type="application/pdf">
<glob pattern="*.pdf"/>
<magic priority="50"><match type="string" value="%PDF-" offset="0"/></magic>
</mime-type>
</mime-info>`))
fmt.Println(db.TypeByName("notes.txt"))
fmt.Println(db.TypeByContent([]byte("%PDF-1.7")))
// A file misnamed .txt but whose content sniffs as PDF: here the weak
// (priority < 80) magic does not override the file name.
fmt.Println(db.TypeByNameAndContent("invoice.txt", []byte("%PDF-1.7")))
}
Output: text/plain application/pdf text/plain
func Default ¶
func Default() *Database
Default returns the process-wide database, loaded once from the system Shared MIME-info directories with Load. If loading fails it returns an empty database so lookups degrade to the spec fallbacks rather than panicking.
func Load ¶
Load reads and merges every Shared MIME-info database found on the system: $XDG_DATA_HOME/mime first, then each $XDG_DATA_DIRS/mime, resolved through github.com/adrg/xdg. Missing directories are skipped; a directory that exists but contains malformed data yields an error.
func LoadDir ¶
LoadDir reads a single Shared MIME-info directory (the "mime" directory that holds globs2, magic, aliases, subclasses and packages/). It prefers the generated files; if none are present it falls back to parsing packages/*.xml.
func New ¶
func New() *Database
New returns an empty Database ready to be populated with AddXML, AddPackagesDir, or the loader helpers.
func (*Database) AddPackagesDir ¶
AddPackagesDir parses every *.xml file in dir (a Shared MIME-info packages/ directory) into the database, in sorted file-name order. A missing directory is not an error; a malformed file is.
func (*Database) AddXML ¶
AddXML parses one Shared MIME-info XML document (a <mime-info> element) from r and merges its globs, magic rules, aliases and subclass relations.
func (*Database) Aliases ¶
Aliases returns the alternative type strings declared as aliases of the canonical type t, in the order they were registered.
func (*Database) IsSubclassOf ¶
IsSubclassOf reports whether t is the same as, or a subclass ("is-a") of, parent. It follows declared sub-class-of relations transitively and honours the spec's implicit rules: every non-inode type is a subclass of application/octet-stream, and every text/* type is a subclass of text/plain.
func (*Database) TypeByContent ¶
TypeByContent sniffs data and returns the best matching canonical type. An empty slice yields ZeroSize; if no magic rule matches, the result is PlainText when the data looks like text and OctetStream otherwise.
func (*Database) TypeByName ¶
TypeByName returns the single best canonical type for a file name using glob matching, or "" when no glob matches. Only the file's base name is considered.
func (*Database) TypeByNameAndContent ¶
TypeByNameAndContent combines glob and magic matching following the spec's recommended checking order and its glob-versus-magic tiebreak, and always returns a concrete type (falling back to ZeroSize / PlainText / OctetStream). Pass data==nil to indicate the content is unavailable; an empty non-nil slice means a genuinely zero-byte file.
func (*Database) TypesByName ¶
TypesByName returns every canonical type in the highest-precedence glob tier that matches name, ordered by descending weight. It is empty when nothing matches. Precedence follows the spec: literal file-name matches beat suffix (*.ext) matches beat full fnmatch globs, and among suffix matches the longest matching suffix wins.