[][src]Crate xdg_mime

SharedMimeInfo allows to look up the MIME type associated to a file name or to the contents of a file, using the Freedesktop.org Shared MIME database specification.

Alongside the MIME type, the Shared MIME database contains other ancillary information, like the icon associated to the MIME type; the aliases for a given MIME type; and the various sub-classes of a MIME type.

Loading the Shared MIME database

The SharedMimeInfo type will automatically load all the instances of shared MIME databases available in the following directories, in this specified order:

  • $XDG_DATA_HOME/mime
    • if XDG_DATA_HOME is unset, this corresponds to $HOME/.local/share/mime
  • $XDG_DATA_DIRS/mime
    • if XDG_DATA_DIRS is unset, this corresponds to /usr/local/share/mime and /usr/share/mime

For more information on the XDG_DATA_HOME and XDG_DATA_DIRS environment variables, see the XDG base directory specification.

The MIME data in each directory will be coalesced into a single database.

Retrieving the MIME type of a file

If you want to know the MIME type of a file, you typically have two options at your disposal:

  • guess from the file name
  • use an appropriately sized chunk of the file contents and perform "content sniffing"

The former step does not come with performance penalties, or even requires the file to exist in the first place; the latter can be an arbitrarily expensive operation to perform. It is recommended to always guess the MIME type from the file name first, and only use content sniffing lazily and, possibly, asynchronously.

Structs

SharedMimeInfo

The shared MIME info database