File Coverage

blib/lib/Dancer2/Core/MIME.pm
Criterion Covered Total %
statement 32 36 88.8
branch 5 6 83.3
condition 2 3 66.6
subroutine 11 11 100.0
pod 6 6 100.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Class to ease manipulation of MIME types
2              
3             package Dancer2::Core::MIME;
4             $Dancer2::Core::MIME::VERSION = '2.0.1';
5 159     159   360989 use Moo;
  159         20009  
  159         1130  
6              
7 159     159   75717 use Plack::MIME;
  159         10656  
  159         5900  
8 159     159   4872 use Dancer2::Core::Types;
  159         368  
  159         1991  
9 159     159   2416124 use Module::Runtime 'require_module';
  159         6654  
  159         1612  
10              
11             # Initialise MIME::Types at compile time, to ensure it's done before
12             # the fork in a preforking webserver like mod_perl or Starman. Not
13             # doing this leads to all MIME types being returned as "text/plain",
14             # as MIME::Types fails to load its mappings from the DATA handle. See
15             # t/04_static_file/003_mime_types_reinit.t and GH#136.
16             BEGIN {
17 159 50   159   25836 if ( eval { require_module('MIME::Types'); 1; } ) {
  159         895  
  0         0  
18 0         0 my $mime_types = MIME::Types->new(only_complete => 1);
19             Plack::MIME->set_fallback(
20             sub {
21 0         0 $mime_types->mimeTypeOf($_[0])
22             }
23 0         0 );
24             }
25             }
26              
27             has custom_types => (
28             is => 'ro',
29             isa => HashRef,
30             default => sub { +{} },
31             );
32              
33             has default => (
34             is => 'rw',
35             isa => Str,
36             builder => "reset_default",
37             );
38              
39             sub reset_default {
40 171     171 1 5871 my ($self) = @_;
41 171         4609 $self->default("application/data");
42             }
43              
44             sub add_type {
45 7     7 1 1190 my ( $self, $name, $type ) = @_;
46 7         33 $self->custom_types->{$name} = $type;
47 7         19 return;
48             }
49              
50             sub add_alias {
51 5     5 1 2929 my ( $self, $alias, $orig ) = @_;
52 5         21 my $type = $self->for_name($orig);
53 5         126 $self->add_type( $alias, $type );
54 5         42 return $type;
55             }
56              
57             sub for_file {
58 19     19 1 71 my ( $self, $filename ) = @_;
59 19         157 my ($ext) = $filename =~ /\.([^.]+)$/;
60 19 100       139 return $self->default unless $ext;
61 17         79 return $self->for_name($ext);
62             }
63              
64             sub name_or_type {
65 1189     1189 1 4719 my ( $self, $name ) = @_;
66              
67 1189 100       6129 return $name if $name =~ m{/}; # probably a mime type
68 1         5 return $self->for_name($name);
69             }
70              
71             sub for_name {
72 25     25 1 790 my ( $self, $name ) = @_;
73              
74             return
75 25   66     363 $self->custom_types->{ lc $name }
76             || Plack::MIME->mime_type( lc ".$name" )
77             || $self->default;
78             }
79              
80             1;
81              
82             __END__