| 1 | # Copyrights 1999-2022 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||
| 5 | # This code is part of distribution MIME::Types. Meta-POD processed with | ||
| 6 | # OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package MojoX::MIME::Types; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '2.23'; | ||
| 12 | |||
| 13 | use Mojo::Base -base; | ||
| 14 | |||
| 15 | use MIME::Types (); | ||
| 16 | |||
| 17 | |||
| 18 | sub new(%) | ||
| 19 | { # base new() constructor incorrect: should call init() | ||
| 20 | my $self = shift->SUPER::new(@_); | ||
| 21 | $self->{MMT_mt} = delete $self->{mime_types} || MIME::Types->new; | ||
| 22 | $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | #---------- | ||
| 26 | |||
| 27 | sub mimeTypes() { shift->{MMT_mt} } | ||
| 28 | |||
| 29 | |||
| 30 | sub mapping(;$) | ||
| 31 | { my $self = shift; | ||
| 32 | return $self->{MMT_ext} if $self->{MMT_ext}; | ||
| 33 | |||
| 34 | my %exttable; | ||
| 35 | my $t = MIME::Types->_MojoExtTable; | ||
| 36 | while(my ($ext, $type) = each %$t) { $exttable{$ext} = [$type] } | ||
| 37 | $self->{MMT_ext} = \%exttable; | ||
| 38 | } | ||
| 39 | |||
| 40 | *types = \&mapping; # renamed in release 6.0 | ||
| 41 | |||
| 42 | #---------- | ||
| 43 | |||
| 44 | sub detect($$;$) | ||
| 45 | { my ($self, $accept, $prio) = @_; | ||
| 46 | my $mt = $self->mimeTypes; | ||
| 47 | my @ext = map $_->extensions, grep defined, map $mt->type($_), | ||
| 48 | grep !/\*/, $mt->httpAccept($accept); | ||
| 49 | \@ext; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub type($;$) | ||
| 54 | { my ($self, $ext, $types) = @_; | ||
| 55 | |||
| 56 | my $mt = $self->mimeTypes; | ||
| 57 | defined $types | ||
| 58 | or return $mt->mimeTypeOf($ext); | ||
| 59 | |||
| 60 | # stupid interface compatibility! | ||
| 61 | $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub file_type($) { | ||
| 66 | my ($self, $fn) = @_; | ||
| 67 | my $mt = $self->mimeTypes or return undef; | ||
| 68 | $mt->mimeTypeOf($fn); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub content_type($;$) { | ||
| 73 | my ($self, $c, $opts) = @_; | ||
| 74 | my $headers = $c->res->headers; | ||
| 75 | return undef if $headers->content_type; | ||
| 76 | |||
| 77 | my $fn = $opts->{file} || $opts->{ext}; | ||
| 78 | |||
| 79 | my $mt = $self->mimeTypes or return undef; | ||
| 80 | $headers->content_type($mt->mimeTypeOf($fn) || $mt->mimeTypeOf('txt')); | ||
| 81 | } | ||
| 82 | |||
| 83 | #--------------- | ||
| 84 | |||
| 85 | 1; |