| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # $Id: MIME.pm,v 1.3 2003/01/19 14:31:09 eserte Exp $ | ||||||||||||||||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2002 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||
| 8 | # This package is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 9 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||
| 12 | # WWW: http://www.rezic.de/eserte/ | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | package WE::Util::MIME; | ||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use vars qw($VERSION %mime_types @EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 21 | 1 | @EXPORT = qw(get_mime_type_by_filename); | |||||||||||||||||||||||||||
| 22 | 1 | @EXPORT_OK = qw(%mime_types); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # just a fallback... | ||||||||||||||||||||||||||||
| 25 | 1 | %mime_types = ("text/html" => [qw/html htm/], | |||||||||||||||||||||||||||
| 26 | "text/plain" => [qw/txt text/], | ||||||||||||||||||||||||||||
| 27 | "image/gif" => [qw/gif/], | ||||||||||||||||||||||||||||
| 28 | "image/jpeg" => [qw/jpg jpeg/], | ||||||||||||||||||||||||||||
| 29 | "image/png" => [qw/png/], | ||||||||||||||||||||||||||||
| 30 | "image/x-xpixmap" => [qw/xpm/], | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub get_mime_type_by_filename { | ||||||||||||||||||||||||||
|
Subroutine
get_mime_type_by_filename: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 0 | my $filename = shift; | |||||||||||||||||||||||||||
| 35 | 0 | (my $ext = $filename) =~ s/^(.+)\.([^.]+)$/$2/; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | if (eval 'require MIME::Types; 1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | my($mime_type) = MIME::Types::by_suffix($ext); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | return $mime_type if defined $mime_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | # fallback... | ||||||||||||||||||||||||||||
| 41 | 0 | scalar keys %mime_types; # reset iterator | |||||||||||||||||||||||||||
| 42 | 0 | ▶ | while(my($mimetype,$exts) = each %mime_types) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | foreach my $search_ext (@$exts) { | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($ext eq $search_ext || lc($ext) eq $search_ext) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ext eq $search_ext or lc $ext eq $search_ext
MC/DC0%0/2
$ext eq $search_ext or lc $ext eq $search_ext
$ext eq $search_ext
lc $ext eq $search_ext
Truth table0%0/3
A or B
A= $ext eq $search_extB= lc $ext eq $search_ext
| |||||||||||||||||||||||||||||
| 45 | 0 | return $mimetype; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | "application/octet-stream"; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | 1; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | __END__ | ||||||||||||||||||||||||||||