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.3521';
5 189     189   36065 use strict;
  189         447  
  189         5377  
6 189     189   971 use warnings;
  189         435  
  189         4775  
7 189     189   938 use base 'Dancer::Object::Singleton';
  189         417  
  189         23822  
8              
9 189     189   8270 use Dancer::Config;
  189         527  
  189         7424  
10              
11 189     189   1135 use Carp;
  189         611  
  189         10046  
12 189     189   86064 use MIME::Types;
  189         803942  
  189         10422  
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   1174 MIME::Types->new(only_complete => 1);
21             }
22              
23             __PACKAGE__->attributes( qw/mime_type custom_types/ );
24              
25             sub init {
26 18     18 1 274 my ($class, $instance) = @_;
27              
28 18         653 $instance->mime_type(MIME::Types->new(only_complete => 1));
29 18         309 $instance->custom_types({});
30             }
31              
32             sub default {
33 8     8 0 82 my $instance = shift;
34 8   100     29 return Dancer::Config::setting("default_mime_type") || "application/data";
35             }
36              
37             sub add_type {
38 6     6 1 15 my ($self, $name, $type) = @_;
39 6         18 $self->custom_types->{$name} = $type;
40 6         14 return;
41             }
42              
43             sub add_alias {
44 4     4 1 13 my($self, $alias, $orig) = @_;
45 4         10 my $type = $self->for_name($orig);
46 4         50 $self->add_type($alias, $type);
47 4         32 return $type;
48             }
49              
50             sub for_file {
51 17     17 1 47 my ($self, $filename) = @_;
52 17         125 my ($ext) = $filename =~ /\.([^.]+)$/;
53 17 50       58 return $self->default unless $ext;
54 17         52 return $self->for_name($ext);
55             }
56              
57             sub name_or_type {
58 26     26 1 63 my($self, $name) = @_;
59              
60 26 100       196 return $name if $name =~ m{/}; # probably a mime type
61 3         12 return $self->for_name($name);
62             }
63              
64             sub for_name {
65 31     31 1 230 my ($self, $name) = @_;
66 31   66     386 return $self->custom_types->{lc $name} || $self->mime_type->mimeTypeOf(lc $name) || $self->default;
67             }
68              
69             42;
70              
71             __END__