/usr/share/perl5/MIME/Types.pm is in libmime-types-perl 2.09-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | # Copyrights 1999,2001-2014 by [Mark Overmeer].
# For other contributors see ChangeLog.
# See the manual pages for details on the licensing terms.
# Pod stripped from pm file by OODoc 2.01.
package MIME::Types;
use vars '$VERSION';
$VERSION = '2.09';
use strict;
use MIME::Type ();
use File::Spec ();
use File::Basename qw(dirname);
use List::Util qw(first);
my %typedb;
sub new(@) { (bless {}, shift)->init( {@_} ) }
sub init($)
{ my ($self, $args) = @_;
keys %typedb or $self->_read_db($args);
$self;
}
sub _read_db($)
{ my ($self, $args) = @_;
my $skip_extensions = $args->{skip_extensions};
my $only_complete = $args->{only_complete};
my $only_iana = $args->{only_iana};
my $db = $args->{db_file}
|| File::Spec->catfile(dirname(__FILE__), 'types.db');
local *DB;
open DB, '<:encoding(utf8)', $db
or die "cannot open type database in $db: $!\n";
while(1)
{ my $header = <DB>;
defined $header or last;
chomp $header;
# This logic is entangled with the bin/collect_types script
my ($count, $major, $is_iana, $has_ext) = split /\:/, $header;
my $skip_section = $major eq 'EXTENSIONS' ? $skip_extensions
: (($only_iana && !$is_iana) || ($only_complete && !$has_ext));
#warn "Skipping section $header\n" if $skip_section;
(my $section = $major) =~ s/^x-//;
if($major eq 'EXTENSIONS')
{ local $_;
while(<DB>)
{ last if m/^$/;
next if $skip_section;
chomp;
$typedb{$section}{$1} = $2 if m/(.*);(.*)/;
}
}
else
{ local $_;
while(<DB>)
{ last if m/^$/;
next if $skip_section;
chomp;
$typedb{$section}{$1} = "$major/$_" if m/^(?:x-)?([^;]+)/;
}
}
}
close DB;
}
# Catalyst-Plugin-Static-Simple uses it :(
sub create_type_index {}
#-------------------------------------------
sub type($)
{ my $spec = lc $_[1];
$spec = 'text/plain' if $spec eq 'text'; # old mailers
$spec =~ m!^(?:x\-)?([^/]+)/(?:x-)?(.*)!
or return;
my $section = $typedb{$1} or return;
my $record = $section->{$2} or return;
return $record if ref $record; # already extended
my $simple = $2;
my ($type, $ext, $enc) = split m/\;/, $record;
my $os = undef; # XXX TODO
$section->{$simple} = MIME::Type->new
( type => $type
, extensions => [split /\,/, $ext]
, encoding => $enc
, system => $os
);
}
sub mimeTypeOf($)
{ my ($self, $name) = @_;
(my $ext = lc $name) =~ s/.*\.//;
my $type = $typedb{EXTENSIONS}{$ext} or return;
$self->type($type);
}
sub addType(@)
{ my $self = shift;
foreach my $type (@_)
{ my ($major, $minor) = split m!/!, $type->simplified;
$typedb{$major}{$minor} = $type;
$typedb{EXTENSIONS}{$_} = $type for $type->extensions;
}
$self;
}
sub types()
{ my $self = shift;
my @types;
foreach my $section (keys %typedb)
{ next if $section eq 'EXTENSIONS';
push @types, map $_->type("$section/$_"),
sort keys %{$typedb{$section}};
}
@types;
}
sub listTypes()
{ my $self = shift;
my @types;
foreach my $section (keys %typedb)
{ next if $section eq 'EXTENSIONS';
foreach my $sub (sort keys %{$typedb{$section}})
{ my $record = $typedb{$section}{$sub};
push @types, ref $record ? $record->type
: $record =~ m/^([^;]+)/ ? $1 : die;
}
}
@types;
}
sub extensions { keys %{$typedb{EXTENSIONS}} }
sub _MojoExtTable() {$typedb{EXTENSIONS}}
#-------------
sub httpAccept($)
{ my $self = shift;
my @listed;
foreach (split /\,\s*/, shift)
{
m!^ ([a-zA-Z0-9-]+ | \*) / ( [a-zA-Z0-9+-]+ | \* )
\s* (?: \;\s*q\=\s* ([0-9]+(?:\.[0-9]*)?) \s* )?
(\;.* | )
$ !x or next;
my $mime = "$1/$2$4";
my $q = $3 || ($1 eq '*' ? -2 : $2 eq '*' ? -1 : 1);
push @listed, [ $mime, $q-@listed*0.001 ];
}
map $_->[0], sort {$b->[1] <=> $a->[1]} @listed;
}
sub httpAcceptBest($@)
{ my $self = shift;
my @accept = ref $_[0] eq 'ARRAY' ? @{(shift)} : $self->httpAccept(shift);
my $match;
foreach my $acc (@accept)
{ $acc =~ s/\s*\;.*//; # remove attributes
my $m = $acc !~ s#/\*$## ? first { $_->equals($acc) } @_
: $acc eq '*' ? $_[0] # $acc eq */*
: first { $_->mediaType eq $acc } @_;
return $m if defined $m;
}
();
}
sub httpAcceptSelect($@)
{ my ($self, $accept) = (shift, shift);
my $fns = !@_ ? return () : ref $_[0] eq 'ARRAY' ? shift : [@_];
unless(defined $accept)
{ my $fn = $fns->[0];
return ($fn, $self->mimeTypeOf($fn));
}
# create mapping type -> filename
my (%have, @have);
foreach my $fn (@$fns)
{ my $type = $self->mimeTypeOf($fn) or next;
$have{$type->simplified} = $fn;
push @have, $type;
}
my $type = $self->httpAcceptBest($accept, @have);
defined $type ? ($have{$type}, $type) : ();
}
#-------------------------------------------
# OLD INTERFACE (version 0.06 and lower)
use base 'Exporter';
our @EXPORT_OK = qw(by_suffix by_mediatype import_mime_types);
my $mime_types;
sub by_suffix($)
{ my $filename = shift;
$mime_types ||= MIME::Types->new;
my $mime = $mime_types->mimeTypeOf($filename);
my @data = defined $mime ? ($mime->type, $mime->encoding) : ('','');
wantarray ? @data : \@data;
}
sub by_mediatype($)
{ my $type = shift;
$mime_types ||= MIME::Types->new;
my @found;
if(!ref $type && index($type, '/') >= 0)
{ my $mime = $mime_types->type($type);
@found = $mime if $mime;
}
else
{ my $search = ref $type eq 'Regexp' ? $type : qr/$type/i;
@found = map $mime_types->type($_),
grep $_ =~ $search,
$mime_types->listTypes;
}
my @data;
foreach my $mime (@found)
{ push @data, map [$_, $mime->type, $mime->encoding],
$mime->extensions;
}
wantarray ? @data : \@data;
}
sub import_mime_types($)
{ my $filename = shift;
use Carp;
croak <<'CROAK';
import_mime_types is not supported anymore: if you have types to add
please send them to the author.
CROAK
}
1;
__END__
# Exceptions
vms:text/plain;doc;8bit
mac:application/x-macbase64;;bin
# IE6 bug
image/pjpeg;;base64
|