lib/File/Type/WebImages.pm

filter: Statementstmt 93.8% 46 / 49 Branchbran 83.3% 20 / 24 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 79.3% 77 / 97 CC 16 SCAR 19.0
CC
16
Cov
85%
checktype_contents
10.3
mime_type
7.0
checktype_filename
2.0
CRAP 6.7
↑ Summary
1 package File::Type::WebImages;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use vars '@EXPORT_OK';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 @EXPORT_OK = 'mime_type';
7  
8 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = "1.01";
11  
12 5 ▶sub mime_type {
Subroutine mime_type: called CC=6 SCAR=19.4
Pod undocumented
13 # magically route argument
14  
15 5 my $argument = shift;
16 5 ▶ return undef unless defined $argument;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argument 1 4 5
17  
18 4 ▶ if (length $argument > 1024 || $argument =~ m/\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $argument > 1024 or $argument =~ /\n/) 0 4 4
Condition33%1/3
length $argument > 1024 or $argument =~ /\n/
l!l&&r!l&&!r
004
MC/DC0%0/2
length $argument > 1024 or $argument =~ /\n/
length $argument > 1024 $argument =~ /\n/
Truth table33%1/3
A or B
A= length $argument > 1024B= $argument =~ /\n/
ABresult
1X1
011
000
19 # assume it's data. Saves a stat call if the data's long
20 # also avoids stat warning if there's a newline
21 0 return checktype_contents($argument);
22 }
23
24 4 ▶ if (-e $argument) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $argument) 2 2 4
25 2 ▶ if (!-d $argument) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $argument) { } 2 0 2
26 2 return checktype_filename($argument);
27 } else {
28 0 return undef; # directories don't have mime types
29 }
30 }
31 # otherwise, fall back to checking the string as if it's data again
32 2 return checktype_contents($argument);
33 }
34  
35 # reads in 16k of selected file, or returns undef if can't open,
36 # then checks contents
37 6 ▶sub checktype_filename {
Subroutine checktype_filename: called CC=2 SCAR=7.0
Pod undocumented
38 6 my $filename = shift;
39 6 ▶ my $fh = IO::File->new($filename) || return undef;
Condition50%1/2
'IO::File'->new($filename) || (return undef)
l!l
60
MC/DC0%0/1
'IO::File'->new($filename) || (return undef)
'IO::File'->new($filename)
Truth table50%1/2
A
A= 'IO::File'->new($filename)
Aresult
11
00
40 6 my $data;
41 6 $fh->read($data, 16*1024);
42 6 $fh->close;
43 6 return checktype_contents($data);
44 }
45  
46 # Matches $data against the magic database criteria and returns the MIME
47 # type of the file.
48 14 ▶sub checktype_contents {
Subroutine checktype_contents: called CC=10 SCAR=23.3
Pod undocumented
49 14 my $data = shift;
50 14 my $substr;
51  
52 14 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 13 14
53  
54 13 ▶ if ($data =~ m[^\x89PNG]) {
Branch: 3 branches
BranchTrueFalseTotal
if ($data =~ /^\x89PNG/) { } 3 10 13
elsif ($data =~ /^GIF8/) { } 3 7 10
elsif ($data =~ /^BM/) { } 2 5 7
55 3 return q{image/png};
56 }
57 elsif ($data =~ m[^GIF8]) {
58 3 return q{image/gif};
59 }
60 elsif ($data =~ m[^BM]) {
61 2 return q{image/bmp};
62 }
63  
64 5 ▶ if (length $data > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > 1) 3 2 5
65 3 $substr = substr($data, 1, 1024);
66 3 ▶ if (defined $substr && $substr =~ m[^PNG]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $substr and $substr =~ /^PNG/) 0 3 3
Condition33%1/3
defined $substr and $substr =~ /^PNG/
!ll&&!rl&&r
030
MC/DC0%0/2
defined $substr and $substr =~ /^PNG/
defined $substr $substr =~ /^PNG/
Truth table33%1/3
A and B
A= defined $substrB= $substr =~ /^PNG/
ABresult
0X0
100
111
67 0 return q{image/png};
68 }
69 }
70 5 ▶ if (length $data > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data > 0) 3 2 5
71 3 $substr = substr($data, 0, 2);
72 3 ▶ if (pack('H*', 'ffd8') eq $substr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("\377\330" eq $substr) 3 0 3
73 3 return q{image/jpeg};
74 }
75 }
76  
77 2 return undef;
78 }
79  
80 2 1;
81  
82 __END__
↑ Summary