1MojoX::MIME::Types(3) User Contributed Perl DocumentationMojoX::MIME::Types(3)
2
3
4
6 MojoX::MIME::Types - MIME Types for Mojolicious
7
9 MojoX::MIME::Types
10 is a Mojo::Base
11
13 use MojoX::MIME::Types;
14
15 # set in Mojolicious as default
16 $app->types(MojoX::MIME::Types->new);
17 app->types(MojoX::MIME::Types->new); # ::Lite
18
19 # basic interface translated into pure MIME::Types
20 $types->type(foo => 'text/foo');
21 say $types->type('foo');
22
24 [Added to MIME::Types 2.07] This module is a drop-in replacement for
25 Mojolicious::Types, but with a more correct handling plus a complete
26 list of types... a huge list of types.
27
28 Some methods ignore information they receive: those parameters are
29 accepted for compatibility with the Mojolicious::Types interface, but
30 should not contain useful information.
31
32 Read the "DETAILS" below, about how to connect this module into
33 Mojolicious and the differences you get.
34
36 Constructors
37 MojoX::MIME::Types->new(%options)
38 Create the 'type' handler for Mojolicious. When you do not specify
39 your own MIME::Type object ($mime_type), it will be instantanted
40 for you. You create one yourself when you would like to pass some
41 parameter to the object constructor.
42
43 -Option --Default
44 mime_types <created internally>
45 types undef
46
47 mime_types => MIME::Types-object
48 Pass your own prepared MIME::Types object, when you need some
49 instantiation parameters different from the defaults.
50
51 types => HASH
52 Ignored.
53
54 example:
55
56 $app->types(MojoX::MIME::Types->new);
57
58 # when you need to pass options to MIME::Types->new
59 my $mt = MIME::Types->new(%opts);
60 my $types = MojoX::MIME::Types->new(mime_types => $mt);
61 $app->types($types);
62
63 Attributes
64 $obj->mapping( [\%table] )
65 In Mojolicious::Types, this attribute exposes the internal
66 administration of types, offering to change it with using a clean
67 abstract interface. That interface mistake bites now we have more
68 complex internals.
69
70 Avoid this method! The returned HASH is expensive to construct,
71 changes passed via %table are ignored: MIME::Types is very
72 complete!
73
74 $obj->mimeTypes()
75 Returns the internal mime types object.
76
77 Actions
78 $obj->content_type($controller, \%options)
79 Set a content type on the controller when not yet set. The
80 %options contains "ext" or "file" specify an file extension or file
81 name which is used to derive the content type. Added and marked
82 EXPERIMENTAL in Mojo 7.94.
83
84 $obj->detect( $accept, [$prio] )
85 Returns a list of filename extensions. The $accept header in HTTP
86 can contain multiple types, with a priority indication ('q'
87 attributes). The returned list contains a list with extensions,
88 the extensions related to the highest priority type first. The
89 $prio-flag is ignored. See MIME::Types::httpAccept().
90
91 This detect() function is not the correct approach for the Accept
92 header: the "Accept" may contain wildcards ('*') in types for
93 globbing, which does not produce extensions. Better use
94 MIME::Types::httpAcceptBest() or MIME::Types::httpAcceptSelect().
95
96 example:
97
98 my $exts = $types->detect('application/json;q=9');
99 my $exts = $types->detect('text/html, application/json;q=9');
100
101 $obj->file_type($filename)
102 Return the mime type for a filename. Added and marked EXPERIMENTAL
103 in Mojo 7.94.
104
105 $obj->type( $ext, [$type|\@types] )
106 Returns the first type name for an extension $ext, unless you
107 specify type names.
108
109 When a single $type or an ARRAY of @types are specified, the $self
110 object is returned. Nothing is done with the provided info.
111
113 Why?
114 The Mojolicious::Types module has only very little knowledge about what
115 is really needed to treat types correctly, and only contains a tiny
116 list of extensions. MIME::Types tries to follow the standards very
117 closely and contains all types found in various lists on internet.
118
119 How to use with Mojolicious
120 Start your Mojo application like this:
121
122 package MyApp;
123 use Mojo::Base 'Mojolicious';
124
125 sub startup {
126 my $self = shift;
127 ...
128 $self->types(MojoX::MIME::Types->new);
129 }
130
131 If you have special options for MIME::Types::new(), then create your
132 own MIME::Types object first:
133
134 my $mt = MIME::Types->new(%opts);
135 my $types = MojoX::MIME::Types->new(mime_types => $mt);
136 $self->types($types);
137
138 In any case, you can reach the smart MIME::Types object later as
139
140 my $mt = $app->types->mimeTypes;
141 my $mime = $mt->mimeTypeOf($filename);
142
143 How to use with Mojolicious::Lite
144 The use in Mojolicious::Lite applications is only slightly different
145 from above:
146
147 app->types(MojoX::MIME::Types->new);
148 my $types = app->types;
149
150 Differences with Mojolicious::Types
151 There are a few major difference with Mojolicious::Types:
152
153 • the tables maintained by MIME::Types are complete. So: there
154 shouldn't be a need to add your own types, not via types(), not via
155 type(). All attempts to add types are ignored; better remove them
156 from your code.
157
158 • This plugin understands the experimental flag 'x-' in types and
159 handles casing issues.
160
161 • Updates to the internal hash via types() are simply ignored,
162 because it is expensive to implement (and won't add something new).
163
164 • The detect() is implemented in a compatible way, but does not
165 understand wildcards ('*'). You should use
166 MIME::Types::httpAcceptBest() or MIME::Types::httpAcceptSelect() to
167 replace this broken function.
168
170 This module is part of MIME-Types distribution version 2.24, built on
171 December 28, 2022. Website: http://perl.overmeer.net/CPAN/
172
174 Copyrights 1999-2022 by [Mark Overmeer <markov@cpan.org>]. For other
175 contributors see ChangeLog.
176
177 This program is free software; you can redistribute it and/or modify it
178 under the same terms as Perl itself. See http://dev.perl.org/licenses/
179
180
181
182perl v5.36.0 2023-01-20 MojoX::MIME::Types(3)