Marc Lehmann > PApp > PApp::MimeType

Download:
PApp-2.0.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 2   Source  

NAME ^

PApp::MimeType - analyze and normalize mimetypes and extensions

SYNOPSIS ^

 use PApp::MimeType;

 my $mt = (PApp::MimeType::by_extension "jpg")->mimetype;

DESCRIPTION ^

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.

Lookup Functions

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 $file_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.

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;
PApp::MimeType::by_filename $path

Like extension, but strips the filename part away first.

PApp::MimeType::by_mimetype $mimetype

Return a PApp::MimeType object by it's mimetype (e.g. "image/jpeg"). Return undef if none could be found.

Methods

PApp::MimeType objects are immutable, and support a number of methods.

$type = $mt->mimetype

Return the normalized mimetype as a string (e.g. "image/pjpeg" objects would return "image/jpeg").

@types = $mt->mimetypes

Return all possible matching mimetypes. The default (suggested) mimetype is returned first.

$extension = $mt->extension

Return the default extension to use (the most common one) for this mimetype.

@extensions = $mt->extensions

Return all extensions possibly used by this mimetype, with more common ones first.

Database Functions

The mime database is initialized on demand form a default file. If you want to overwrite or augment it, use the following functions:

clear_mimedb

Clears the internal mimetypes database

load_mimedb [$path]

Appends the mime type data in the given file to the internal mimetypes database. If $path is omitted, uses the system mimedb.

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

SEE ALSO ^

PApp.

AUTHOR ^

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/
syntax highlighting: