File Coverage

blib/lib/File/MimeInfo/Simple.pm
Criterion Covered Total %
statement 30 36 83.3
branch 11 16 68.7
condition 1 5 20.0
subroutine 7 7 100.0
pod 1 1 100.0
total 50 65 76.9


line stmt bran cond sub pod time code
1             package File::MimeInfo::Simple;
2              
3 6     6   711968 use strict;
  6         12  
  6         214  
4 6     6   29 use warnings;
  6         15  
  6         355  
5              
6 6     6   37 use Carp;
  6         9  
  6         442  
7 6     6   2964 use YAML::Syck;
  6         12688  
  6         2994  
8              
9             require Exporter;
10              
11             our $VERSION = '0.8';
12             our @ISA = qw(Exporter);
13             our @EXPORT = qw(mimetype);
14              
15             # Lazy-loaded MIME type lookup table
16             my $yaml;
17              
18             sub _load_mime_table {
19 38 100   38   176 return if $yaml;
20 5         61 local $/;
21 5         1888 my $data = ;
22 5         132 $yaml = Load($data);
23             }
24              
25             sub mimetype {
26 7     7 1 386466 my ($filename) = shift;
27            
28 7 50       48 croak "No filename passed to mimetype()" unless $filename;
29 7 50 33     408 croak "Unable to read file: $filename" if -d $filename or ! -r $filename;
30            
31 7         23 my $mimetype;
32             # if platform -> windows
33 7 50       74 if($^O =~ m!MSWin32!i) {
34 0         0 return _find_mimetype_by_table($filename);
35             } else {
36             # Use safe pipe open to avoid shell injection
37 7 50       42746 if (open(my $fh, '-|', 'file', '--mime', '-br', $filename)) {
38 0         0 $mimetype = <$fh>;
39 0         0 close($fh);
40             }
41 7 50       179 unless($mimetype) {
42 7         165 return _find_mimetype_by_table($filename);
43             }
44             }
45            
46 0         0 chomp $mimetype;
47            
48 0         0 $mimetype =~ s/[;,\s]+.*$//;
49 0   0     0 return $mimetype || undef;
50             }
51              
52             sub _find_mimetype_by_table {
53 38     38   569078 my($filename) = shift;
54 38         151 _load_mime_table();
55             # Extract extension: everything after the last dot
56 38         17118 my($ext) = $filename =~ /\.([^.]+)$/;
57 38 100       108 return undef unless $ext;
58 36 100       620 return $yaml->{lc $ext} if(exists $yaml->{lc $ext});
59 2         8 return undef;
60             }
61              
62             1;
63              
64             =head1 NAME
65              
66             File::MimeInfo::Simple - Simple implementation to determine file type
67              
68             =head1 USAGE
69              
70             use File::MimeInfo::Simple;
71             say mimetype("/Users/damog/vatos_rudos.jpg"); # prints out 'image/jpeg'
72             say mimetype("C:\perl\foo.pl") # prints out 'application/x-perl'
73              
74             =head1 DESCRIPTION
75              
76             C is a much simpler implementation and uses a much
77             simpler approach than C, using the 'file' command on a
78             UNIX-based operating system. Windows uses a key-value list for extensions. It's
79             inspired on Matt Aimonetti's mimetype-fu used on Ruby and the Rails world.
80              
81             =head1 FUNCTIONS
82              
83             =head2 mimetype( $filename )
84              
85             C is exported by default. It receives a parameter, the file
86             path. It returns a string containing the mime type for the file, or
87             C if the type cannot be determined.
88              
89             =head1 AUTHOR
90              
91             David Moreno <damog@damog.net>.
92              
93             =head1 LICENSE
94              
95             Copyright 2009-2025 David Moreno.
96              
97             This program is free software; you can redistribute it and/or modify it under
98             the same terms as Perl itself.
99              
100             =cut
101              
102             __DATA__