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.3514_04'; # TRIAL
5             $Dancer::MIME::VERSION = '1.351404';
6 190     190   32578 use strict;
  190         361  
  190         4620  
7 190     190   799 use warnings;
  190         334  
  190         4015  
8 190     190   749 use base 'Dancer::Object::Singleton';
  190         327  
  190         19111  
9              
10 190     190   6729 use Dancer::Config;
  190         390  
  190         6085  
11              
12 190     190   1012 use Carp;
  190         392  
  190         8705  
13 190     190   70189 use MIME::Types;
  190         673026  
  190         9372  
14              
15             # Initialise MIME::Types at compile time, to ensure it's done before
16             # the fork in a preforking webserver like mod_perl or Starman. Not
17             # doing this leads to all MIME types being returned as "text/plain",
18             # as MIME::Types fails to load its mappings from the DATA handle. See
19             # t/04_static_file/003_mime_types_reinit.t and GH#136.
20             BEGIN {
21 190     190   993 MIME::Types->new(only_complete => 1);
22             }
23              
24             __PACKAGE__->attributes( qw/mime_type custom_types/ );
25              
26             sub init {
27 18     18 1 207 my ($class, $instance) = @_;
28              
29 18         387 $instance->mime_type(MIME::Types->new(only_complete => 1));
30 18         148 $instance->custom_types({});
31             }
32              
33             sub default {
34 8     8 0 63 my $instance = shift;
35 8   100     25 return Dancer::Config::setting("default_mime_type") || "application/data";
36             }
37              
38             sub add_type {
39 6     6 1 10 my ($self, $name, $type) = @_;
40 6         15 $self->custom_types->{$name} = $type;
41 6         11 return;
42             }
43              
44             sub add_alias {
45 4     4 1 10 my($self, $alias, $orig) = @_;
46 4         8 my $type = $self->for_name($orig);
47 4         45 $self->add_type($alias, $type);
48 4         11 return $type;
49             }
50              
51             sub for_file {
52 17     17 1 42 my ($self, $filename) = @_;
53 17         111 my ($ext) = $filename =~ /\.([^.]+)$/;
54 17 50       49 return $self->default unless $ext;
55 17         44 return $self->for_name($ext);
56             }
57              
58             sub name_or_type {
59 26     26 1 47 my($self, $name) = @_;
60              
61 26 100       147 return $name if $name =~ m{/}; # probably a mime type
62 3         8 return $self->for_name($name);
63             }
64              
65             sub for_name {
66 31     31 1 132 my ($self, $name) = @_;
67 31   66     158 return $self->custom_types->{lc $name} || $self->mime_type->mimeTypeOf(lc $name) || $self->default;
68             }
69              
70             42;
71              
72             __END__