黒羊庭園

技術系の話題とかその他

MojoliciousでファイルのMIME-typeを追加する方法

Mojoliciousでデフォルトで有効になっているMIME-typeは以下の通り

    atom => 'application/atom+xml',
    bin  => 'application/octet-stream',
    css  => 'text/css',
    gif  => 'image/gif',
    gz   => 'application/gzip',
    htm  => 'text/html',
    html => 'text/html;charset=UTF-8',
    ico  => 'image/x-icon',
    jpeg => 'image/jpeg',
    jpg  => 'image/jpeg',
    js   => 'application/x-javascript',
    json => 'application/json',
    mp3  => 'audio/mpeg',
    png  => 'image/png',
    rss  => 'application/rss+xml',
    svg  => 'image/svg+xml',
    tar  => 'application/x-tar',
    txt  => 'text/plain',
    woff => 'application/x-font-woff',
    xml  => 'text/xml',
    xsl  => 'text/xml',
    zip  => 'application/zip'

Mojolicious::Types.pmより


これ以外のファイル、例えばFLASHの.swfなどは設定されていないので適切なcontent-typeが渡されてきません。

 とは言ってもHTMLのタグとして記述していれば、ブラウザがよしなにしてくれることもあるっぽくて意外と普通に表示できたりします。

 ただ、一部の携帯電話のブラウザなどそこまでお人よしじゃないものもまだまだありますし、ちゃんと動くと保証できませんので正しいcontent-typeを指定した方がいいですね。

 新規にMIME-typeを追加する方法は(例えばFlashのswfを追加する場合)

use Mojolicious::Types;
my $types = Mojolicious::Types->new;
$types->type(swf => 'application/x-shockwave-flash');

 Mojolicious::Liteで使う場合は

app->types->type(swf => 'application/x-shockwave-flash');

関連
Mojolicious::Liteでstaticファイルのpathを変える - 黒羊工房はてな版