| 1 | package App::AutoCRUD::View::Download; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | extends 'App::AutoCRUD::View'; | ||
| 9 | |||
| 10 | use Cwd; | ||
| 11 | use HTTP::Date; | ||
| 12 | use Plack::MIME; | ||
| 13 | |||
| 14 | use namespace::clean -except => 'meta'; | ||
| 15 | |||
| 16 | |||
| 17 | sub render { | ||
| 18 | my ($self, $file, $context) = @_; | ||
| 19 | |||
| 20 | open my $fh, "<:raw", $file or die $!; | ||
| 21 | my ($size, $mtime) = (stat $fh)[7, 9]; | ||
| 22 | |||
| 23 | # code mostly borrowed from App::File | ||
| 24 | my $content_type = Plack::MIME->mime_type($file) || 'text/plain'; | ||
| 25 | Plack::Util::set_io_path($fh, Cwd::realpath($file)); | ||
| 26 | |||
| 27 | # TODO : SUPPORT CONDITIONAL GET | ||
| 28 | |||
| 29 | return [ | ||
| 30 | 200, | ||
| 31 | [ | ||
| 32 | 'Content-Type' => $content_type, | ||
| 33 | 'Content-Length' => $size, | ||
| 34 | 'Last-Modified' => HTTP::Date::time2str($mtime), | ||
| 35 | ], | ||
| 36 | $fh, | ||
| 37 | ]; | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | |||
| 44 | __END__ |