blib/lib/MKDoc/Core/Plugin/Resources.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran n/a Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 97 CC 1 SCAR 6.9
CC
1
Cov
0%
activate
2.0
render
2.0
HTTP_Last_Modified
2.0
CRAP 2.0
1 =head1 NAME
2  
3 MKDoc::Core::Plugin::Resources - Serves Resource Files.
4  
5  
6 =head1 SUMMARY
7  
8 L<MKDoc::Core> has a mechanism to provide resource files (such as style sheet, images, etc)
9 which supports some inheritance mechanism.
10  
11 When a request is made to /.resources/foo.png, it will try to serve:
12  
13 =over 4
14  
15 =item $ENV{SITE_DIR}/resources/foo.png
16  
17 =item $ENV{MKDOC_DIR}/resources/foo.png
18  
19 =item @INC/MKDoc/resources/foo.png
20  
21 =back
22  
23 Furthermore, if at any point it finds /resources/foo.png.deleted, it will
24 decline the request, which usually results in a 404 page.
25  
26 =cut
27 package MKDoc::Core::Plugin::Resources;
28 use MKDoc::Core::ResourceFinder;
29 use base qw /MKDoc::Core::Plugin/;
30 use strict;
31 use warnings;
32 use HTTP::Date;
33  
34  
35 sub activate
36 {
37 my $self = shift;
38 my $req = $self->request();
39 my $path = $req->path_info();
40 $path =~ s/^\/\.resources\/// or return;
41  
42 my $file = MKDoc::Core::ResourceFinder::rel2abs ($path) || return;
43 $self->{'.file'} = $file;
44  
45 open FP, "<$file" or do {
46 warn "Cannot read $file. Reason: $!";
47 return;
48 };
49 $self->{'.data'} = join '', <FP>;
50 close FP;
51  
52 return $self->SUPER::activate (@_);
53 }
54  
55  
56 sub render
57 {
58 my $self = shift;
59 return $self->{'.data'};
60 }
61  
62  
63 sub HTTP_Last_Modified
64 {
65 my $self = shift;
66 my $file = $self->{'.file'};
67 my ( $dev, $ino, $mode, $nlink, $uid,
68 $gid, $rdev, $size, $atime, $mtime,
69 $ctime,$blksize,$blocks ) = stat ($file);
70  
71 return HTTP::Date::time2str ($mtime);
72 }
73  
74  
75 sub HTTP_Content_Type
76 {
77 my $self = shift;
78 my $file = $self->{'.file'};
79  
80 # if there is no mime types resource file, try
81 # and return a generic default.
82 my $mime = MKDoc::Core::ResourceFinder::rel2abs ('/misc/mime.types') || return 'application/octet-stream';
83
84 # if we can't open the file, moan in the error log
85 # and return a generic default.
86 open FP, "<$mime" || do {
87 warn "Cannot read-open $mime";
88 return 'application/octet-stream';
89 };
90  
91 # build a file extension => mime type hash
92 my %hash = ();
93 while (my $line = <FP>)
94 {
95 chomp ($line);
96 $line =~ s/^\s+//;
97 $line =~ /^\#/ and next;
98  
99 my @split = split /\s+/, $line;
100 @split <= 1 and next;
101
102 my $mime = shift (@split);
103 for (@split) { $hash{$_} = $mime }
104 }
105 close FP;
106  
107 # match the current file against file extensions, but
108 # longest file extension first (i.e. .tar.gz != .gz). If a
109 # match is found, return the matching mime type.
110 foreach my $key (sort { length ($b) <=> length ($a) } keys %hash)
111 {
112 $file =~ /\.$key$/ and return $hash{$key};
113 }
114  
115 # no match is found, return generic default.
116 return 'application/octet-stream';
117 }
118 1;
119  
120  
121 __END__