| 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
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | if (length $argument > 1024 || $argument =~ m/\n/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $argument > 1024 or $argument =~ /\n/
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/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | if (!-d $argument) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC0%0/1
'IO::File'->new($filename) || (return undef)
'IO::File'->new($filename)
Truth table50%1/2
A
A= 'IO::File'->new($filename)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 13 | ▶ | if ($data =~ m[^\x89PNG]) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | 3 | $substr = substr($data, 1, 1024); | |||||||||||||||||||||||||||
| 66 | 3 | ▶ | if (defined $substr && $substr =~ m[^PNG]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $substr and $substr =~ /^PNG/
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/
| |||||||||||||||||||||||||||||
| 67 | 0 | return q{image/png}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 5 | ▶ | if (length $data > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 3 | $substr = substr($data, 0, 2); | |||||||||||||||||||||||||||
| 72 | 3 | ▶ | if (pack('H*', 'ffd8') eq $substr ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | return q{image/jpeg}; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | return undef; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | 1; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||