PApp::MimeType - analyze and normalize mimetypes and extensions
use PApp::MimeType; my $mt = (PApp::MimeType::by_extension "jpg")->mimetype;
Looks up mime types and file extensions, and gives hints which file extension would be most commonly used.
All mimetypes and extensions returned by this module are in lowercase. Matches done by this module are done in a case-independent-manner.
These functions look up (existing) mimetype objects and return it. Watch out, they are not constructors, so either import them to your namespace or call them like functions (PApp::MimeType::by_extension).
PApp::MimeType::by_extension
Return a PApp::MimeType object by guessing from the file extension (leading dots are stripped). If no entry could be found for that specific extension, returns undef.
PApp::MimeType
To get a guarenteed mimetype for any file, use something like this:
my $content_type = (PApp::MimeType::by_extension $ext or PApp::MimeType::by_mimetype "application/octet-stream") ->mimetype;
Like extension, but strips the filename part away first.
extension
Return a PApp::MimeType object by it's mimetype (e.g. "image/jpeg"). Return undef if none could be found.
undef
PApp::MimeType objects are immutable, and support a number of methods.
Return the normalized mimetype as a string (e.g. "image/pjpeg" objects would return "image/jpeg").
Return all possible matching mimetypes. The default (suggested) mimetype is returned first.
Return the default extension to use (the most common one) for this mimetype.
Return all extensions possibly used by this mimetype, with more common ones first.
The mime database is initialized on demand form a default file. If you want to overwrite or augment it, use the following functions:
Clears the internal mimetypes database
Appends the mime type data in the given file to the internal mimetypes database. If $path is omitted, uses the system mimedb.
$path
The format of the mime database file is similar (but not identical) to the mime.types file used by many servers:
MIMEDB := LINE* LINE := ( EMPTY | MIMERECORD ) COMMENT? NL COMMENT := '#' NON-NL* EMPTY := WS* MIMERECORD := MIMETYPES EXTENSIONS MIMETYPES := MIMETYPE ( ',' MIMETYPE )* EXTENSIONS := EXTENSION ( WS* EXTENSION )* EXTENSION := NON-WS-NON-DOT
Mimetypes and extensions are sorted in the order of most-common ot least-common.
Here is a simple example for text/plain
text/plain txt asc
Here is a more complicated example for image/jpeg, which also covers the wrong but commonly in use (MICROSOFT, DIE DIE DIE) pjpeg-type.
image/jpeg,image/pjpeg jpg jpeg jpe pjpg pjpeg
PApp.
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/
To install PApp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PApp
CPAN shell
perl -MCPAN -e shell install PApp
For more information on module installation, please visit the detailed CPAN module installation guide.