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