lib/Mojolicious/Plugin/DirectoryHandler.pm

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