名前

Mojolicious::Types - MIMEタイプ

使い方

use Mojolicious::Types;

my $types = Mojolicious::Types->new;
$types->type(foo => 'text/foo');
say $types->type('foo');

説明

Mojolicious::TypesはMIMEタイプを管理します。

appcache -> text/cache-manifest
atom     -> application/atom+xml
bin      -> application/octet-stream
css      -> text/css
gif      -> image/gif
gz       -> application/x-gzip
htm      -> text/html
html     -> text/html;charset=UTF-8
ico      -> image/x-icon
jpeg     -> image/jpeg
jpg      -> image/jpeg
js       -> application/javascript
json     -> application/json;charset=UTF-8
mp3      -> audio/mpeg
mp4      -> video/mp4
ogg      -> audio/ogg
ogv      -> video/ogg
pdf      -> application/pdf
png      -> image/png
rss      -> application/rss+xml
svg      -> image/svg+xml
txt      -> text/plain;charset=UTF-8
webm     -> video/webm
woff     -> font/woff
woff2    -> font/woff2
xml      -> application/xml,text/xml
zip      -> application/zip

属性

Mojolicious::Typesは次の属性を実装しています。

mapping

my $mapping = $types->mapping;
$types      = $types->mapping({png => ['image/png']});

MIMEタイプのマッピング。

メソッド

Mojolicious::TypesMojo::Baseのすべてのメソッドを継承しており、 次の新しいメソッドを実装しています。

content_type

$types->content_type(Mojolicious::Controller->new, {ext => 'json'});

レスポンスヘッダでContent-Typeが指定されていない場合に、 Mojolicious::ControllerのためにMIMEタイプを検出します。 デフォルトは、もし代替のよりよいものが見つからない場合は、txt拡張子のMIMEタイプです。 このメソッドは、実験的であり、警告なしに変更される可能性があります。

以下のオプションが現在利用可能です。

ext
ext => 'json'

MIMEタイプを取得するためのファイル拡張子。

file
file => 'foo/bar.png'

MIMEタイプを取得するためのファイルパス

detect

my $extensions = $types->detect('application/json;q=9');

Acceptヘッダーからファイルの拡張子を検知します。

# 優先順位のついた検地された拡張をリスト
say for @{$types->detect('application/json, text/xml;q=0.1', 1)};

type

my $type = $types->type('png');
$types   = $types->type(png => 'image/png');
$types   = $types->type(json => ['application/json', 'text/x-json']);

ファイル拡張子のためのMIMEタイプの設定と取得。

参考

Mojolicious, Mojolicious::Guides, http://mojolicio.us.

(Mojolicious 8.12を反映。2019年6月14日更新)

関連情報