MojoX/MIME/Types.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 83 CC 3 SCAR 12.4
CC
3
Cov
0%
mapping
6.0
content_type
6.0
new
2.0
CRAP 3.5
1 # This code is part of Perl distribution MIME-Types version 2.30.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 1999-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package MojoX::MIME::Types;{
13 our $VERSION = '2.30';
14 }
15  
16 use Mojo::Base -base;
17  
18 use MIME::Types ();
19  
20 #--------------------
21  
22 sub new(%)
23 { # base new() constructor incorrect: should call init()
24 my $self = shift->SUPER::new(@_);
25 $self->{MMT_mt} = delete $self->{mime_types} || MIME::Types->new;
26 $self;
27 }
28  
29 #--------------------
30  
31 sub mimeTypes() { $_[0]->{MMT_mt} }
32  
33  
34 sub mapping(;$)
35 { my $self = shift;
36 return $self->{MMT_ext} if $self->{MMT_ext};
37  
38 my %exttable;
39 my $t = MIME::Types->_MojoExtTable;
40 while(my ($ext, $type) = each %$t) { $exttable{$ext} = [$type] }
41 $self->{MMT_ext} = \%exttable;
42 }
43  
44 *types = \&mapping; # renamed in release 6.0
45  
46 #--------------------
47  
48 sub detect($$;$)
49 { my ($self, $accept, $prio) = @_;
50 my $mt = $self->mimeTypes;
51 my @ext = map $_->extensions, grep defined, map $mt->type($_),
52 grep !/\*/, $mt->httpAccept($accept);
53 \@ext;
54 }
55  
56  
57 sub type($;$)
58 { my ($self, $ext, $types) = @_;
59  
60 my $mt = $self->mimeTypes;
61 defined $types
62 or return $mt->mimeTypeOf($ext);
63  
64 # stupid interface compatibility!
65 $self;
66 }
67  
68  
69 sub file_type($) {
70 my ($self, $fn) = @_;
71 my $mt = $self->mimeTypes or return undef;
72 $mt->mimeTypeOf($fn);
73 }
74  
75  
76 sub content_type($;$) {
77 my ($self, $c, $opts) = @_;
78 my $headers = $c->res->headers;
79 return undef if $headers->content_type;
80  
81 my $fn = $opts->{file} || $opts->{ext};
82  
83 my $mt = $self->mimeTypes or return undef;
84 $headers->content_type($mt->mimeTypeOf($fn) || $mt->mimeTypeOf('txt'));
85 }
86  
87 #--------------------
88  
89 1;