blib/lib/File/MimeInfo/Simple.pm

filter: Statementstmt 85.7% 36 / 42 Branchbran 68.7% 11 / 16 Conditioncond 20.0% 1 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 75.6% 56 / 74 CC 11 SCAR 21.9
CC
11
Cov
76%
mimetype
16.9
_find_mimetype_by_table
3.0
_load_mime_table
2.0
CRAP 8.9
↑ Summary
1 package File::MimeInfo::Simple;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 require Exporter;
10  
11 6 our $VERSION = '0.8';
12 6 our @ISA = qw(Exporter);
13 6 our @EXPORT = qw(mimetype);
14  
15 # Lazy-loaded MIME type lookup table
16 6 my $yaml;
17  
18 38 ▶sub _load_mime_table {
Subroutine _load_mime_table: called CC=2 SCAR=6.9
19 38 ▶ return if $yaml;
Branch: 1 branch
BranchTrueFalseTotal
if $yaml 33 5 38
20 5 local $/;
21 5 my $data = <DATA>;
22 5 $yaml = Load($data);
23 }
24  
25 7 ▶sub mimetype {
Subroutine mimetype: called CC=8 SCAR=28.2
Pod documented
26 7 my ($filename) = shift;
27
28 7 ▶ croak "No filename passed to mimetype()" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 7 7
29 7 ▶ croak "Unable to read file: $filename" if -d $filename or ! -r $filename;
Branch: 1 branch
BranchTrueFalseTotal
if -d $filename or not -r $filename 0 7 7
Condition33%1/3
-d $filename or not -r $filename
l!l&&r!l&&!r
007
MC/DC0%0/2
-d $filename or not -r $filename
-d $filename not -r $filename
Truth table33%1/3
A or not(B)
A= -d $filenameB= not -r $filename
ABresult
1X1
011
000
30
31 7 my $mimetype;
32 # if platform -> windows
33 7 ▶ if($^O =~ m!MSWin32!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/i) { } 0 7 7
34 0 return _find_mimetype_by_table($filename);
35 } else {
36 # Use safe pipe open to avoid shell injection
37 7 ▶ if (open(my $fh, '-|', 'file', '--mime', '-br', $filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "-|", "file", "--mime", "-br", $filename) 0 7 7
38 0 $mimetype = <$fh>;
39 0 close($fh);
40 }
41 7 ▶ unless($mimetype) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mimetype) 7 0 7
42 7 return _find_mimetype_by_table($filename);
43 }
44 }
45
46 0 chomp $mimetype;
47
48 0 $mimetype =~ s/[;,\s]+.*$//;
49 0 ▶ return $mimetype || undef;
Condition0%0/2
$mimetype || undef
l!l
00
MC/DC0%0/1
$mimetype || undef
$mimetype
Truth table0%0/2
A
A= $mimetype
Aresult
11
00
50 }
51  
52 38 ▶sub _find_mimetype_by_table {
Subroutine _find_mimetype_by_table: called CC=3 SCAR=11.0
53 38 my($filename) = shift;
54 38 _load_mime_table();
55 # Extract extension: everything after the last dot
56 38 my($ext) = $filename =~ /\.([^.]+)$/;
57 38 ▶ return undef unless $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 2 36 38
58 36 ▶ return $yaml->{lc $ext} if(exists $yaml->{lc $ext});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$yaml{lc $ext} 34 2 36
59 2 return undef;
60 }
61  
62 6 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<File::MimeInfo::Simple> is a much simpler implementation and uses a much
77 simpler approach than C<File::MimeInfo>, 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<mimetype> 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<undef> if the type cannot be determined.
88  
89 =head1 AUTHOR
90  
91 David Moreno &lt;damog@damog.net&gt;.
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__
↑ Summary