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 = '1.0.0';
5 146     146   66908 use Moo;
  146         7529  
  146         898  
6              
7 146     146   51031 use Plack::MIME;
  146         4479  
  146         4610  
8 146     146   3103 use Dancer2::Core::Types;
  146         7764  
  146         1301  
9 146     146   1897086 use Module::Runtime 'require_module';
  146         2135  
  146         6201  
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 146 50   146   19193 if ( eval { require_module('MIME::Types'); 1; } ) {
  146         1062  
  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 168     168 1 5646 my ($self) = @_;
41 168         3531 $self->default("application/data");
42             }
43              
44             sub add_type {
45 7     7 1 860 my ( $self, $name, $type ) = @_;
46 7         26 $self->custom_types->{$name} = $type;
47 7         15 return;
48             }
49              
50             sub add_alias {
51 5     5 1 522 my ( $self, $alias, $orig ) = @_;
52 5         15 my $type = $self->for_name($orig);
53 5         80 $self->add_type( $alias, $type );
54 5         30 return $type;
55             }
56              
57             sub for_file {
58 19     19 1 55 my ( $self, $filename ) = @_;
59 19         137 my ($ext) = $filename =~ /\.([^.]+)$/;
60 19 100       119 return $self->default unless $ext;
61 17         60 return $self->for_name($ext);
62             }
63              
64             sub name_or_type {
65 1133     1133 1 2996 my ( $self, $name ) = @_;
66              
67 1133 100       5102 return $name if $name =~ m{/}; # probably a mime type
68 1         3 return $self->for_name($name);
69             }
70              
71             sub for_name {
72 25     25 1 576 my ( $self, $name ) = @_;
73              
74             return
75 25   66     270 $self->custom_types->{ lc $name }
76             || Plack::MIME->mime_type( lc ".$name" )
77             || $self->default;
78             }
79              
80             1;
81              
82             __END__
83              
84             =pod
85              
86             =encoding UTF-8
87              
88             =head1 NAME
89              
90             Dancer2::Core::MIME - Class to ease manipulation of MIME types
91              
92             =head1 VERSION
93              
94             version 1.0.0
95              
96             =head1 SYNOPSIS
97              
98             use Dancer2::Core::MIME;
99              
100             my $mime = Dancer2::Core::MIME->new();
101              
102             # get mime type for an alias
103             my $type = $mime->for_name('css');
104              
105             # set a new mime type
106             my $type = $mime->add_type( foo => 'text/foo' );
107              
108             # set a mime type alias
109             my $alias = $mime->add_alias( f => 'foo' );
110              
111             # get mime type for a file (based on extension)
112             my $file = $mime->for_file( "foo.bar" );
113              
114             # set the $thing into a content $type.
115             my $type = $mime->name_or_type($thing);
116              
117             # get current defined default mime type
118             my $type = $mime->default;
119              
120             # set the default mime type using config.yml
121             # or using the set keyword
122             set default_mime_type => 'text/plain';
123              
124             =head1 DESCRIPTION
125              
126             Dancer2::Core::MIME is a thin wrapper around L<MIME::Types> providing helpful
127             methods for MIME handling.
128              
129             =head1 ATTRIBUTES
130              
131             =head2 custom_types
132              
133             Custom user-defined MIME types that are added the with C<add_type>.
134              
135             =head2 default
136              
137             Default MIME type defined by MIME::Types, set to: B<application/data>.
138              
139             =head1 METHODS
140              
141             =head2 reset_default
142              
143             This method resets C<mime_type> to the default type.
144              
145             =head2 add_type
146              
147             This method adds the new MIME type.
148              
149             =head2 add_alias
150              
151             The C<add_alias> sets a MIME type alias.
152              
153             =head2 for_name
154              
155             The method C<for_name> gets MIME type for an alias.
156              
157             =head2 for_file
158              
159             This method gets MIME type for a file based on extension.
160              
161             =head2 name_or_type
162              
163             This method sets the customized MIME name or default MIME type into a content
164             type.
165              
166             =head1 AUTHOR
167              
168             Dancer Core Developers
169              
170             =head1 COPYRIGHT AND LICENSE
171              
172             This software is copyright (c) 2023 by Alexis Sukrieh.
173              
174             This is free software; you can redistribute it and/or modify it under
175             the same terms as the Perl 5 programming language system itself.
176              
177             =cut