File Coverage

blib/lib/Dancer/MIME.pm
Criterion Covered Total %
statement 40 40 100.0
branch 3 4 75.0
condition 4 5 80.0
subroutine 14 14 100.0
pod 6 7 85.7
total 67 70 95.7


line stmt bran cond sub pod time code
1             package Dancer::MIME;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: Singleton object to handle MimeTypes
4             $Dancer::MIME::VERSION = '1.3520';
5 189     189   37843 use strict;
  189         424  
  189         5461  
6 189     189   938 use warnings;
  189         468  
  189         4804  
7 189     189   954 use base 'Dancer::Object::Singleton';
  189         432  
  189         24684  
8              
9 189     189   8418 use Dancer::Config;
  189         470  
  189         7620  
10              
11 189     189   1226 use Carp;
  189         568  
  189         10168  
12 189     189   87610 use MIME::Types;
  189         831293  
  189         10615  
13              
14             # Initialise MIME::Types at compile time, to ensure it's done before
15             # the fork in a preforking webserver like mod_perl or Starman. Not
16             # doing this leads to all MIME types being returned as "text/plain",
17             # as MIME::Types fails to load its mappings from the DATA handle. See
18             # t/04_static_file/003_mime_types_reinit.t and GH#136.
19             BEGIN {
20 189     189   1165 MIME::Types->new(only_complete => 1);
21             }
22              
23             __PACKAGE__->attributes( qw/mime_type custom_types/ );
24              
25             sub init {
26 18     18 1 332 my ($class, $instance) = @_;
27              
28 18         595 $instance->mime_type(MIME::Types->new(only_complete => 1));
29 18         274 $instance->custom_types({});
30             }
31              
32             sub default {
33 8     8 0 69 my $instance = shift;
34 8   100     27 return Dancer::Config::setting("default_mime_type") || "application/data";
35             }
36              
37             sub add_type {
38 6     6 1 19 my ($self, $name, $type) = @_;
39 6         15 $self->custom_types->{$name} = $type;
40 6         13 return;
41             }
42              
43             sub add_alias {
44 4     4 1 16 my($self, $alias, $orig) = @_;
45 4         10 my $type = $self->for_name($orig);
46 4         57 $self->add_type($alias, $type);
47 4         23 return $type;
48             }
49              
50             sub for_file {
51 17     17 1 47 my ($self, $filename) = @_;
52 17         120 my ($ext) = $filename =~ /\.([^.]+)$/;
53 17 50       56 return $self->default unless $ext;
54 17         59 return $self->for_name($ext);
55             }
56              
57             sub name_or_type {
58 26     26 1 67 my($self, $name) = @_;
59              
60 26 100       155 return $name if $name =~ m{/}; # probably a mime type
61 3         24 return $self->for_name($name);
62             }
63              
64             sub for_name {
65 31     31 1 187 my ($self, $name) = @_;
66 31   66     278 return $self->custom_types->{lc $name} || $self->mime_type->mimeTypeOf(lc $name) || $self->default;
67             }
68              
69             42;
70              
71             __END__