| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ABSTRACT: Class to ease manipulation of MIME types |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$Dancer2::Core::MIME::VERSION = '0.400000'; |
|
4
|
|
|
|
|
|
|
use Moo; |
|
5
|
143
|
|
|
143
|
|
56821
|
|
|
|
143
|
|
|
|
|
6136
|
|
|
|
143
|
|
|
|
|
757
|
|
|
6
|
|
|
|
|
|
|
use Plack::MIME; |
|
7
|
143
|
|
|
143
|
|
42053
|
use Dancer2::Core::Types; |
|
|
143
|
|
|
|
|
3681
|
|
|
|
143
|
|
|
|
|
3899
|
|
|
8
|
143
|
|
|
143
|
|
2514
|
use Module::Runtime 'require_module'; |
|
|
143
|
|
|
|
|
341
|
|
|
|
143
|
|
|
|
|
1376
|
|
|
9
|
143
|
|
|
143
|
|
1165679
|
|
|
|
143
|
|
|
|
|
1870
|
|
|
|
143
|
|
|
|
|
1041
|
|
|
10
|
|
|
|
|
|
|
# Initialise MIME::Types at compile time, to ensure it's done before |
|
11
|
|
|
|
|
|
|
# the fork in a preforking webserver like mod_perl or Starman. Not |
|
12
|
|
|
|
|
|
|
# doing this leads to all MIME types being returned as "text/plain", |
|
13
|
|
|
|
|
|
|
# as MIME::Types fails to load its mappings from the DATA handle. See |
|
14
|
|
|
|
|
|
|
# t/04_static_file/003_mime_types_reinit.t and GH#136. |
|
15
|
|
|
|
|
|
|
BEGIN { |
|
16
|
|
|
|
|
|
|
if ( eval { require_module('MIME::Types'); 1; } ) { |
|
17
|
143
|
50
|
|
143
|
|
18340
|
my $mime_types = MIME::Types->new(only_complete => 1); |
|
|
143
|
|
|
|
|
687
|
|
|
|
0
|
|
|
|
|
0
|
|
|
18
|
0
|
|
|
|
|
0
|
Plack::MIME->set_fallback( |
|
19
|
|
|
|
|
|
|
sub { |
|
20
|
|
|
|
|
|
|
$mime_types->mimeTypeOf($_[0]) |
|
21
|
0
|
|
|
|
|
0
|
} |
|
22
|
|
|
|
|
|
|
); |
|
23
|
0
|
|
|
|
|
0
|
} |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has custom_types => ( |
|
27
|
|
|
|
|
|
|
is => 'ro', |
|
28
|
|
|
|
|
|
|
isa => HashRef, |
|
29
|
|
|
|
|
|
|
default => sub { +{} }, |
|
30
|
|
|
|
|
|
|
); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
has default => ( |
|
33
|
|
|
|
|
|
|
is => 'rw', |
|
34
|
|
|
|
|
|
|
isa => Str, |
|
35
|
|
|
|
|
|
|
builder => "reset_default", |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my ($self) = @_; |
|
39
|
|
|
|
|
|
|
$self->default("application/data"); |
|
40
|
165
|
|
|
165
|
1
|
4395
|
} |
|
41
|
165
|
|
|
|
|
2971
|
|
|
42
|
|
|
|
|
|
|
my ( $self, $name, $type ) = @_; |
|
43
|
|
|
|
|
|
|
$self->custom_types->{$name} = $type; |
|
44
|
|
|
|
|
|
|
return; |
|
45
|
7
|
|
|
7
|
1
|
640
|
} |
|
46
|
7
|
|
|
|
|
19
|
|
|
47
|
7
|
|
|
|
|
11
|
my ( $self, $alias, $orig ) = @_; |
|
48
|
|
|
|
|
|
|
my $type = $self->for_name($orig); |
|
49
|
|
|
|
|
|
|
$self->add_type( $alias, $type ); |
|
50
|
|
|
|
|
|
|
return $type; |
|
51
|
5
|
|
|
5
|
1
|
435
|
} |
|
52
|
5
|
|
|
|
|
11
|
|
|
53
|
5
|
|
|
|
|
65
|
my ( $self, $filename ) = @_; |
|
54
|
5
|
|
|
|
|
24
|
my ($ext) = $filename =~ /\.([^.]+)$/; |
|
55
|
|
|
|
|
|
|
return $self->default unless $ext; |
|
56
|
|
|
|
|
|
|
return $self->for_name($ext); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
19
|
|
|
19
|
1
|
61
|
|
|
59
|
19
|
|
|
|
|
114
|
my ( $self, $name ) = @_; |
|
60
|
19
|
100
|
|
|
|
100
|
|
|
61
|
17
|
|
|
|
|
61
|
return $name if $name =~ m{/}; # probably a mime type |
|
62
|
|
|
|
|
|
|
return $self->for_name($name); |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
1109
|
|
|
1109
|
1
|
2673
|
my ( $self, $name ) = @_; |
|
66
|
|
|
|
|
|
|
|
|
67
|
1109
|
100
|
|
|
|
4093
|
return |
|
68
|
1
|
|
|
|
|
2
|
$self->custom_types->{ lc $name } |
|
69
|
|
|
|
|
|
|
|| Plack::MIME->mime_type( lc ".$name" ) |
|
70
|
|
|
|
|
|
|
|| $self->default; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
25
|
|
|
25
|
1
|
483
|
|
|
73
|
|
|
|
|
|
|
1; |
|
74
|
|
|
|
|
|
|
|
|
75
|
25
|
|
66
|
|
|
258
|
|
|
76
|
|
|
|
|
|
|
=pod |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=encoding UTF-8 |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 NAME |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Dancer2::Core::MIME - Class to ease manipulation of MIME types |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 VERSION |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
version 0.400000 |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
use Dancer2::Core::MIME; |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $mime = Dancer2::Core::MIME->new(); |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# get mime type for an alias |
|
95
|
|
|
|
|
|
|
my $type = $mime->for_name('css'); |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# set a new mime type |
|
98
|
|
|
|
|
|
|
my $type = $mime->add_type( foo => 'text/foo' ); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# set a mime type alias |
|
101
|
|
|
|
|
|
|
my $alias = $mime->add_alias( f => 'foo' ); |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# get mime type for a file (based on extension) |
|
104
|
|
|
|
|
|
|
my $file = $mime->for_file( "foo.bar" ); |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# set the $thing into a content $type. |
|
107
|
|
|
|
|
|
|
my $type = $mime->name_or_type($thing); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# get current defined default mime type |
|
110
|
|
|
|
|
|
|
my $type = $mime->default; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# set the default mime type using config.yml |
|
113
|
|
|
|
|
|
|
# or using the set keyword |
|
114
|
|
|
|
|
|
|
set default_mime_type => 'text/plain'; |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Dancer2::Core::MIME is a thin wrapper around L<MIME::Types> providing helpful |
|
119
|
|
|
|
|
|
|
methods for MIME handling. |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head2 custom_types |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Custom user-defined MIME types that are added the with C<add_type>. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 default |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Default MIME type defined by MIME::Types, set to: B<application/data>. |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 METHODS |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 reset_default |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
This method resets C<mime_type> to the default type. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 add_type |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This method adds the new MIME type. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 add_alias |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
The C<add_alias> sets a MIME type alias. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 for_name |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
The method C<for_name> gets MIME type for an alias. |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 for_file |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This method gets MIME type for a file based on extension. |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 name_or_type |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
This method sets the customized MIME name or default MIME type into a content |
|
156
|
|
|
|
|
|
|
type. |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 AUTHOR |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Dancer Core Developers |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This software is copyright (c) 2022 by Alexis Sukrieh. |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
167
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=cut |