File Coverage

blib/lib/Dancer2/Core/MIME.pm
Criterion Covered Total %
statement 36 45 80.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 12 14 85.7
pod 7 7 100.0
total 62 75 82.6


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.1.0';
5 165     165   337270 use Moo;
  165         25067  
  165         2467  
6 165     165   82618 use Carp ();
  165         2438  
  165         4739  
7              
8 165     165   9052 use Plack::MIME;
  165         21422  
  165         6289  
9 165     165   6125 use Dancer2::Core::Types;
  165         475  
  165         1639  
10 165     165   2231387 use Module::Runtime 'require_module';
  165         22799  
  165         1650  
11              
12             # Initialise MIME::Types at compile time, to ensure it's done before
13             # the fork in a preforking webserver like mod_perl or Starman. Not
14             # doing this leads to all MIME types being returned as "text/plain",
15             # as MIME::Types fails to load its mappings from the DATA handle. See
16             # t/04_static_file/003_mime_types_reinit.t and GH#136.
17             BEGIN {
18 165 50   165   27935 if ( eval { require_module('MIME::Types'); 1; } ) {
  165         1032  
  0         0  
19 0         0 my $mime_types = MIME::Types->new(only_complete => 1);
20             Plack::MIME->set_fallback(
21             sub {
22 0         0 $mime_types->mimeTypeOf($_[0])
23             }
24 0         0 );
25             }
26             }
27              
28 165     165   46233 use constant { 'DEFAULT_MIME_TYPE' => 'application/data' };
  165         421  
  165         108659  
29              
30             has custom_types => (
31             is => 'ro',
32             isa => HashRef,
33             default => sub { +{} },
34             );
35              
36             has default => (
37             is => 'rw',
38             isa => Str,
39             default => sub { DEFAULT_MIME_TYPE() },
40             );
41              
42             sub reset_to_default {
43 0     0 1 0 my ($self) = @_;
44 0         0 $self->default( DEFAULT_MIME_TYPE() );
45             }
46              
47             sub reset_default {
48 0     0 1 0 my ($self) = @_;
49 0         0 Carp::carp 'DEPRECATED: reset_default internal method, replaced by reset_to_default';
50 0         0 goto &reset_to_default;
51             }
52              
53             sub add_type {
54 7     7 1 1035 my ( $self, $name, $type ) = @_;
55 7         28 $self->custom_types->{$name} = $type;
56 7         16 return;
57             }
58              
59             sub add_alias {
60 5     5 1 835 my ( $self, $alias, $orig ) = @_;
61 5         16 my $type = $self->for_name($orig);
62 5         70 $self->add_type( $alias, $type );
63 5         23 return $type;
64             }
65              
66             sub for_file {
67 19     19 1 62 my ( $self, $filename ) = @_;
68 19         131 my ($ext) = $filename =~ /\.([^.]+)$/;
69 19 100       262 return $self->default unless $ext;
70 18         96 return $self->for_name($ext);
71             }
72              
73             sub name_or_type {
74 1194     1194 1 4109 my ( $self, $name ) = @_;
75              
76 1194 100       6276 return $name if $name =~ m{/}; # probably a mime type
77 1         3 return $self->for_name($name);
78             }
79              
80             sub for_name {
81 26     26 1 792 my ( $self, $name ) = @_;
82              
83             return
84 26   66     403 $self->custom_types->{ lc $name }
85             || Plack::MIME->mime_type( lc ".$name" )
86             || $self->default;
87             }
88              
89             1;
90              
91             __END__