NAME

Media::Type::Simple - MIME Types and their file extensions

SYNOPSIS

use Media::Type::Simple;

$type = type_from_ext("jpg");        # returns "image/jpeg"

$ext  = ext_from_type("text/plain"); # returns "txt"

DESCRIPTION

This package gives a simple functions for obtaining common file extensions from media types, and from obtaining media types from file extensions.

It is also relaxed with respect to having multiple media types associated with a file extension, or multiple extensions associated with a media type, and it includes media types for encodings such as gzip. It is defined this way in the default data, but this does not meet your needs, then you can have it use a system file (e.g. /etc/mime.types) or custom data.

By default, there is a functional interface, although you can also use an object-oriented interface. (Different objects will not share the same data.)

Methods

new
$o = Media::Type::Simple->new;

Creates a new object. You may optionally give it a filehandle of a file with system Media information, e.g.

open $f, "/etc/mime.types";
$o =  Media::Type::Simple->new( $f );
add_types_from_file
$o->add_types_from_file( $filehandle );

Imports types from a file. Called by "new" when a filehandle is specified.

is_type
if (is_type("text/plain")) { ... }

if ($o->is_type("text/plain")) { ... }

Returns a true value if the type is defined in the system.

Note that a true value does not necessarily indicate that the type has file extensions associated with it.

alt_types
@alts = alt_types("image/jpeg");

@alts = $o->alt_types("image/jpeg");

Returns alternative or related Media types that are defined in the system For instance,

alt_types("model/dwg")

returns the list

image/vnd.dwg
ext_from_type
$ext  = ext_from_type( $type );

@exts = ext_from_type( $type );

$ext  = $o->ext_from_type( $type );

@exts = $o->ext_from_type( $type );

Returns the file extension(s) associated with the given Media type. When called in a scalar context, returns the first extension from the list.

The order of extensions is based on the order that they occur in the source data (either the default here, or the order added using "add_types_from_file" or calls to "add_type").

ext3_from_type

Like "ext_from_type", but only returns file extensions under three characters long.

is_ext
if (is_ext("jpeg")) { ... }

if ($o->is_ext("jpeg")) { ... }

Returns a true value if the extension is defined in the system.

type_from_ext
$type  = type_from_ext( $extension );

@types = type_from_ext( $extension );

$type  = $o->type_from_ext( $extension );

@types = $o->type_from_ext( $extension );

Returns the Media type(s) associated with the extension. When called in a scalar context, returns the first type from the list.

The order of types is based on the order that they occur in the source data (either the default here, or the order added using "add_types_from_file" or calls to "add_type").

add_type
$o->add_type( $type, @extensions );

Add a type to the system, with an optional list of extensions.

clone
$c = $o->clone;

Returns a clone of a Media::Type::Simple object. This allows you to add new types via "add_types_from_file" or "add_type" without affecting the original.

This can only be used in the object-oriented interface.

REVISION HISTORY

For a detailed history see the Changes file included in this distribution.

SEE ALSO

The MIME::Types module has a similar functionality, but with a more complex interface.

LWP::MediaTypes will guess the media type from a file extension, attempting to use the ~/.media.types file.

An "official" list of Media Types can be found at http://www.iana.org/assignments/media-types.

AUTHOR

Robert Rothenberg <rrwo at cpan.org>

Contributors

Russell Jenkins
Martin McGrath

Acknowledgements

Some of the code comes from self module (by Kang-min Liu). The data for the media types is based on the Debian mime-support package, http://packages.debian.org/mime-support, although with many changes from the original.

Suggestions and Bug Reporting

Feedback is always welcome. Please use the CPAN Request Tracker at http://rt.cpan.org to submit bug reports.

The git repository for this module is at https://github.com/robrwo/Media-Types-Simple.

COPYRIGHT & LICENSE

Copyright 2009-2015 Robert Rothenberg, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.