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
|
# This code is part of Perl distribution MIME-Types version 2.29.
# The POD got stripped from this file by OODoc version 3.05.
# 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.
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
#oodist: This file contains OODoc-style documentation which will get stripped
#oodist: during its release in the distribution. You can use this file for
#oodist: testing, however the code of this development version may be broken!
package MojoX::MIME::Types;{
our $VERSION = '2.29';
}
use Mojo::Base -base;
use MIME::Types ();
#--------------------
sub new(%)
{ # base new() constructor incorrect: should call init()
my $self = shift->SUPER::new(@_);
$self->{MMT_mt} = delete $self->{mime_types} || MIME::Types->new;
$self;
}
#--------------------
sub mimeTypes() { $_[0]->{MMT_mt} }
sub mapping(;$)
{ my $self = shift;
return $self->{MMT_ext} if $self->{MMT_ext};
my %exttable;
my $t = MIME::Types->_MojoExtTable;
while(my ($ext, $type) = each %$t) { $exttable{$ext} = [$type] }
$self->{MMT_ext} = \%exttable;
}
*types = \&mapping; # renamed in release 6.0
#--------------------
sub detect($$;$)
{ my ($self, $accept, $prio) = @_;
my $mt = $self->mimeTypes;
my @ext = map $_->extensions, grep defined, map $mt->type($_),
grep !/\*/, $mt->httpAccept($accept);
\@ext;
}
sub type($;$)
{ my ($self, $ext, $types) = @_;
my $mt = $self->mimeTypes;
defined $types
or return $mt->mimeTypeOf($ext);
# stupid interface compatibility!
$self;
}
sub file_type($) {
my ($self, $fn) = @_;
my $mt = $self->mimeTypes or return undef;
$mt->mimeTypeOf($fn);
}
sub content_type($;$) {
my ($self, $c, $opts) = @_;
my $headers = $c->res->headers;
return undef if $headers->content_type;
my $fn = $opts->{file} || $opts->{ext};
my $mt = $self->mimeTypes or return undef;
$headers->content_type($mt->mimeTypeOf($fn) || $mt->mimeTypeOf('txt'));
}
#--------------------
1;
|