File::MimeInfo - Determine file type from the file name
use File::MimeInfo; my $mime_type = mimetype($file); my $mime_type2 = mimetype('test.png');
This module can be used to determine the mime type of a file. It tries to implement the freedesktop specification for a shared MIME database.
For this module shared-mime-info-spec 0.13 was used.
This package only uses the globs file. No real magic checking is used. The File::MimeInfo::Magic package is provided for magic typing.
This module loads the various data files when needed. If you want to hash data earlier see the
rehash methods below.
mimetype is exported by default. The methods
mimetype_isa can be exported on demand.
Simple constructor to allow Object Oriented use of this module. If you want to use this, include the package as
use File::MimeInfo ();to avoid importing sub
Returns a mimetype string for
$file, returns undef on failure.
This method bundles
If these methods are unsuccessful the file is read and the mimetype defaults to 'text/plain' or to 'application/octet-stream' when the first ten chars of the file match ascii control chars (white spaces excluded). If the file doesn't exist or isn't readable
Returns a mimetype in the 'inode' namespace or undef when the file is actually a normal file.
Returns a mimetype string for
$filebased on the filename and filename extensions. Returns undef on failure. The file doesn't need to exist.
Behaviour in list context (wantarray) is unspecified and will change in future releases.
This method decides whether a file is binary or plain text by looking at the first few bytes in the file. Used to decide between "text/plain" and "application/octet-stream" if all other methods have failed.
The spec states that we should check for the ascii control chars and let higher bit chars pass to allow utf8. We try to be more intelligent using perl utf8 support.
In list context, returns the list of filename extensions that map to the given mimetype. In scalar context, returns the first extension that is found in the database for this mimetype.
Returns a description of this mimetype as supplied by the mime info database. You can specify a language with the optional parameter
$lang, this should be the two letter language code used in the xml files. Also you can set the global variable
$File::MimeInfo::LANGto specify a language.
This method returns undef when no xml file was found (i.e. the mimetype doesn't exist in the database). It returns an empty string when the xml file doesn't contain a description in the language you specified.
Currently no real xml parsing is done, it trusts the xml files are nicely formatted.
Returns the canonical mimetype for a given mimetype. Deprecated mimetypes are typically aliased to their canonical variants. This method only checks aliases, doesn't check whether the mimetype exists.
Use this method as a filter when you take a mimetype as input.
When give only one argument this method returns a list with mimetypes that are parent classes for this mimetype.
When given two arguments returns true if the second mimetype is a parent class of the first one.
This method checks the subclasses table and applies a few rules for implicit subclasses.
Check if there are mimeinfo database files available; returns 1 on success. If you don't have the shared-mime-info package installed or not in the PATH or
@File::MimeInfo::DIRSdoes not contain database directories, you will not get the successful reply.
New in version 0.30.
Rehash the data files. Glob information is preparsed when this method is called.
If you want to by-pass the XDG basedir system you can specify your database directories by setting
@File::MimeInfo::DIRS. But normally it is better to change the XDG basedir environment variables.
Rehashes the mime/aliases files.
Rehashes the mime/subclasses files.
This module throws an exception when it can't find any data files, when it can't open a data file it found for reading or when a subroutine doesn't get enough arguments. In the first case you either don't have the freedesktop mime info database installed, or your environment variables point to the wrong places, in the second case you have the database installed, but it is broken (the mime info database should logically be world readable).
Make an option for using some caching mechanism to reduce init time.
describe() use real xml parsing ?
Perl versions prior to 5.8.0 do not have the ':utf8' IO Layer, thus for the default method and for reading the xml files utf8 is not supported for these versions.
Since it is not possible to distinguish between encoding types (utf8, latin1, latin2 etc.) in a straightforward manner only utf8 is supported (because the spec recommends this).
This module does not yet check extended attributes for a mimetype. Patches for this are very welcome.
This module uses the FreeDesktop.org shared mime info database. On your desktop linux this is typically pre-installed so it's not a problem. On your server you can install the shared-mime-info package via apt or dnf or apk or whatnot.
To install on macOS, you can install it like this:
brew install shared-mime-info
Jaap Karssenberg <firstname.lastname@example.org> Maintained by Michiel Beijen <email@example.com>
Copyright (c) 2003, 2012 Jaap G Karssenberg. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
- freedesktop specifications used
- freedesktop mime database