File: Types.pod

package info (click to toggle)
libmime-types-perl 2.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 316 kB
  • sloc: perl: 634; makefile: 2
file content (228 lines) | stat: -rw-r--r-- 6,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
=encoding utf8

=head1 NAME

MojoX::MIME::Types - MIME Types for Mojolicious

=head1 INHERITANCE

 MojoX::MIME::Types
   is a Mojo::Base

=head1 SYNOPSIS

  use MojoX::MIME::Types;

  # set in Mojolicious as default
  $app->types(MojoX::MIME::Types->new);
  app->types(MojoX::MIME::Types->new);   # ::Lite

  # basic interface translated into pure MIME::Types
  $types->type(foo => 'text/foo');
  say $types->type('foo');

=head1 DESCRIPTION

[Added to L<MIME::Types|MIME::Types> 2.07]
This module is a drop-in replacement for Mojolicious::Types, but
with a more correct handling plus a complete list of types... a huge
list of types.

Some methods ignore information they receive: those parameters are
accepted for compatibility with the Mojolicious::Types interface,
but should not contain useful information.

Read the L</DETAILS> below, about how to connect this module into
Mojolicious and the differences you get.

=head1 METHODS

=head2 Constructors

=over 4

=item $class-E<gt>B<new>(%options)

Create the 'type' handler for Mojolicious.  When you do not specify your
own L<MIME::Type|MIME::Type> object (C<mime_types>), it will be instantanted for you.
You create one yourself when you would like to pass some parameter to
the object constructor.

 -Option    --Default
  mime_types  <created internally>
  types       undef

=over 2

=item mime_types => MIME::Types-object

Pass your own prepared L<MIME::Types|MIME::Types> object, when you need some
instantiation parameters different from the defaults.

=item types => HASH

Ignored.

=back

example: 

  $app->types(MojoX::MIME::Types->new);

  # when you need to pass options to MIME::Types->new
  my $mt    = MIME::Types->new(%opts);
  my $types = MojoX::MIME::Types->new(mime_types => $mt);
  $app->types($types);

=back

=head2 Attributes

=over 4

=item $obj-E<gt>B<mapping>( [\%table] )

In Mojolicious::Types, this attribute exposes the internal
administration of types, offering to change it with using a clean
abstract interface.  That interface mistake bites now we have more
complex internals.

B<Avoid this method!>  The returned HASH is expensive to construct,
changes passed via C<%table> are ignored: L<MIME::Types|MIME::Types> is very complete!

=item $obj-E<gt>B<mimeTypes>()

Returns the internal mime types object.

=back

=head2 Actions

=over 4

=item $obj-E<gt>B<content_type>($controller, \%options)

Set a content type on the controller when not yet set.
The C<%options> contains C<ext> or C<file> specify an file extension or file
name which is used to derive the content type.
Added and marked EXPERIMENTAL in Mojo 7.94.

=item $obj-E<gt>B<detect>( $accept, [$prio] )

Returns a list of filename extensions.  The C<$accept> header in HTTP can
contain multiple types, with a priority indication ('q' attributes).
The returned list contains a list with extensions, the extensions related
to the highest priority type first.  The C<$prio>-flag is ignored.
See L<MIME::Types::httpAccept()|MIME::Types/"HTTP support">.

This detect() function is not the correct approach for the Accept header:
the "Accept" may contain wildcards ('*') in types for globbing, which
does not produce extensions.  Better use L<MIME::Types::httpAcceptBest()|MIME::Types/"HTTP support">
or L<MIME::Types::httpAcceptSelect()|MIME::Types/"HTTP support">.

example: 

  my $exts = $types->detect('application/json;q=9');
  my $exts = $types->detect('text/html, application/json;q=9');

=item $obj-E<gt>B<file_type>($filename)

Return the mime type for a filename.
Added and marked EXPERIMENTAL in Mojo 7.94.

=item $obj-E<gt>B<type>( $ext, [$type|\@types] )

Returns the first type name for an extension C<$ext>, unless you specify
type names.

When a single C<$type> or an ARRAY of C<@types> are specified, the current
object is returned.  Nothing is done with the provided info.

=back

=head1 DETAILS

=head2 Why?

The Mojolicious::Types module has only very little knowledge about
what is really needed to treat types correctly, and only contains a tiny
list of extensions.  L<MIME::Types|MIME::Types> tries to follow the standards
very closely and contains all types found in various lists on internet.

=head2 How to use with Mojolicious

Start your Mojo application like this:

  package MyApp;
  use Mojo::Base 'Mojolicious';

  sub startup {
     my $self = shift;
     ...
     $self->types(MojoX::MIME::Types->new);
  }

If you have special options for L<MIME::Types::new()|MIME::Types/"Constructors">, then create
your own L<MIME::Types|MIME::Types> object first:

  my $mt    = MIME::Types->new(%opts);
  my $types = MojoX::MIME::Types->new(mime_types => $mt);
  $self->types($types);

In any case, you can reach the smart L<MIME::Types|MIME::Types> object later as

  my $mt    = $app->types->mimeTypes;
  my $mime  = $mt->mimeTypeOf($filename);

=head2 How to use with Mojolicious::Lite

The use in Mojolicious::Lite applications is only slightly different
from above:

  app->types(MojoX::MIME::Types->new);
  my $types = app->types;

=head2 Differences with Mojolicious::Types

There are a few major difference with Mojolicious::Types:

=over 4

=item *

the tables maintained by L<MIME::Types|MIME::Types> are complete.  So: there shouldn't
be a need to add your own types, not via C<types()>, not via C<type()>.
All attempts to add types are ignored; better remove them from your code.

=item *

This plugin understands the experimental flag 'x-' in types and handles
casing issues.

=item *

Updates to the internal hash via types() are simply ignored, because it
is expensive to implement (and won't add something new).

=item *

The L<detect()|MojoX::MIME::Types/"Actions"> is implemented in a compatible way, but does not understand
wildcards ('*').  You should use L<MIME::Types::httpAcceptBest()|MIME::Types/"HTTP support"> or
L<MIME::Types::httpAcceptSelect()|MIME::Types/"HTTP support"> to replace this broken function.

=back

=head1 SEE ALSO

This module is part of MIME-Types version 2.29,
built on September 15, 2025. Website: F<http://perl.overmeer.net/CPAN/>

=head1 LICENSE

For contributors see file ChangeLog.

This software is copyright (c) 1999-2025 by Mark Overmeer.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.