-
Notifications
You must be signed in to change notification settings - Fork 3
/
dirserve
executable file
·175 lines (145 loc) · 4.26 KB
/
dirserve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd 'cwd';
use Getopt::Long;
# Configure
my $usage = <<"EOF";
usage: dirserve [OPTIONS]
These options are available:
--directory Directory to serve. Defaults to current directory.
--index-page Page to show by default. Defaults to directory listing.
--port Port to serve on. Defaults to 3000.
--no-dots No navigating to parent directories via '..'.
--help This message.
EOF
my @GETOPT_SPEC = qw/
directory=s
index=s
port=s
no-dots
help
/;
my %opt = (
directory => cwd,
port => 3000
);
my $p = Getopt::Long::Parser->new;
$p->getoptions( \%opt, @GETOPT_SPEC );
print $usage and exit if $opt{help};
package App::dirserve;
use Mojolicious::Lite;
use Mojo::Server::Daemon;
app->static->root($opt{directory});
# Default page
get '/' => sub {
shift->render_static( $opt{index} );
}
if $opt{index};
# Dir listing
get '/(*dir)' => { dir => '' } => sub {
my $self = shift;
# Trailing slash confuses parent navigation
my $dir = $self->param('dir');
$dir =~ s|/$||;
my $fulldir = "$opt{directory}/$dir";
# Redirect to nearest valid dir
if (! -e $fulldir) {
$dir =~ s|/[^/]*$|| if ! -e $fulldir;
$self->redirect_to('/' . $dir);
}
opendir my ($dh), $fulldir;
my @items = map +{ name => $_, is_dir => -d "$fulldir/$_" }, readdir $dh;
closedir $dh;
# Remove . dirs
@items = grep $_->{name} !~ /^(?:\.|\.\.)$/, @items;
# No parent navigation
unshift @items, { name => '..', is_dir => 1 } if $dir and !$opt{'no-dots'};
$self->stash(
dir => $dir,
fulldir => $fulldir,
items => \@items
);
$self->render('listing');
};
app->secret('cookie-phrase');
Mojo::Server::Daemon->new->silent(1)->listen( ["http://*:$opt{port}"] )->run;
1;
__DATA__
@@ listing.html.ep
% my $class = begin
% if (shift->{is_dir}) {
class="dir"
% }
% end
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Lucida Grande", tahoma, sans-serif;
font-size: 100%;
margin: 0;
width: 100%;
}
h1 {
background: #999; /* for non-css3 browsers */
background: -webkit-gradient(linear, left top, left bottom, from(#A2C6E5), to(#2B6699)); /* webkit */
background: -moz-linear-gradient(top, #A2C6E5, #2B6699); /* mozilla */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#A2C6E5', endColorstr='#2B6699'); /* ie */
padding: 10px 0 10px 10px;
margin: 0;
color: white;
}
li.dir a {
font-weight: bold;
font-size: 1.1em;
color: #346D9E;
}
a {
color: #5C8DB8;
}
hr {
border: solid silver 1px;
width: 95%;
}
</style>
</head>
<body>
<base href="/" />
<h1><%= $fulldir %></h1>
<ul>
% foreach (@$items) {
<li<%== $class->($_) %>>
<a href="<%= $dir %>/<%= $_->{name} %>"><%= $_->{name} %></a>
</li>
% }
</ul>
<hr />
</body>
</html>
@@ favicon.ico (base64)
AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABEL
AAARCwAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAwMtBAQESAAAAB0AAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABra2tjr6+v835+
fvYnJye2AAAAHQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAn5+fm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