line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::Type::WebImages; |
2
|
2
|
|
|
2
|
|
4512
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
61
|
|
3
|
2
|
|
|
2
|
|
15
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
65
|
|
4
|
2
|
|
|
2
|
|
10
|
use base 'Exporter'; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
236
|
|
5
|
2
|
|
|
2
|
|
10
|
use vars '@EXPORT_OK'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
134
|
|
6
|
|
|
|
|
|
|
@EXPORT_OK = 'mime_type'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1866
|
use IO::File; |
|
2
|
|
|
|
|
26081
|
|
|
2
|
|
|
|
|
1247
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "1.01"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub mime_type { |
13
|
|
|
|
|
|
|
# magically route argument |
14
|
|
|
|
|
|
|
|
15
|
5
|
|
|
5
|
0
|
1571
|
my $argument = shift; |
16
|
5
|
100
|
|
|
|
13
|
return undef unless defined $argument; |
17
|
|
|
|
|
|
|
|
18
|
4
|
100
|
100
|
|
|
27
|
if (length $argument > 1024 || $argument =~ m/\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
|
2
|
|
|
|
|
4
|
return checktype_contents($argument); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
2
|
50
|
|
|
|
16
|
if (-e $argument) { |
25
|
0
|
0
|
|
|
|
0
|
if (!-d $argument) { |
26
|
0
|
|
|
|
|
0
|
return checktype_filename($argument); |
27
|
|
|
|
|
|
|
} else { |
28
|
0
|
|
|
|
|
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
|
|
|
|
|
6
|
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
|
|
|
|
|
|
|
sub checktype_filename { |
38
|
4
|
|
|
4
|
0
|
1555
|
my $filename = shift; |
39
|
4
|
|
50
|
|
|
31
|
my $fh = IO::File->new($filename) || return undef; |
40
|
4
|
|
|
|
|
357
|
my $data; |
41
|
4
|
|
|
|
|
20
|
$fh->read($data, 16*1024); |
42
|
4
|
|
|
|
|
181
|
$fh->close; |
43
|
4
|
|
|
|
|
66
|
return checktype_contents($data); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Matches $data against the magic database criteria and returns the MIME |
47
|
|
|
|
|
|
|
# type of the file. |
48
|
|
|
|
|
|
|
sub checktype_contents { |
49
|
14
|
|
|
14
|
0
|
432
|
my $data = shift; |
50
|
14
|
|
|
|
|
16
|
my $substr; |
51
|
|
|
|
|
|
|
|
52
|
14
|
100
|
|
|
|
38
|
return undef unless defined $data; |
53
|
|
|
|
|
|
|
|
54
|
13
|
100
|
|
|
|
67
|
if ($data =~ m[^\x89PNG]) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
55
|
3
|
|
|
|
|
19
|
return q{image/png}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ($data =~ m[^GIF8]) { |
58
|
3
|
|
|
|
|
21
|
return q{image/gif}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
elsif ($data =~ m[^BM]) { |
61
|
2
|
|
|
|
|
20
|
return q{image/bmp}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
5
|
100
|
|
|
|
15
|
if (length $data > 1) { |
65
|
3
|
|
|
|
|
15
|
$substr = substr($data, 1, 1024); |
66
|
3
|
50
|
33
|
|
|
25
|
if (defined $substr && $substr =~ m[^PNG]) { |
67
|
0
|
|
|
|
|
0
|
return q{image/png}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
5
|
100
|
|
|
|
15
|
if (length $data > 0) { |
71
|
3
|
|
|
|
|
5
|
$substr = substr($data, 0, 2); |
72
|
3
|
50
|
|
|
|
10
|
if (pack('H*', 'ffd8') eq $substr ) { |
73
|
3
|
|
|
|
|
15
|
return q{image/jpeg}; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
|
|
12
|
return undef; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |