File Coverage

blib/lib/WebServer/DirIndex.pm
Criterion Covered Total %
statement 33 33 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod n/a
total 44 44 100.0


line stmt bran cond sub pod time code
1 1     1   158135 use strict;
  1         2  
  1         29  
2 1     1   3 use warnings;
  1         2  
  1         41  
3 1     1   433 use Feature::Compat::Class;
  1         329  
  1         3  
4 1     1   541 use WebServer::DirIndex::HTML;
  1         2  
  1         60  
5              
6             class WebServer::DirIndex v0.1.0 {
7              
8 1     1   909 use Path::Tiny;
  1         10265  
  1         51  
9 1     1   449 use HTTP::Date;
  1         3755  
  1         59  
10 1     1   404 use HTML::Escape qw(escape_html);
  1         641  
  1         52  
11 1     1   430 use MIME::Types;
  1         5176  
  1         84  
12 1     1   654 use URI::Escape;
  1         1557  
  1         69  
13 1     1   389 use WebServer::DirIndex::CSS;
  1         3  
  1         25  
14 1     1   347 use WebServer::DirIndex::File;
  1         2  
  1         644  
15              
16             my $mime_types = MIME::Types->new;
17              
18             field $dir :param;
19             field $dir_url :param;
20             field $icons :param = undef;
21             field $pretty :param = 0;
22             field $html_class :param = 'WebServer::DirIndex::HTML';
23             field $css_class :param = 'WebServer::DirIndex::CSS';
24             field $_html_obj;
25             field $_css_obj;
26             field @files;
27              
28             ADJUST {
29             $icons = 1 if !defined($icons) && $pretty; # pretty implies icons when unset
30             $icons //= 1; # default to enabled otherwise
31             $_html_obj = $html_class->new(icons => $icons);
32             $_css_obj = $css_class->new(pretty => $pretty);
33             @files = ( WebServer::DirIndex::File->parent_dir(
34             html_class => $html_class,
35             icons => $icons,
36             ) );
37              
38             my @children = map { $_->basename } path($dir)->children;
39              
40             for my $basename (sort { $a cmp $b } @children) {
41             my $file = "$dir/$basename";
42             my $url = $dir_url . $basename;
43              
44             my $is_dir = -d $file;
45             my @stat = stat $file;
46              
47             $url = join '/', map { uri_escape($_) } split m{/}, $url;
48              
49             if ($is_dir) {
50             $basename .= '/';
51             $url .= '/';
52             }
53              
54             my $type_obj = $is_dir ? undef : $mime_types->mimeTypeOf($file);
55             my $mime_type = $is_dir
56             ? 'directory'
57             : ($type_obj ? $type_obj->type : 'text/plain');
58              
59             push @files, WebServer::DirIndex::File->new(
60             url => $url,
61             name => $basename,
62             size => $stat[7],
63             mime_type => $mime_type,
64             mtime => HTTP::Date::time2str($stat[9]),
65             html_class => $html_class,
66             icons => $icons,
67             );
68             }
69             }
70              
71             method files { return @files }
72              
73             method to_html ($path_info) {
74             my $path = escape_html("Index of $path_info");
75             my $files_html = join "\n", map { $_->to_html } @files;
76             my $css = $_css_obj->css;
77             return sprintf $_html_obj->dir_html, $path, $css, $path, $files_html;
78             }
79             }
80              
81             1;
82              
83             __END__