1 | package Mojolicious::Types;
|
---|
2 | use Mojo::Base -base;
|
---|
3 |
|
---|
4 | has mapping => sub {
|
---|
5 | {
|
---|
6 | appcache => ['text/cache-manifest'],
|
---|
7 | atom => ['application/atom+xml'],
|
---|
8 | bin => ['application/octet-stream'],
|
---|
9 | css => ['text/css'],
|
---|
10 | gif => ['image/gif'],
|
---|
11 | gz => ['application/x-gzip'],
|
---|
12 | htm => ['text/html'],
|
---|
13 | html => ['text/html;charset=UTF-8'],
|
---|
14 | ico => ['image/x-icon'],
|
---|
15 | jpeg => ['image/jpeg'],
|
---|
16 | jpg => ['image/jpeg'],
|
---|
17 | js => ['application/javascript'],
|
---|
18 | json => ['application/json;charset=UTF-8'],
|
---|
19 | mp3 => ['audio/mpeg'],
|
---|
20 | mp4 => ['video/mp4'],
|
---|
21 | ogg => ['audio/ogg'],
|
---|
22 | ogv => ['video/ogg'],
|
---|
23 | pdf => ['application/pdf'],
|
---|
24 | png => ['image/png'],
|
---|
25 | rss => ['application/rss+xml'],
|
---|
26 | svg => ['image/svg+xml'],
|
---|
27 | txt => ['text/plain;charset=UTF-8'],
|
---|
28 | webm => ['video/webm'],
|
---|
29 | woff => ['application/font-woff'],
|
---|
30 | xml => ['application/xml', 'text/xml'],
|
---|
31 | zip => ['application/zip']
|
---|
32 | };
|
---|
33 | };
|
---|
34 |
|
---|
35 | sub detect {
|
---|
36 | my ($self, $accept) = @_;
|
---|
37 |
|
---|
38 | # Extract and prioritize MIME types
|
---|
39 | my %types;
|
---|
40 | /^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/i
|
---|
41 | and $types{lc $1} = $2 // 1
|
---|
42 | for split ',', $accept // '';
|
---|
43 | my @detected = sort { $types{$b} <=> $types{$a} } sort keys %types;
|
---|
44 |
|
---|
45 | # Detect extensions from MIME types
|
---|
46 | my %reverse;
|
---|
47 | my $mapping = $self->mapping;
|
---|
48 | for my $ext (sort keys %$mapping) {
|
---|
49 | my @types = @{$mapping->{$ext}};
|
---|
50 | push @{$reverse{$_}}, $ext for map { s/\;.*$//; lc $_ } @types;
|
---|
51 | }
|
---|
52 |
|
---|
53 | return [map { @{$reverse{$_} // []} } @detected];
|
---|
54 | }
|
---|
55 |
|
---|
56 | sub type {
|
---|
57 | my ($self, $ext, $type) = @_;
|
---|
58 | return $self->mapping->{lc $ext}[0] unless $type;
|
---|
59 | $self->mapping->{lc $ext} = ref $type ? $type : [$type];
|
---|
60 | return $self;
|
---|
61 | }
|
---|
62 |
|
---|
63 | 1;
|
---|
64 |
|
---|
65 | =encoding utf8
|
---|
66 |
|
---|
67 | =head1 NAME
|
---|
68 |
|
---|
69 | Mojolicious::Types - MIME types
|
---|
70 |
|
---|
71 | =head1 SYNOPSIS
|
---|
72 |
|
---|
73 | use Mojolicious::Types;
|
---|
74 |
|
---|
75 | my $types = Mojolicious::Types->new;
|
---|
76 | $types->type(foo => 'text/foo');
|
---|
77 | say $types->type('foo');
|
---|
78 |
|
---|
79 | =head1 DESCRIPTION
|
---|
80 |
|
---|
81 | L<Mojolicious::Types> manages MIME types for L<Mojolicious>.
|
---|
82 |
|
---|
83 | appcache -> text/cache-manifest
|
---|
84 | atom -> application/atom+xml
|
---|
85 | bin -> application/octet-stream
|
---|
86 | css -> text/css
|
---|
87 | gif -> image/gif
|
---|
88 | gz -> application/x-gzip
|
---|
89 | htm -> text/html
|
---|
90 | html -> text/html;charset=UTF-8
|
---|
91 | ico -> image/x-icon
|
---|
92 | jpeg -> image/jpeg
|
---|
93 | jpg -> image/jpeg
|
---|
94 | js -> application/javascript
|
---|
95 | json -> application/json;charset=UTF-8
|
---|
96 | mp3 -> audio/mpeg
|
---|
97 | mp4 -> video/mp4
|
---|
98 | ogg -> audio/ogg
|
---|
99 | ogv -> video/ogg
|
---|
100 | pdf -> application/pdf
|
---|
101 | png -> image/png
|
---|
102 | rss -> application/rss+xml
|
---|
103 | svg -> image/svg+xml
|
---|
104 | txt -> text/plain;charset=UTF-8
|
---|
105 | webm -> video/webm
|
---|
106 | woff -> application/font-woff
|
---|
107 | xml -> application/xml,text/xml
|
---|
108 | zip -> application/zip
|
---|
109 |
|
---|
110 | The most common ones are already defined.
|
---|
111 |
|
---|
112 | =head1 ATTRIBUTES
|
---|
113 |
|
---|
114 | L<Mojolicious::Types> implements the following attributes.
|
---|
115 |
|
---|
116 | =head2 mapping
|
---|
117 |
|
---|
118 | my $mapping = $types->mapping;
|
---|
119 | $types = $types->mapping({png => ['image/png']});
|
---|
120 |
|
---|
121 | MIME type mapping.
|
---|
122 |
|
---|
123 | =head1 METHODS
|
---|
124 |
|
---|
125 | L<Mojolicious::Types> inherits all methods from L<Mojo::Base> and implements
|
---|
126 | the following new ones.
|
---|
127 |
|
---|
128 | =head2 detect
|
---|
129 |
|
---|
130 | my $exts = $types->detect('text/html, application/json;q=9');
|
---|
131 |
|
---|
132 | Detect file extensions from C<Accept> header value.
|
---|
133 |
|
---|
134 | # List detected extensions prioritized
|
---|
135 | say for @{$types->detect('application/json, text/xml;q=0.1', 1)};
|
---|
136 |
|
---|
137 | =head2 type
|
---|
138 |
|
---|
139 | my $type = $types->type('png');
|
---|
140 | $types = $types->type(png => 'image/png');
|
---|
141 | $types = $types->type(json => ['application/json', 'text/x-json']);
|
---|
142 |
|
---|
143 | Get or set MIME types for file extension, alternatives are only used for
|
---|
144 | detection.
|
---|
145 |
|
---|
146 | =head1 SEE ALSO
|
---|
147 |
|
---|
148 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
149 |
|
---|
150 | =cut
|
---|