diff --git a/lib/Plack/App/Directory.pm b/lib/Plack/App/Directory.pm index 48443ca0..32968f7a 100644 --- a/lib/Plack/App/Directory.pm +++ b/lib/Plack/App/Directory.pm @@ -3,6 +3,7 @@ use parent qw(Plack::App::File); use strict; use warnings; use Plack::Util; +use Plack::Util::Accessor 'render_cb'; use HTTP::Date; use Plack::MIME; use DirHandle; @@ -38,6 +39,19 @@ table { width:100%%; } PAGE +sub render_index { + my $self = shift; + my ($path, @files) = @_; + + my $title = Plack::Util::encode_html("Index of $path"); + my $files = join "\n", map { + my $f = $_; + sprintf $dir_file, map Plack::Util::encode_html($_), @$f; + } @files; + + return sprintf $dir_page, $title, $title, $files; +} + sub should_handle { my($self, $file) = @_; return -d $file || -f $file; @@ -96,12 +110,9 @@ sub serve_path { push @files, [ $url, $basename, $stat[7], $mime_type, HTTP::Date::time2str($stat[9]) ]; } - my $path = Plack::Util::encode_html("Index of $env->{PATH_INFO}"); - my $files = join "\n", map { - my $f = $_; - sprintf $dir_file, map Plack::Util::encode_html($_), @$f; - } @files; - my $page = sprintf $dir_page, $path, $path, $files; + my $page = $self->render_cb ? + $self->render_cb->($env->{PATH_INFO}, @files) : + $self->render_index($env->{PATH_INFO}, @files); return [ 200, ['Content-Type' => 'text/html; charset=utf-8'], [ $page ] ]; } @@ -120,6 +131,21 @@ Plack::App::Directory - Serve static files from document root with directory ind use Plack::App::Directory; my $app = Plack::App::Directory->new({ root => "/path/to/htdocs" })->to_app; +Or + + my $app = Plack::App::Directory->new({ + root => "/path/to/htdocs", + render_cb => \&render, + })->to_app; + + sub render { + my ($path, @files) = @_; + + # Code to render an HTML page + + return $html; + } + =head1 DESCRIPTION This is a static file server PSGI application with directory index a la Apache's mod_autoindex. @@ -132,6 +158,44 @@ This is a static file server PSGI application with directory index a la Apache's Document root directory. Defaults to the current directory. +=item render_cb + +A reference to a subroutine that takes information about a directory and +returns the HTML to display that directory - this is used to override the +default display of the directory. + +The first argument passed to this subroutine is the name of the path that +is being displayed. All other arguments contain information about the +files in the directory. Each of these arguments is a reference to an +array with five elements. These elements are: + +=over 4 + +=item * + +The URL of the file + +=item * + +The name of the file + +=item * + +The size of the file in bytes + +=item * + +The MIME type of the file + +=item * + +The last modified date of the file + +=back + +This subroutine is expected to return a string of HTML which will be +returned to the browser. + =back =head1 AUTHOR