File Coverage

blib/lib/WE/Util/MIME.pm
Criterion Covered Total %
statement 9 20 45.0
branch 0 6 0.0
condition 0 3 0.0
subroutine 3 4 75.0
pod 1 1 100.0
total 13 34 38.2


line stmt bran cond sub pod time code
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     1   3220 use base qw(Exporter);
  1         2  
  1         74  
17              
18 1     1   5 use strict;
  1         2  
  1         30  
19 1     1   5 use vars qw($VERSION %mime_types @EXPORT @EXPORT_OK);
  1         2  
  1         371  
20             $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
21             @EXPORT = qw(get_mime_type_by_filename);
22             @EXPORT_OK = qw(%mime_types);
23              
24             # just a fallback...
25             %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             sub get_mime_type_by_filename {
34 0     0 1   my $filename = shift;
35 0           (my $ext = $filename) =~ s/^(.+)\.([^.]+)$/$2/;
36 0 0         if (eval 'require MIME::Types; 1') {
37 0           my($mime_type) = MIME::Types::by_suffix($ext);
38 0 0         return $mime_type if defined $mime_type;
39             }
40             # fallback...
41 0           scalar keys %mime_types; # reset iterator
42 0           while(my($mimetype,$exts) = each %mime_types) {
43 0           foreach my $search_ext (@$exts) {
44 0 0 0       if ($ext eq $search_ext || lc($ext) eq $search_ext) {
45 0           return $mimetype;
46             }
47             }
48             }
49 0           "application/octet-stream";
50             }
51              
52             1;
53              
54             __END__