| 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
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
| |||||||||||||||||||||||||||||||
| 60 | 10 | my $handler = $args->{handler}; | |||||||||||||||||||||||||||||
| 61 | 10 | ▶ | my $auto_index = $args->{auto_index} // 1; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"auto_index"} // 1
MC/DC100%1/1
$args->{"auto_index"} // 1
$args->{"auto_index"}
Truth table100%2/2
A
A= $args->{"auto_index"}
| |||||||||||||||||||||||||||||||
| 62 | 10 | my $json = $args->{json}; | |||||||||||||||||||||||||||||
| 63 | 10 | ▶ | my $delivery_path = $args->{delivery_path} || $root; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$args->{'delivery_path'} || $root
MC/DC0%0/2
$args->{'delivery_path'} || $root
$args->{'delivery_path'}
$root
Truth table33%1/3
A || B
A= $args->{'delivery_path'}B= $root
| |||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | $dir_page = $args->{dir_page} if ($args->{dir_page}); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 37 | ▶ | if ($req_path =~ m/^\/$delivery_path/) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 92 | 8 | ▶ | render_indexes($c, $path, $json) unless not $auto_index; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | 8 | return; | |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||
| 95 | 10 | ▶ | if (-f $path) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 112 | 9 | ▶ | return if ($c->tx->res->code); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 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'
MC/DC100%1/1
get_ext($path) || 'txt'
get_ext($path)
Truth table100%2/2
A
A= get_ext($path)
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 135 | 2 | $basename .= '/'; | |||||||||||||||||||||||||||||
| 136 | 2 | $url->trailing_slash(1); | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||
| 139 | 15 | ▶ | my $mime_type = $is_dir ? 'directory' | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$types->type(get_ext($file) || "txt") or "text/plain"
get_ext($file) || 'txt'
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")
Truth table100%2/2
A
A= get_ext($file)
| |||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$stat[7] || 0
$stat[7]
Truth table50%1/2
A
A= $stat[7]
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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 [])
MC/DC0%0/1
shift() || (return [])
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||
| 171 | 7 | my $dh = DirHandle->new($dir); | |||||||||||||||||||||||||||||
| 172 | 7 | my @children; | |||||||||||||||||||||||||||||
| 173 | 7 | ▶ | while (defined(my $ent = $dh->read)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 174 | 29 | ▶ | next if $ent eq '.' or $ent eq '..'; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ent eq "." or $ent eq ".."
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 ".."
| |||||||||||||||||||||||||||||||
| 175 | 15 | push @children, Encode::decode_utf8($ent); | |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||
| 177 | 7 | return [ @children ]; | |||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | 3 | 1; | |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||||||||||||||