| 1 | package Catalyst::Plugin::Static::File; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 5 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # ABSTRACT: Serve a specific static file | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 5 | ▶ | use Moose::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 5 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use File::stat; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 5 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 5 | ▶ | use Plack::MIME; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 5 | ▶ | use Plack::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 5 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 5 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 5 | our $VERSION = 'v0.2.5'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 8 | ▶ | sub serve_static_file { | ||||||||||||||||||
|
Subroutine
serve_static_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 22 | 8 | my ( $c, $path, $type ) = @_; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 8 | my $res = $c->res; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 8 | my $abs = File::Spec->rel2abs("$path"); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 8 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Ideally we could let the file open fail when a file does not exist, but this seems to cause the process to | ||||||||||||||||||||
| 31 | # exit in a way that try/catch cannot handle on some systems. We can risk a potential race condition where the | ||||||||||||||||||||
| 32 | # file disappears between the existence check and opening: the worst case is that it would have the same effect | ||||||||||||||||||||
| 33 | # as not checking for file existence. | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 8 | ▶ | die "No such file or directory" unless -e $abs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 7 | ▶ | my $fh = IO::File->new( $abs, "r" ) or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 7 | binmode($fh); | |||||||||||||||||||
| 40 | 7 | Plack::Util::set_io_path( $fh, $abs ); | |||||||||||||||||||
| 41 | 7 | $res->body($fh); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 7 | ▶ | $type //= Plack::MIME->mime_type($abs); | ||||||||||||||||||
|
Condition66%2/3
$type //= "Plack::MIME"->mime_type($abs)
MC/DC0%0/2
$type //= "Plack::MIME"->mime_type($abs)
$type
"Plack::MIME"->mime_type($abs)
Truth table66%2/3
A //= B
A= $typeB= "Plack::MIME"->mime_type($abs)
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 7 | my $headers = $res->headers; | |||||||||||||||||||
| 46 | 7 | $headers->content_type("$type"); | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 7 | my $stat = stat($fh); | |||||||||||||||||||
| 49 | 7 | $headers->content_length( $stat->size ); | |||||||||||||||||||
| 50 | 7 | $headers->last_modified( $stat->mtime ); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | 1 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 1 | my $error = $_; | |||||||||||||||||||
| 56 | 1 | Catalyst::Exception->throw("Unable to open ${abs} for reading: ${error}"); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 8 | }; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 7 | return 1; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 5 | 1; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | __END__ | ||||||||||||||||||||