File Coverage

blib/lib/Cake/Plugins/Static.pm
Criterion Covered Total %
statement 20 52 38.4
branch 2 10 20.0
condition 0 2 0.0
subroutine 5 6 83.3
pod 0 2 0.0
total 27 72 37.5


line stmt bran cond sub pod time code
1             package Cake::Plugins::Static;
2 1     1   6 use Cake 'Plugin';
  1         2  
  1         9  
3 1     1   6 use Cake::Utils;
  1         2  
  1         84  
4 1     1   6 use Carp;
  1         1  
  1         619  
5            
6             my $MIME_TYPES = {
7             "3gp" => "video/3gpp",
8             "a" => "application/octet-stream",
9             "ai" => "application/postscript",
10             "aif" => "audio/x-aiff",
11             "aiff" => "audio/x-aiff",
12             "asc" => "application/pgp-signature",
13             "asf" => "video/x-ms-asf",
14             "asm" => "text/x-asm",
15             "asx" => "video/x-ms-asf",
16             "atom" => "application/atom+xml",
17             "au" => "audio/basic",
18             "avi" => "video/x-msvideo",
19             "bat" => "application/x-msdownload",
20             "bin" => "application/octet-stream",
21             "bmp" => "image/bmp",
22             "bz2" => "application/x-bzip2",
23             "c" => "text/x-c",
24             "cab" => "application/vnd.ms-cab-compressed",
25             "cc" => "text/x-c",
26             "chm" => "application/vnd.ms-htmlhelp",
27             "class" => "application/octet-stream",
28             "com" => "application/x-msdownload",
29             "conf" => "text/plain",
30             "cpp" => "text/x-c",
31             "crt" => "application/x-x509-ca-cert",
32             "css" => "text/css",
33             "csv" => "text/csv",
34             "cxx" => "text/x-c",
35             "deb" => "application/x-debian-package",
36             "der" => "application/x-x509-ca-cert",
37             "diff" => "text/x-diff",
38             "djv" => "image/vnd.djvu",
39             "djvu" => "image/vnd.djvu",
40             "dll" => "application/x-msdownload",
41             "dmg" => "application/octet-stream",
42             "doc" => "application/msword",
43             "dot" => "application/msword",
44             "dtd" => "application/xml-dtd",
45             "dvi" => "application/x-dvi",
46             "ear" => "application/java-archive",
47             "eml" => "message/rfc822",
48             "eps" => "application/postscript",
49             "exe" => "application/x-msdownload",
50             "f" => "text/x-fortran",
51             "f77" => "text/x-fortran",
52             "f90" => "text/x-fortran",
53             "flv" => "video/x-flv",
54             "for" => "text/x-fortran",
55             "gem" => "application/octet-stream",
56             "gemspec" => "text/x-script.ruby",
57             "gif" => "image/gif",
58             "gz" => "application/x-gzip",
59             "h" => "text/x-c",
60             "hh" => "text/x-c",
61             "htm" => "text/html",
62             "html" => "text/html",
63             "ico" => "image/vnd.microsoft.icon",
64             "ics" => "text/calendar",
65             "ifb" => "text/calendar",
66             "iso" => "application/octet-stream",
67             "jar" => "application/java-archive",
68             "java" => "text/x-java-source",
69             "jnlp" => "application/x-java-jnlp-file",
70             "jpeg" => "image/jpeg",
71             "jpg" => "image/jpeg",
72             "js" => "application/javascript",
73             "json" => "application/json",
74             "log" => "text/plain",
75             "m3u" => "audio/x-mpegurl",
76             "m4v" => "video/mp4",
77             "man" => "text/troff",
78             "manifest" => "text/cache-manifest",
79             "mathml" => "application/mathml+xml",
80             "mbox" => "application/mbox",
81             "mdoc" => "text/troff",
82             "me" => "text/troff",
83             "mid" => "audio/midi",
84             "midi" => "audio/midi",
85             "mime" => "message/rfc822",
86             "mml" => "application/mathml+xml",
87             "mng" => "video/x-mng",
88             "mov" => "video/quicktime",
89             "mp3" => "audio/mpeg",
90             "mp4" => "video/mp4",
91             "mp4v" => "video/mp4",
92             "mpeg" => "video/mpeg",
93             "mpg" => "video/mpeg",
94             "ms" => "text/troff",
95             "msi" => "application/x-msdownload",
96             "odp" => "application/vnd.oasis.opendocument.presentation",
97             "ods" => "application/vnd.oasis.opendocument.spreadsheet",
98             "odt" => "application/vnd.oasis.opendocument.text",
99             "ogg" => "application/ogg",
100             "p" => "text/x-pascal",
101             "pas" => "text/x-pascal",
102             "pbm" => "image/x-portable-bitmap",
103             "pdf" => "application/pdf",
104             "pem" => "application/x-x509-ca-cert",
105             "pgm" => "image/x-portable-graymap",
106             "pgp" => "application/pgp-encrypted",
107             "pkg" => "application/octet-stream",
108             "pl" => "text/x-script.perl",
109             "pm" => "text/x-script.perl-module",
110             "png" => "image/png",
111             "pnm" => "image/x-portable-anymap",
112             "ppm" => "image/x-portable-pixmap",
113             "pps" => "application/vnd.ms-powerpoint",
114             "ppt" => "application/vnd.ms-powerpoint",
115             "ps" => "application/postscript",
116             "psd" => "image/vnd.adobe.photoshop",
117             "py" => "text/x-script.python",
118             "qt" => "video/quicktime",
119             "ra" => "audio/x-pn-realaudio",
120             "rake" => "text/x-script.ruby",
121             "ram" => "audio/x-pn-realaudio",
122             "rar" => "application/x-rar-compressed",
123             "rb" => "text/x-script.ruby",
124             "rdf" => "application/rdf+xml",
125             "roff" => "text/troff",
126             "rpm" => "application/x-redhat-package-manager",
127             "rss" => "application/rss+xml",
128             "rtf" => "application/rtf",
129             "ru" => "text/x-script.ruby",
130             "s" => "text/x-asm",
131             "sgm" => "text/sgml",
132             "sgml" => "text/sgml",
133             "sh" => "application/x-sh",
134             "sig" => "application/pgp-signature",
135             "snd" => "audio/basic",
136             "so" => "application/octet-stream",
137             "svg" => "image/svg+xml",
138             "svgz" => "image/svg+xml",
139             "swf" => "application/x-shockwave-flash",
140             "t" => "text/troff",
141             "tar" => "application/x-tar",
142             "tbz" => "application/x-bzip-compressed-tar",
143             "tcl" => "application/x-tcl",
144             "tex" => "application/x-tex",
145             "texi" => "application/x-texinfo",
146             "texinfo" => "application/x-texinfo",
147             "text" => "text/plain",
148             "tif" => "image/tiff",
149             "tiff" => "image/tiff",
150             "torrent" => "application/x-bittorrent",
151             "tr" => "text/troff",
152             "txt" => "text/plain",
153             "vcf" => "text/x-vcard",
154             "vcs" => "text/x-vcalendar",
155             "vrml" => "model/vrml",
156             "war" => "application/java-archive",
157             "wav" => "audio/x-wav",
158             "wma" => "audio/x-ms-wma",
159             "wmv" => "video/x-ms-wmv",
160             "wmx" => "video/x-ms-wmx",
161             "wrl" => "model/vrml",
162             "wsdl" => "application/wsdl+xml",
163             "xbm" => "image/x-xbitmap",
164             "xhtml" => "application/xhtml+xml",
165             "xls" => "application/vnd.ms-excel",
166             "xml" => "application/xml",
167             "xpm" => "image/x-xpixmap",
168             "xsl" => "application/xml",
169             "xslt" => "application/xslt+xml",
170             "yaml" => "text/yaml",
171             "yml" => "text/yaml",
172             "zip" => "application/zip"
173             };
174            
175 1     1   6 use Data::Dumper;
  1         1  
  1         6271  
176             sub sendFile {
177 0     0 0 0 my $self = shift;
178 0         0 print Dumper $self;
179             }
180            
181             sub setup {
182 1     1 0 8 my $self = shift;
183 1         4 my $settings = settings();
184            
185             ####static path
186 1         3 my $dir = $settings->{dir};
187 1 50       3 if (!$dir){
188 0         0 $self->warn("***Please specify a static path when using Cake::Plugins::Static");
189 0         0 $self->warn("***Ex: 'Static'=>{dir=>'/path/to/static/folder'}");
190 0         0 return $self->NEXT::setup();
191             }
192            
193             ##get last folder
194 1         7 $dir =~ s/(.*)\/(.*?)$/$1/;
195 1         3 my $ma_path = '/'.$2;
196            
197 1 50       13 if ($self->path =~ /^$ma_path/i){
198            
199 0         0 my $path = $self->path;
200 0         0 my ($ext) = $path =~ m/.*\.(.*?)$/;
201 0   0     0 my $content_type = $MIME_TYPES->{$ext} || 'text/plain';
202 0         0 my $file = $dir.$path;
203            
204 0         0 $self->content_type($content_type);
205            
206             ##client caching
207 0         0 my $lastModified = (stat $file)[9];
208 0 0       0 if (my $etag = $self->env->HTTP_IF_NONE_MATCH){
209 0 0       0 if ($lastModified == $etag){
210 0         0 $self->status_code(304);
211 0         0 $self->log('Serving From Client Cache');
212 0         0 return 1;
213             }
214             }
215            
216 0         0 my $data = '';
217             #open file
218 0 0       0 if (open my $fh,'<',$file ){
219 0         0 binmode $fh;
220 0         0 $data = do { local $/; <$fh> };
  0         0  
  0         0  
221 0         0 close($fh);
222             } else {
223 0         0 $self->content_type('text/plain');
224 0         0 $self->status_code('404');
225 0         0 $self->body('File Doesn\'t Exists');
226 0         0 $self->log("Static File $path not found");
227 0         0 return 1;
228             }
229            
230 0         0 $self->push_header(Etag => $lastModified);
231 0         0 $self->log("Serving Static File $path");
232 0         0 $self->body($data);
233 0         0 return 1;
234            
235             } else {
236 1         13 $self->NEXT::setup();
237             }
238             }
239            
240             register();
241            
242             1;
243            
244             __END__