Newer Older
152 lines | 4.045kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojolicious::Types;
2
use Mojo::Base -base;
3

            
4
has types => sub {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
5
  {
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
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'],
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'],
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
27
    txt      => ['text/plain;charset=UTF-8'],
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
28
    webm     => ['video/webm'],
29
    woff     => ['application/font-woff'],
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
30
    xml      => ['application/xml', 'text/xml'],
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
31
    zip      => ['application/zip']
copy gitweblite soruce code
root authored on 2012-11-23
32
  };
33
};
34

            
35
sub detect {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
36
  my ($self, $accept, $prioritize) = @_;
37

            
38
  # Extract and prioritize MIME types
39
  my %types;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
40
  /^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/i
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
41
    and $types{lc $1} = defined $2 ? $2 : 1
42
    for split /,/, defined $accept ? $accept : '';
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
43
  my @detected = sort { $types{$b} <=> $types{$a} } sort keys %types;
44
  return [] if !$prioritize && @detected > 1;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
45

            
46
  # Detect extensions from MIME types
47
  my %reverse;
copy gitweblite soruce code
root authored on 2012-11-23
48
  my $types = $self->types;
49
  for my $ext (sort keys %$types) {
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
50
    my @types = @{$types->{$ext}};
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
51
    push @{$reverse{$_}}, $ext for map { s/\;.*$//; lc $_ } @types;
copy gitweblite soruce code
root authored on 2012-11-23
52
  }
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
53
  return [map { @{defined $reverse{$_} ? $reverse{$_} : []} } @detected];
copy gitweblite soruce code
root authored on 2012-11-23
54
}
55

            
56
sub type {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
57
  my ($self, $ext, $type) = @_;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
58
  return $self->types->{$ext}[0] unless $type;
59
  $self->types->{$ext} = ref $type ? $type : [$type];
copy gitweblite soruce code
root authored on 2012-11-23
60
  return $self;
61
}
62

            
63
1;
64

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
65
=encoding utf8
66

            
copy gitweblite soruce code
root authored on 2012-11-23
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;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
76
  $types->type(foo => 'text/foo');
77
  say $types->type('foo');
copy gitweblite soruce code
root authored on 2012-11-23
78

            
79
=head1 DESCRIPTION
80

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
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
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
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
104
  txt      -> text/plain;charset=UTF-8
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
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.
copy gitweblite soruce code
root authored on 2012-11-23
111

            
112
=head1 ATTRIBUTES
113

            
114
L<Mojolicious::Types> implements the following attributes.
115

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
116
=head2 types
copy gitweblite soruce code
root authored on 2012-11-23
117

            
118
  my $map = $types->types;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
119
  $types  = $types->types({png => ['image/png']});
copy gitweblite soruce code
root authored on 2012-11-23
120

            
121
List of MIME types.
122

            
123
=head1 METHODS
124

            
125
L<Mojolicious::Types> inherits all methods from L<Mojo::Base> and implements
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
126
the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
127

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
128
=head2 detect
copy gitweblite soruce code
root authored on 2012-11-23
129

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
130
  my $exts = $types->detect('application/json;q=9');
131
  my $exts = $types->detect('text/html, application/json;q=9', 1);
132

            
133
Detect file extensions from C<Accept> header value, prioritization of
134
unspecific values that contain more than one MIME type is disabled by default.
copy gitweblite soruce code
root authored on 2012-11-23
135

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
136
  # List detected extensions prioritized
137
  say for @{$types->detect('application/json, text/xml;q=0.1', 1)};
copy gitweblite soruce code
root authored on 2012-11-23
138

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
139
=head2 type
copy gitweblite soruce code
root authored on 2012-11-23
140

            
141
  my $type = $types->type('png');
142
  $types   = $types->type(png => 'image/png');
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
143
  $types   = $types->type(json => [qw(application/json text/x-json)]);
copy gitweblite soruce code
root authored on 2012-11-23
144

            
145
Get or set MIME types for file extension, alternatives are only used for
146
detection.
147

            
148
=head1 SEE ALSO
149

            
150
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
151

            
152
=cut