[perl] [Catalyst] [download]

まあ、メモ。

ああ、また、最近言及したキーワードのhogeがでかくなった。orz

sub download : Local {
     my ( $self , $c ) = @_;

     my $file_name    = $c->req->param("file_name");
     my $fh           = IO::File->new($file_name,'r');
     my $body         = join('',<$fh>);

     $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
     $c->res->content_type($self->_content_type_selector($file_name));
     $c->res->body($body);
}

sub _content_type_selector {
    my ($self, $file_name )  = @_;

    my %content_types = (
        'txt'  => 'text/plain',
        'gif'  => 'image/gif',
        'jpg'  => 'image/jpeg',
    );

    if( $file_name =~ /\.(.*)$/i ) {
        my $ext = lc $1;
        return $content_types{$ext};
    }
    else {
        return ;
    }
}

http://search.cpan.org/~jrockway/Catalyst-Manual-5.701003/lib/Catalyst/Manual/Cookbook.pod
http://www.atmarkit.co.jp/fnetwork/rensai/netpro03/content-type.html
http://www.phpbook.jp/pear/pear_http_download/index5.html
http://www.cadias.addzest.com/autopc/support/news/030602.cfm