File Coverage

blib/lib/WebServer/DirIndex/File.pm
Criterion Covered Total %
statement 21 22 95.4
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 33 34 97.0


line stmt bran cond sub pod time code
1 2     2   133427 use strict;
  2         3  
  2         64  
2 2     2   8 use warnings;
  2         3  
  2         93  
3 2     2   517 use Feature::Compat::Class;
  2         531  
  2         10  
4 2     2   743 use WebServer::DirIndex::HTML;
  2         5  
  2         153  
5              
6             class WebServer::DirIndex::File v0.1.0 {
7              
8 2     2   456 use HTML::Escape qw(escape_html);
  2         1568  
  2         1634  
9              
10             my %ICON_MAP = (
11             'directory' => 'fa-solid fa-folder',
12             '' => 'fa-solid fa-arrow-up',
13             'text/plain' => 'fa-solid fa-file-lines',
14             'text/html' => 'fa-solid fa-file-code',
15             'text/css' => 'fa-solid fa-file-code',
16             'text/csv' => 'fa-solid fa-file-csv',
17             'text/javascript' => 'fa-solid fa-file-code',
18             'application/pdf' => 'fa-solid fa-file-pdf',
19             'application/msword' => 'fa-solid fa-file-word',
20             'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'fa-solid fa-file-word',
21             'application/vnd.ms-excel' => 'fa-solid fa-file-excel',
22             'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'fa-solid fa-file-excel',
23             'application/vnd.ms-powerpoint' => 'fa-solid fa-file-powerpoint',
24             'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'fa-solid fa-file-powerpoint',
25             'application/javascript' => 'fa-solid fa-file-code',
26             'application/json' => 'fa-solid fa-file-code',
27             'application/xml' => 'fa-solid fa-file-code',
28             'application/zip' => 'fa-solid fa-file-zipper',
29             'application/x-tar' => 'fa-solid fa-file-zipper',
30             'application/gzip' => 'fa-solid fa-file-zipper',
31             'application/x-bzip2' => 'fa-solid fa-file-zipper',
32             'application/x-rar-compressed' => 'fa-solid fa-file-zipper',
33             );
34              
35             my %ICON_PREFIX_MAP = (
36             'image/' => 'fa-solid fa-file-image',
37             'audio/' => 'fa-solid fa-file-audio',
38             'video/' => 'fa-solid fa-file-video',
39             'text/' => 'fa-solid fa-file-lines',
40             );
41              
42             sub _icon_class {
43 10     10   20 my ($mime_type) = @_;
44 10 100       64 return $ICON_MAP{$mime_type} if exists $ICON_MAP{$mime_type};
45 1         6 for my $prefix (keys %ICON_PREFIX_MAP) {
46 2 100       13 return $ICON_PREFIX_MAP{$prefix} if index($mime_type, $prefix) == 0;
47             }
48 0         0 return 'fa-solid fa-file';
49             }
50              
51             field $url :param :reader;
52             field $name :param :reader;
53             field $size :param :reader;
54             field $mime_type :param :reader;
55             field $mtime :param :reader;
56             field $icon :param :reader = undef;
57             field $icons :param = 0;
58             field $html_class :param = 'WebServer::DirIndex::HTML';
59             field $_html_obj = $html_class->new(icons => $icons);
60              
61             ADJUST {
62             if ($icons && !defined $icon) {
63             $icon = _icon_class($mime_type);
64             }
65             }
66              
67             method to_html {
68             if (defined $icon) {
69             return sprintf $_html_obj->file_html_icons,
70             map { escape_html($_) }
71             ($icon, $url, $name, $size, $mime_type, $mtime);
72             }
73             return sprintf $_html_obj->file_html,
74             map { escape_html($_) }
75             ($url, $name, $size, $mime_type, $mtime);
76             }
77              
78             sub parent_dir {
79 8     8 1 9442 my ($class, %args) = @_;
80 8         86 return WebServer::DirIndex::File->new(
81             url => '../',
82             name => 'Parent Directory',
83             size => '',
84             mime_type => '',
85             mtime => '',
86             %args,
87             );
88             }
89             }
90              
91             1;
92              
93             __END__