line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Directory::Stylish; |
2
|
|
|
|
|
|
|
$Mojolicious::Plugin::Directory::Stylish::VERSION = '1.005'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Serve static files from document root with directory index using Mojolicious templates |
4
|
10
|
|
|
10
|
|
20392
|
use strict; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
282
|
|
5
|
10
|
|
|
10
|
|
54
|
use warnings; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
376
|
|
6
|
|
|
|
|
|
|
|
7
|
10
|
|
|
10
|
|
54
|
use Cwd (); |
|
10
|
|
|
|
|
32
|
|
|
10
|
|
|
|
|
152
|
|
8
|
10
|
|
|
10
|
|
463
|
use Encode (); |
|
10
|
|
|
|
|
7522
|
|
|
10
|
|
|
|
|
154
|
|
9
|
10
|
|
|
10
|
|
3693
|
use DirHandle; |
|
10
|
|
|
|
|
5156
|
|
|
10
|
|
|
|
|
277
|
|
10
|
10
|
|
|
10
|
|
406
|
use Mojo::Base qw{ Mojolicious::Plugin }; |
|
10
|
|
|
|
|
6380
|
|
|
10
|
|
|
|
|
66
|
|
11
|
10
|
|
|
10
|
|
2765
|
use Mojolicious::Types; |
|
10
|
|
|
|
|
585
|
|
|
10
|
|
|
|
|
92
|
|
12
|
10
|
|
|
10
|
|
607
|
use Mojo::Asset::File; |
|
10
|
|
|
|
|
94906
|
|
|
10
|
|
|
|
|
140
|
|
13
|
10
|
|
|
10
|
|
237
|
use Mojo::File; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
10002
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $types = Mojolicious::Types->new; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub register { |
18
|
9
|
|
|
9
|
1
|
356
|
my ( $self, $app, $args ) = @_; |
19
|
|
|
|
|
|
|
|
20
|
9
|
|
66
|
|
|
90
|
my $root = Mojo::File->new( $args->{root} || Cwd::getcwd ); |
21
|
9
|
|
|
|
|
92
|
my $handler = $args->{handler}; |
22
|
9
|
|
|
|
|
21
|
my $index = $args->{dir_index}; |
23
|
9
|
|
|
|
|
17
|
my $enable_json = $args->{enable_json}; |
24
|
9
|
|
100
|
|
|
54
|
my $auto_index = $args->{auto_index} // 1; |
25
|
|
|
|
|
|
|
|
26
|
9
|
|
100
|
|
|
61
|
my $css = $args->{css} || 'style'; |
27
|
9
|
|
50
|
|
|
49
|
my $render_opts = $args->{render_opts} || {}; |
28
|
9
|
|
100
|
|
|
47
|
$render_opts->{template} = $args->{dir_template} || 'list'; |
29
|
9
|
|
|
|
|
20
|
push @{ $app->renderer->classes }, __PACKAGE__; |
|
9
|
|
|
|
|
59
|
|
30
|
9
|
|
|
|
|
158
|
push @{ $app->static->classes }, __PACKAGE__; |
|
9
|
|
|
|
|
46
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$app->hook( |
33
|
|
|
|
|
|
|
before_dispatch => sub { |
34
|
26
|
|
|
26
|
|
289208
|
my $c = shift; |
35
|
|
|
|
|
|
|
|
36
|
26
|
100
|
|
|
|
167
|
return render_file( $c, $root ) if ( -f $root->to_string() ); |
37
|
|
|
|
|
|
|
|
38
|
24
|
|
|
|
|
956
|
my $child = Mojo::Util::url_unescape( $c->req->url->path ); |
39
|
24
|
|
|
|
|
1425
|
$child =~ s!^/!!g; |
40
|
24
|
|
|
|
|
1248
|
my $path = $root->child( $child ); |
41
|
24
|
100
|
|
|
|
650
|
$handler->( $c, $path ) if ( ref $handler eq 'CODE' ); |
42
|
|
|
|
|
|
|
|
43
|
24
|
100
|
|
|
|
3598
|
if ( -f $path ) { |
|
|
50
|
|
|
|
|
|
44
|
11
|
50
|
|
|
|
232
|
render_file( $c, $path ) unless ( $c->tx->res->code ); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
elsif ( -d $path ) { |
47
|
13
|
100
|
66
|
|
|
369
|
if ( $index && ( my $file = locate_index( $index, $path ) ) ) { |
48
|
1
|
|
|
|
|
29
|
return render_file( $c, $file ); |
49
|
|
|
|
|
|
|
} |
50
|
12
|
100
|
|
|
|
49
|
if ( $auto_index ) { |
51
|
11
|
50
|
|
|
|
43
|
$c->stash(css => $css), |
52
|
|
|
|
|
|
|
render_indexes( $c, $path, $render_opts, $enable_json ) |
53
|
|
|
|
|
|
|
unless ( $c->tx->res->code ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
}, |
57
|
9
|
|
|
|
|
185
|
); |
58
|
9
|
|
|
|
|
192
|
return $app; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub locate_index { |
62
|
1
|
|
50
|
1
|
0
|
5
|
my $index = shift || return; |
63
|
1
|
|
33
|
|
|
3
|
my $dir = shift || Cwd::getcwd; |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
12
|
my $root = Mojo::Home->new($dir); |
66
|
|
|
|
|
|
|
|
67
|
1
|
50
|
|
|
|
10
|
$index = ( ref $index eq 'ARRAY' ) ? $index : ["$index"]; |
68
|
1
|
|
|
|
|
7
|
for (@$index) { |
69
|
1
|
|
|
|
|
14
|
my $path = $root->rel_file($_); |
70
|
1
|
50
|
|
|
|
53
|
return $path if ( -e $path ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub render_file { |
75
|
3
|
|
|
3
|
0
|
85
|
my ( $c, $file ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
|
|
27
|
my $asset = Mojo::Asset::File->new(path => $file); |
78
|
3
|
|
|
|
|
49
|
$c->reply->asset($asset); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub render_indexes { |
82
|
11
|
|
|
11
|
0
|
397
|
my ( $c, $dir, $render_opts, $enable_json ) = @_; |
83
|
|
|
|
|
|
|
|
84
|
11
|
100
|
|
|
|
42
|
my @files = |
85
|
|
|
|
|
|
|
( $c->req->url eq '/' ) |
86
|
|
|
|
|
|
|
? () |
87
|
|
|
|
|
|
|
: ( { url => '../', name => 'Parent Directory', size => '', type => '', mtime => '' } ); |
88
|
|
|
|
|
|
|
|
89
|
11
|
|
|
|
|
1651
|
my ( $current, $list ) = list_files( $c, $dir ); |
90
|
11
|
|
|
|
|
418
|
push @files, @$list; |
91
|
|
|
|
|
|
|
|
92
|
11
|
|
|
|
|
62
|
$c->stash( files => \@files ); |
93
|
11
|
|
|
|
|
224
|
$c->stash( current => $current ); |
94
|
|
|
|
|
|
|
|
95
|
11
|
|
|
|
|
179
|
my %respond = ( any => $render_opts ); |
96
|
11
|
100
|
|
|
|
57
|
$respond{json} = { json => { files => \@files, current => $current } } |
97
|
|
|
|
|
|
|
if ($enable_json); |
98
|
|
|
|
|
|
|
|
99
|
11
|
|
|
|
|
58
|
$c->respond_to(%respond); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub list_files { |
103
|
11
|
|
|
11
|
0
|
93
|
my ( $c, $dir ) = @_; |
104
|
|
|
|
|
|
|
|
105
|
11
|
|
|
|
|
56
|
my $current = Encode::decode_utf8( Mojo::Util::url_unescape( $c->req->url->path ) ); |
106
|
|
|
|
|
|
|
|
107
|
11
|
50
|
|
|
|
1132
|
return ( $current, [] ) unless $dir; |
108
|
|
|
|
|
|
|
|
109
|
11
|
|
|
|
|
118
|
my $dh = DirHandle->new($dir); |
110
|
11
|
|
|
|
|
726
|
my @children; |
111
|
11
|
|
|
|
|
52
|
while ( defined( my $ent = $dh->read ) ) { |
112
|
150
|
100
|
100
|
|
|
3360
|
next if $ent eq '.' or $ent eq '..'; |
113
|
128
|
|
|
|
|
300
|
push @children, Encode::decode_utf8($ent); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
11
|
|
|
|
|
126
|
my @files; |
117
|
11
|
|
|
|
|
70
|
for my $basename ( sort { $a cmp $b } @children ) { |
|
343
|
|
|
|
|
561
|
|
118
|
128
|
|
|
|
|
431
|
my $file = "$dir/$basename"; |
119
|
128
|
|
|
|
|
928
|
my $url = Mojo::Path->new($current)->trailing_slash(0); |
120
|
128
|
|
|
|
|
7310
|
push @{ $url->parts }, $basename; |
|
128
|
|
|
|
|
329
|
|
121
|
|
|
|
|
|
|
|
122
|
128
|
|
|
|
|
2251
|
my $is_dir = -d $file; |
123
|
128
|
|
|
|
|
476
|
my @stat = stat _; |
124
|
128
|
100
|
|
|
|
355
|
if ($is_dir) { |
125
|
14
|
|
|
|
|
43
|
$basename .= '/'; |
126
|
14
|
|
|
|
|
45
|
$url->trailing_slash(1); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
128
|
100
|
100
|
|
|
477
|
my $mime_type = |
130
|
|
|
|
|
|
|
($is_dir) |
131
|
|
|
|
|
|
|
? 'directory' |
132
|
|
|
|
|
|
|
: ( $types->type( get_ext($file) || 'txt' ) || 'text/plain' ); |
133
|
128
|
|
|
|
|
1865
|
my $mtime = Mojo::Date->new( $stat[9] )->to_string(); |
134
|
|
|
|
|
|
|
|
135
|
128
|
|
100
|
|
|
4627
|
push @files, { |
136
|
|
|
|
|
|
|
url => $url, |
137
|
|
|
|
|
|
|
name => $basename, |
138
|
|
|
|
|
|
|
size => $stat[7] || 0, |
139
|
|
|
|
|
|
|
type => $mime_type, |
140
|
|
|
|
|
|
|
mtime => $mtime, |
141
|
|
|
|
|
|
|
}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
11
|
|
|
|
|
82
|
return ( $current, \@files ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub get_ext { |
148
|
114
|
100
|
|
114
|
0
|
605
|
$_[0] =~ /\.([0-9a-zA-Z]+)$/ || return; |
149
|
108
|
|
|
|
|
626
|
return lc $1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
1; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=pod |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=encoding UTF-8 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 NAME |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Mojolicious::Plugin::Directory::Stylish - Serve static files from document root with directory index using Mojolicious templates |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 VERSION |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
version 1.005 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SYNOPSIS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
use Mojolicious::Lite; |
169
|
|
|
|
|
|
|
plugin 'Directory::Stylish'; |
170
|
|
|
|
|
|
|
app->start; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
or |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
> perl -Mojo -E 'a->plugin("Directory::Stylish")->start' daemon |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 DESCRIPTION |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
L is a static file server directory index a la Apache's mod_autoindex. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 METHODS |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
L inherits all methods from L. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 OPTIONS |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L supports the following options. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 C |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { root => "/path/to/htdocs" }; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Document root directory. Defaults to the current directory. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
If root is a file, serve only root file. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 C |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# Mojolicious::Lite |
199
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { auto_index => 0 }; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Automatically generate index page for directory, default true. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head2 C |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { dir_index => [qw/index.html index.htm/] }; |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Like a Apache's DirectoryIndex directive. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head2 C |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { dir_template => 'index' }; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
# with 'render_opts' option |
214
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { |
215
|
|
|
|
|
|
|
dir_template => 'index', |
216
|
|
|
|
|
|
|
render_opts => { format => 'html', handler => 'ep' }, |
217
|
|
|
|
|
|
|
}; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
... |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
__DATA__ |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
@@ index.html.ep |
224
|
|
|
|
|
|
|
% layout 'default'; |
225
|
|
|
|
|
|
|
% title 'DirectoryIndex'; |
226
|
|
|
|
|
|
|
Index of <%= $current %> |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
% for my $file (@$files) { |
229
|
|
|
|
|
|
|
<%== $file->{name} %> |
230
|
|
|
|
|
|
|
% } |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
@@ layouts/default.html.ep |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
<%= title %> |
236
|
|
|
|
|
|
|
<%= content %> |
237
|
|
|
|
|
|
|
%= include $css; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
A name for the template to use for the index page. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
"$files", "$current", and "$css" are passed in stash. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=over 2 |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=item * $files: Array[Hash] |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
list of files and directories |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=item * $current: String |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
current path |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=item * $css: String |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
name of template with css that you want to include |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=back |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head2 C |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
use Text::Markdown qw{ markdown }; |
263
|
|
|
|
|
|
|
use Path::Class; |
264
|
|
|
|
|
|
|
use Encode qw{ decode_utf8 }; |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { |
267
|
|
|
|
|
|
|
handler => sub { |
268
|
|
|
|
|
|
|
my ($c, $path) = @_; |
269
|
|
|
|
|
|
|
if ($path =~ /\.(md|mkdn)$/) { |
270
|
|
|
|
|
|
|
my $text = file($path)->slurp; |
271
|
|
|
|
|
|
|
my $html = markdown( decode_utf8($text) ); |
272
|
|
|
|
|
|
|
$c->render( inline => $html ); |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
}; |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
CODEREF for handle a request file. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
If not rendered in CODEREF, serve as static file. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 C |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
# http://host/directory?format=json |
284
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { enable_json => 1 }; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
enable json response. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head2 C |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
plugin 'Directory::Stylish' => { css => 'custom_template' }; |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
... |
293
|
|
|
|
|
|
|
__DATA__ |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
@@ custom_template.html.ep |
296
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
A name for the template with css that will be included by the default template |
301
|
|
|
|
|
|
|
for the index. |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
This name will be available as C<$css> in the stash. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
Many thanks to the contributors for their work. |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=over 2 |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=item * ChinaXing |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=item * Su-Shee |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=back |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=head1 SEE ALSO |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=over 2 |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=item * L |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=item * L |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=back |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head1 ORIGINAL AUTHOR |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
hayajo Ehayajo@cpan.orgE - Original author of L |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=head1 AUTHOR |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
Andreas Guldstrand |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Hayato Imai, Andreas Guldstrand. |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
340
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=cut |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
__DATA__ |