Created
May 19, 2023 01:05
-
-
Save lbe/1b0de949e14300ffa52bd9f1c6896895 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite -signatures; | |
use Mojo::File; | |
use Mojo::Util qw(getopt); | |
use Pod::Usage; | |
use version 0.77; | |
my $VERSION = "0.01"; | |
my $opts = { | |
auto_index => 1, | |
dir_index => [qw/index.html index.htm/], | |
enable_json => 1, | |
help => 0, | |
man => 0, | |
port => 8080, | |
}; | |
getopt( | |
'autoindex' => \$opts->{auto_index}, | |
'enable_json' => \$opts->{enable_json}, | |
'help|h' => \$opts->{help}, | |
'man' => \$opts->{man}, | |
'port=i' => \$opts->{port} | |
) || pod2usage; | |
pod2usage(0) if $opts->{help}; | |
pod2usage( -exitval => 0, -verbose => 2 ) if $opts->{man}; | |
my $root = shift // './'; | |
plugin 'Directory::Stylish' => { | |
root => $root, | |
dir_index => $opts->{dir_index}, | |
enable_json => 1 | |
}; | |
helper split_path_to_link => sub ($c) { | |
my $current = $c->stash->{current}; | |
my $parts = Mojo::File->new($current)->to_array; | |
my $href = ''; | |
my @ret; | |
foreach my $p ( @{$parts} ) { | |
next if ( $href ne '' && $p eq '' ); | |
my $label = $p eq '' ? '.' : "$p"; | |
$href .= $p eq '' ? '/' : qq/\/$p/; | |
$href =~ s/\/\//\//g; | |
my $row = { label => $label, href => $href }; | |
push( @ret, $row ); | |
} | |
return ( \@ret ); | |
}; | |
app->start( 'daemon', '-l', "http://*:$opts->{port}" ); | |
__DATA__ | |
@@ list.html.ep | |
% title "index of $current"; | |
% layout 'default'; | |
<h2>listing | |
<%foreach my $pp ( @{ $c->split_path_to_link } ) {%><a href='<%= $pp->{href} %>'><%= $pp->{label} %></a>/<%}%> | |
</h2> | |
<div id="container"> | |
<table> | |
<thead> | |
<tr> | |
<th>filename</th> | |
<th class="size">size</th> | |
<th>type</th> | |
<th>last modified</th> | |
</tr> | |
</thead> | |
<tbody> | |
% for my $file (@$files) { | |
% next if substr($file->{name},0,1) eq '.'; | |
% while ($file->{size} =~ s/^(-?\d+)(\d{3}(?:,\d{3})*(?:\.\d+)*)$/$1,$2/) {}; | |
<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> | |
% } | |
</tbody> | |
</table> | |
</div> | |
@@ style.html.ep | |
<style type='text/css'> | |
body { | |
color: #bbbbbb; | |
background-color: #000000; | |
font-family: Calibri, Candara, Segoe, Segoe UI, Helvetica Neue, Helvetica, Optima, Arial, sans-serif; | |
font-size: normal 1em sans-serif; | |
text-align: center; | |
padding: 0; | |
margin: 0; | |
} | |
h2 { | |
font-size: 2.000em; | |
font-weight: 700; | |
} | |
table { | |
width: 90%; | |
margin: 3em; | |
border: 1px solid #aaaaaa; | |
border-collapse: collapse; } | |
thead { | |
background-color: #222222; | |
font-weight: 700; | |
font-size: 1.300em; | |
} | |
td, th { | |
padding: 1em; | |
text-align: left; | |
border-bottom: 1px solid #999999; | |
} | |
tr:nth-child(even) { | |
background: #1B1B1B; | |
} | |
.size { | |
text-align: right; | |
padding-right: 1.700em; | |
} | |
a:link { | |
font-size: 1.200em; | |
font-weight: 500; | |
color: #bbbbbb;; | |
text-decoration: none; | |
} | |
a:visted { | |
font-size: 1.200em; | |
font-weight: 500; | |
color: #301934; | |
text-decoration: none; | |
</style> | |
__END__ | |
=pod | |
=head1 NAME | |
serve_this - export the current directory over HTTP | |
=head1 VERSION | |
version 0.01 | |
=head1 SYNOPSIS | |
## Export the current directory with HTTP | |
$ serve_this | |
## Export the dir_name directory with HTTP (default = current directory) | |
$ serve_this dir_name | |
## Start the server on a specific port (default = 8081) | |
$ serve_this --port 9001 | |
## Start the server using index.html for directory requests | |
$ serve_this --autoindex | |
## Show documentation about our options | |
$ serve_this --help | |
## Show the entire man page | |
$ serve_this --man | |
=head1 DESCRIPTION | |
The C<serve_this> command exports the current directory via HTTP. You can | |
also export any directory by providing the path as a parameter. | |
A simple web server is started and is kept running until you kill it | |
with C<Ctrl-C>. | |
All the files and directories will be availble to a browser under the | |
URL the script outputs. | |
=encoding utf8 | |
=head1 ARGUMENTS | |
The script accepts a single optional argument: the path of the directory | |
to export. | |
=head1 OPTIONS | |
The following options are available: | |
=over 4 | |
=item --port PORT | |
Start the HTTP server on a specific C<PORT>. | |
=item --autoindex | |
By default, if the server gets a request that ends in / then it displays | |
a listing of that directory. With the C<--autoindex> flag, it will serve | |
the C<index.html> file from that directory (if it exists). | |
=item --help | |
Print information about the script usage and its options. | |
=item --man | |
Print the entire man page for the command. | |
=back | |
=head1 AUTHOR | |
learnedbyerror <[email protected]> | |
=head1 COPYRIGHT AND LICENSE | |
This software is Copyright (c) 2023 by Learned By Error | |
This is free software, licensed under: | |
MIT License | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment