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