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