blib/lib/Mojolicious/Plugin/Directory.pm

filter: Statementstmt 100% 96 / 96 Branchbran 91.6% 33 / 36 Conditioncond 72.4% 21 / 29 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 100% 15 / 15 Podpod 16.6% 1 / 6 totaltotal 87.4% 174 / 199 CC 33 SCAR 17.6
CC
33
Cov
93%
__ANON__
9.0
render_indexes
9.0
locate_index
7.1
CRAP 5.8
↑ Summary
1 package Mojolicious::Plugin::Directory;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 our $VERSION = '0.14';
5  
6 8 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use DirHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Mojo::Base qw{ Mojolicious::Plugin };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Mojolicious::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Stolen from Plack::App::Direcotry
14 8 my $dir_page = <<'PAGE';
15 <html><head>
16 <title>Index of <%= $cur_path %></title>
17 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
18 <style type='text/css'>
19 table { width:100%%; }
20 .name { text-align:left; }
21 .size, .mtime { text-align:right; }
22 .type { width:11em; }
23 .mtime { width:15em; }
24 </style>
25 </head><body>
26 <h1>Index of <%= $cur_path %></h1>
27 <hr />
28 <table>
29 <tr>
30 <th class='name'>Name</th>
31 <th class='size'>Size</th>
32 <th class='type'>Type</th>
33 <th class='mtime'>Last Modified</th>
34 </tr>
35 % for my $file (@$files) {
36 <tr><td class='name'><a href='<%= $file->{url} %>'><%== $file->{name} %></a></td><td class='size'><%= $file->{size} %></td><td class='type'><%= $file->{type} %></td><td class='mtime'><%= $file->{mtime} %></td></tr>
37 % }
38 </table>
39 <hr />
40 </body></html>
41 PAGE
42  
43 8 my $types = Mojolicious::Types->new;
44  
45 7 ▶sub register {
Subroutine register: called CC=4 SCAR=13.9
Pod documented
46 7 my $self = shift;
47 7 my ( $app, $args ) = @_;
48  
49 7 ▶ my $root = Mojo::Home->new( $args->{root} || Cwd::getcwd );
Condition66%2/3
$args->{'root'} || Cwd::getcwd()
l!l&&r!l&&!r
610
MC/DC0%0/2
$args->{'root'} || Cwd::getcwd()
$args->{'root'} Cwd::getcwd()
Truth table66%2/3
A || B
A= $args->{'root'}B= Cwd::getcwd()
ABresult
1X1
011
000
50 7 my $handler = $args->{handler};
51 7 my $index = $args->{dir_index};
52 7 ▶ my $auto_index = $args->{auto_index} // 1;
Condition100%2/2
$args->{"auto_index"} // 1
l!l
16
MC/DC100%1/1
$args->{"auto_index"} // 1
$args->{"auto_index"}
Truth table100%2/2
A
A= $args->{"auto_index"}
Aresult
11
00
53 7 my $json = $args->{json};
54 7 ▶ $dir_page = $args->{dir_page} if ( $args->{dir_page} );
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"dir_page"} 1 6 7
55  
56 $app->hook(
57 21 ▶ before_dispatch => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
58 21 my $c = shift;
59 21 ▶ return render_file( $c, $root, $handler ) if ( -f $root->to_string() );
Branch: 1 branch
BranchTrueFalseTotal
if -f $root->to_string 2 19 21
60 19 my $path = $root->rel_file( Mojo::Util::url_unescape( $c->req->url->path ) );
61 19 ▶ if ( -f $path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path) { } 9 10 19
elsif (-d $path) { } 10 0 10
62 9 render_file( $c, $path, $handler );
63 }
64 elsif ( -d $path ) {
65 10 ▶ if ( $index && ( my $index_path = locate_index( $index, $path ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index and my $index_path = locate_index($index, $path)) 1 9 10
Condition66%2/3
$index and my $index_path = locate_index($index, $path)
!ll&&!rl&&r
901
MC/DC50%1/2
$index and my $index_path = locate_index($index, $path)
$index my $index_path = locate_index($index, $path)
Truth table66%2/3
A and B
A= $indexB= my $index_path = locate_index($index, $path)
ABresult
0X0
100
111
66 1 return render_file( $c, $index_path, $handler );
67 }
68  
69 9 ▶ if ( $c->req->url->path ne '/' && ! $c->req->url->path->trailing_slash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->url->path ne "/" and not $c->req->url->path->trailing_slash) 1 8 9
Condition100%3/3
$c->req->url->path ne "/" and not $c->req->url->path->trailing_slash
!ll&&!rl&&r
711
MC/DC100%2/2
$c->req->url->path ne "/" and not $c->req->url->path->trailing_slash
$c->req->url->path ne "/" not $c->req->url->path->trailing_slash
Truth table100%3/3
A and not(B)
A= $c->req->url->path ne "/"B= not $c->req->url->path->trailing_slash
ABresult
0X0
100
111
70 1 $c->redirect_to($c->req->url->path->trailing_slash(1));
71 1 return;
72 }
73  
74 8 ▶ render_indexes( $c, $path, $json ) unless not $auto_index;
Branch: 1 branch
BranchTrueFalseTotal
unless not $auto_index 7 1 8
75 }
76 },
77 7 );
78 7 return $app;
79 }
80  
81 1 ▶sub locate_index {
Subroutine locate_index: called CC=6 SCAR=19.6
Pod undocumented
82 1 ▶ my $index = shift || return;
Condition50%1/2
shift() || (return)
l!l
10
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
83 1 ▶ my $dir = shift || Cwd::getcwd;
Condition33%1/3
shift() || Cwd::getcwd()
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || Cwd::getcwd()
shift() Cwd::getcwd()
Truth table33%1/3
A || B
A= shift()B= Cwd::getcwd()
ABresult
1X1
011
000
84 1 my $root = Mojo::Home->new($dir);
85 1 ▶ $index = ( ref $index eq 'ARRAY' ) ? $index : ["$index"];
Branch: 1 branch
BranchTrueFalseTotal
ref $index eq 'ARRAY' ? : 1 0 1
86 1 for (@$index) {
87 1 my $path = $root->rel_file($_);
88 1 ▶ return $path if ( -e $path );
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 1 0 1
89 }
90 }
91  
92 12 ▶sub render_file {
Subroutine render_file: called CC=4 SCAR=13.9
Pod undocumented
93 12 my $c = shift;
94 12 my $path = shift;
95 12 my $handler = shift;
96 12 ▶ $handler->( $c, $path ) if ( ref $handler eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $handler eq "CODE" 9 3 12
97 12 ▶ return if ( $c->tx->res->code );
Branch: 1 branch
BranchTrueFalseTotal
if $c->tx->res->code 9 3 12
98 3 my $data = Mojo::File::slurp($path);
99 3 ▶ $c->render( data => $data, format => get_ext($path) || 'txt' );
Condition50%1/2
get_ext($path) || 'txt'
l!l
30
MC/DC0%0/1
get_ext($path) || 'txt'
get_ext($path)
Truth table50%1/2
A
A= get_ext($path)
Aresult
11
00
100 }
101  
102 7 ▶sub render_indexes {
Subroutine render_indexes: called CC=9 SCAR=22.0
Pod undocumented
103 7 my $c = shift;
104 7 my $dir = shift;
105 7 my $json = shift;
106  
107 7 ▶ my @files =
Branch: 1 branch
BranchTrueFalseTotal
$c->req->url->path eq '/' ? : 6 1 7
108 ( $c->req->url->path eq '/' )
109 ? ()
110 : ( { url => '../', name => 'Parent Directory', size => '', type => '', mtime => '' } );
111 7 my $children = list_files($dir);
112  
113 7 my $cur_path = Encode::decode_utf8( Mojo::Util::url_unescape( $c->req->url->path ) );
114 7 for my $basename ( sort { $a cmp $b } @$children ) {
115 69 my $file = "$dir/$basename";
116 69 my $url = Mojo::Path->new($cur_path)->trailing_slash(0);
117 69 push @{ $url->parts }, $basename;
118  
119 69 my $is_dir = -d $file;
120 69 my @stat = stat _;
121 69 ▶ if ($is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dir) 10 59 69
122 10 $basename .= '/';
123 10 $url->trailing_slash(1);
124 }
125  
126 69 ▶ my $mime_type =
Branch: 1 branch
BranchTrueFalseTotal
$is_dir ? : 10 59 69
Condition100%4/4
$types->type(get_ext($file) || "txt") or "text/plain"
l!l
1445
get_ext($file) || 'txt'
l!l
536
MC/DC100%1/1
$types->type(get_ext($file) || "txt") or "text/plain"
$types->type(get_ext($file) || "txt")
MC/DC100%1/1
get_ext($file) || 'txt'
get_ext($file)
Truth table100%2/2
A
A= $types->type(get_ext($file) || "txt")
Aresult
11
00
Truth table100%2/2
A
A= get_ext($file)
Aresult
11
00
127 $is_dir
128 ? 'directory'
129 : ( $types->type( get_ext($file) || 'txt' ) || 'text/plain' );
130 69 my $mtime = Mojo::Date->new( $stat[9] )->to_string();
131  
132 69 ▶ push @files, {
Condition50%1/2
$stat[7] || 0
l!l
690
MC/DC0%0/1
$stat[7] || 0
$stat[7]
Truth table50%1/2
A
A= $stat[7]
Aresult
11
00
133 url => $url,
134 name => $basename,
135 size => $stat[7] || 0,
136 type => $mime_type,
137 mtime => $mtime,
138 };
139 }
140  
141 7 my $any = { inline => $dir_page, files => \@files, cur_path => $cur_path };
142 7 ▶ if ($json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json) { } 2 5 7
143 2 $c->respond_to(
144 json => { json => encode_json(\@files) },
145 any => $any,
146 );
147 }
148 else {
149 5 $c->render( %$any );
150 }
151 }
152  
153 62 ▶sub get_ext {
Subroutine get_ext: called CC=2 SCAR=6.9
Pod undocumented
154 62 ▶ $_[0] =~ /\.([0-9a-zA-Z]+)$/ || return;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /\.([0-9a-zA-Z]+)$/u or return 56 6 62
155 56 return lc $1;
156 }
157  
158 7 ▶sub list_files {
Subroutine list_files: called CC=5 SCAR=16.1
Pod undocumented
159 7 ▶ my $dir = shift || return [];
Condition50%1/2
shift() || (return [])
l!l
70
MC/DC0%0/1
shift() || (return [])
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
160 7 my $dh = DirHandle->new($dir);
161 7 my @children;
162 7 ▶ while ( defined( my $ent = $dh->read ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ent = $dh->read)) 83 7 90
163 83 ▶ next if $ent eq '.' or $ent eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $ent eq "." or $ent eq ".." 14 69 83
Condition100%3/3
$ent eq "." or $ent eq ".."
l!l&&r!l&&!r
7769
MC/DC100%2/2
$ent eq "." or $ent eq ".."
$ent eq "." $ent eq ".."
Truth table100%3/3
A or B
A= $ent eq "."B= $ent eq ".."
ABresult
1X1
011
000
164 69 push @children, Encode::decode_utf8($ent);
165 }
166 7 return [ @children ];
167 }
168  
169 8 1;
170  
171 __END__
↑ Summary