| 1 | package Data::FormValidator::Constraints::Upload; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 6 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 7 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # This allows declaration use Data::FormValidator::Constraints::Upload ':all'; | ||||||||||||||||||||||||||||
| 10 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 11 | # will save memory. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 14 | valid_file_format | ||||||||||||||||||||||||||||
| 15 | valid_image_max_dimensions | ||||||||||||||||||||||||||||
| 16 | valid_file_max_bytes | ||||||||||||||||||||||||||||
| 17 | valid_image_min_dimensions | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 21 | file_format | ||||||||||||||||||||||||||||
| 22 | image_max_dimensions | ||||||||||||||||||||||||||||
| 23 | file_max_bytes | ||||||||||||||||||||||||||||
| 24 | image_min_dimensions | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our $VERSION = 4.88; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub file_format { | ||||||||||||||||||||||||||
|
Subroutine
file_format: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 0 | $self->set_current_constraint_name('file_format'); | |||||||||||||||||||||||||||
| 34 | 0 | valid_file_format($self,\%params); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 0 | } | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub image_max_dimensions { | ||||||||||||||||||||||||||
|
Subroutine
image_max_dimensions: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $w = shift || die 'image_max_dimensions: missing maximum width value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_max_dimensions: missing maximum width value')
MC/DC0%0/1
shift() || die('image_max_dimensions: missing maximum width value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $h = shift || die 'image_max_dimensions: missing maximum height value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_max_dimensions: missing maximum height value')
MC/DC0%0/1
shift() || die('image_max_dimensions: missing maximum height value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | 0 | $self->set_current_constraint_name('image_max_dimensions'); | |||||||||||||||||||||||||||
| 44 | 0 | valid_image_max_dimensions($self,\$w,\$h); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 0 | } | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub file_max_bytes { | ||||||||||||||||||||||||||
|
Subroutine
file_max_bytes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | my ($max_bytes) = @_; | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | 0 | $self->set_current_constraint_name('file_max_bytes'); | |||||||||||||||||||||||||||
| 53 | 0 | valid_file_max_bytes($self,\$max_bytes); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 0 | } | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub image_min_dimensions { | ||||||||||||||||||||||||||
|
Subroutine
image_min_dimensions: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $w = shift || die 'image_min_dimensions: missing minimum width value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_min_dimensions: missing minimum width value')
MC/DC0%0/1
shift() || die('image_min_dimensions: missing minimum width value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $h = shift || die 'image_min_dimensions: missing minimum height value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_min_dimensions: missing minimum height value')
MC/DC0%0/1
shift() || die('image_min_dimensions: missing minimum height value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 0 | $self->set_current_constraint_name('image_min_dimensions'); | |||||||||||||||||||||||||||
| 63 | 0 | valid_image_min_dimensions($self,\$w,\$h); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | } | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub valid_file_format { | ||||||||||||||||||||||||||
|
Subroutine
valid_file_format: not called CC=14 SCAR=52.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | $self->isa('Data::FormValidator::Results') || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | die "file_format: first argument is not a Data::FormValidator::Results object. "; | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $params = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 72 | # if (ref $params ne 'HASH' ) { | ||||||||||||||||||||||||||||
| 73 | # die "format: hash reference expected. Make sure you have | ||||||||||||||||||||||||||||
| 74 | # included 'params => []' in your constraint definition, even if there | ||||||||||||||||||||||||||||
| 75 | # are no additional arguments"; | ||||||||||||||||||||||||||||
| 76 | # } | ||||||||||||||||||||||||||||
| 77 | 0 | my $q = $self->get_filtered_data; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 80 | 0 | my $fh = _get_upload_fh($self); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ## load filehandle | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if (!$fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | warn "$0: can't get filehandle for field named $field" and return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ## load file magic stuff | ||||||||||||||||||||||||||||
| 88 | 0 | require File::MMagic; | |||||||||||||||||||||||||||
| 89 | 0 | my $mm = File::MMagic->new; | |||||||||||||||||||||||||||
| 90 | 0 | my $fm_mt; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ## only use filehandle bits for magic data | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | $fm_mt = $mm->checktype_filehandle($fh) || | ||||||||||||||||||||||||||
|
Condition0%0/5
$mm->checktype_filehandle($fh) || warn("$0: can't get filehandle for field named $field") && (return undef)
warn "$0: can't get filehandle for field named $field" and return undef
MC/DC0%0/2
$mm->checktype_filehandle($fh) || warn("$0: can't get filehandle for field named $field") && (return undef)
$mm->checktype_filehandle($fh)
warn "$0: can't get filehandle for field named $field"
Truth table0%0/3
A || B
A= $mm->checktype_filehandle($fh)B= warn "$0: can't get filehandle for field named $field"
| |||||||||||||||||||||||||||||
| 94 | (warn "$0: can't get filehandle for field named $field" and return undef); | ||||||||||||||||||||||||||||
| 95 | # Work around a bug in File::MMagic (RT#12074) | ||||||||||||||||||||||||||||
| 96 | 0 | seek($fh,0,0); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # File::MMagic returns 'application/octet-stream" as a punt | ||||||||||||||||||||||||||||
| 99 | # for "I don't know, here's a generic binary MIME type. | ||||||||||||||||||||||||||||
| 100 | # In some cases that is has indicated a bug in File::MMagic, | ||||||||||||||||||||||||||||
| 101 | # but it's a generally worthless response for identifying the file type. | ||||||||||||||||||||||||||||
| 102 | # so, we throw away the response in that case. The uploaded MIME type | ||||||||||||||||||||||||||||
| 103 | # will be used instead later, if present | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $fm_mt = undef if ($fm_mt eq 'application/octet-stream'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | ## fetch mime type universally (or close) | ||||||||||||||||||||||||||||
| 108 | 0 | my $uploaded_mt = _get_upload_mime_type($self); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # try the File::MMagic, then the uploaded field, then return undef we find neither | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $mt = ($fm_mt || $uploaded_mt) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$fm_mt || $uploaded_mt
MC/DC0%0/2
$fm_mt || $uploaded_mt
$fm_mt
$uploaded_mt
Truth table0%0/3
A || B
A= $fm_mtB= $uploaded_mt
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # figure out an extension | ||||||||||||||||||||||||||||
| 114 | 1 | ▶ | use MIME::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 0 | my $mimetypes = MIME::Types->new; | |||||||||||||||||||||||||||
| 116 | 0 | my MIME::Type $t = $mimetypes->type($mt); | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | my @mt_exts = $t ? $t->extensions : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ## setup filename to retrieve extension | ||||||||||||||||||||||||||||
| 120 | 0 | my $fn = $self->get_input_data->param($field); | |||||||||||||||||||||||||||
| 121 | 0 | my ($uploaded_ext) = ($fn =~ m/\.([\w\d]*)?$/); | |||||||||||||||||||||||||||
| 122 | 0 | my $ext; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if (scalar @mt_exts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | # If the upload extension is one recognized by MIME::Type, use it. | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (grep {/^$uploaded_ext$/} @mt_exts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | $ext = $uploaded_ext; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | # otherwise, use one from MIME::Type, just to be safe | ||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||
| 131 | 0 | $ext = $mt_exts[0]; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | # If is a provided extension but no MIME::Type extension, use that. | ||||||||||||||||||||||||||||
| 136 | # It's possible that there no extension uploaded or found) | ||||||||||||||||||||||||||||
| 137 | 0 | $ext = $uploaded_ext; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Add the mime_type and extension to the valid data set | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my $info = $self->meta($field) || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->meta($field) || {}
MC/DC0%0/1
$self->meta($field) || {}
$self->meta($field)
Truth table0%0/2
A
A= $self->meta($field)
| |||||||||||||||||||||||||||||
| 142 | 0 | $info = { %$info, mime_type => $mt, extension => ".$ext" }; | |||||||||||||||||||||||||||
| 143 | 0 | $self->meta($field,$info); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | return _is_allowed_type($mt, $params); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | ## Returns true if the passed-in mime-type matches our allowed types | ||||||||||||||||||||||||||||
| 149 | 4 | ▶ | sub _is_allowed_type { | ||||||||||||||||||||||||||
|
Subroutine
_is_allowed_type: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 150 | 4 | my $mt = shift; | |||||||||||||||||||||||||||
| 151 | 4 | my $params = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # XXX perhaps this should be in a global variable so it's easier | ||||||||||||||||||||||||||||
| 154 | # for other apps to change the defaults; | ||||||||||||||||||||||||||||
| 155 | 4 | ▶ | $params->{mime_types} ||= [qw!image/jpeg image/pjpeg image/gif image/png!]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params->{"mime_types"} ||= ["image/jpeg", "image/pjpeg", "image/gif", "image/png"]
MC/DC0%0/1
$params->{"mime_types"} ||= ["image/jpeg", "image/pjpeg", "image/gif", "image/png"]
$params->{"mime_types"}
Truth table50%1/2
A
A= $params->{"mime_types"}
| |||||||||||||||||||||||||||||
| 156 | 4 | my %allowed_types = map { $_ => 1 } @{ $params->{mime_types} }; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 4 | return $allowed_types{lc $mt}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub valid_image_max_dimensions { | ||||||||||||||||||||||||||
|
Subroutine
valid_image_max_dimensions: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | $self->isa('Data::FormValidator::Results') || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | die "image_max_dimensions: first argument is not a Data::FormValidator::Results object. "; | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my $max_width_ref = shift || die 'image_max_dimensions: missing maximum width value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_max_dimensions: missing maximum width value')
MC/DC0%0/1
shift() || die('image_max_dimensions: missing maximum width value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my $max_height_ref = shift || die 'image_max_dimensions: missing maximum height value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_max_dimensions: missing maximum height value')
MC/DC0%0/1
shift() || die('image_max_dimensions: missing maximum height value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 168 | 0 | my $max_width = $$max_width_ref; | |||||||||||||||||||||||||||
| 169 | 0 | my $max_height = $$max_height_ref; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | ($max_width > 0) || die 'image_max_dimensions: maximum width must be > 0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | ($max_height > 0) || die 'image_max_dimensions: maximum height must be > 0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | my $q = $self->get_filtered_data; | |||||||||||||||||||||||||||
| 174 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 175 | 0 | my ($width,$height) = _get_img_size($self); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | unless ($width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | warn "$0: imgsize test failed"; | |||||||||||||||||||||||||||
| 179 | 0 | return undef; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Add the dimensions to the valid hash | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | my $info = $self->meta($field) || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->meta($field) || {}
MC/DC0%0/1
$self->meta($field) || {}
$self->meta($field)
Truth table0%0/2
A
A= $self->meta($field)
| |||||||||||||||||||||||||||||
| 184 | 0 | $info = { %$info, width => $width, height => $height }; | |||||||||||||||||||||||||||
| 185 | 0 | $self->meta($field,$info); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | return (($width <= $$max_width_ref) and ($height <= $$max_height_ref)); | ||||||||||||||||||||||||||
|
Condition0%0/3
$width <= $$max_width_ref && $height <= $$max_height_ref
MC/DC0%0/2
$width <= $$max_width_ref && $height <= $$max_height_ref
$width <= $$max_width_ref
$height <= $$max_height_ref
Truth table0%0/3
A && B
A= $width <= $$max_width_refB= $height <= $$max_height_ref
| |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub valid_file_max_bytes { | ||||||||||||||||||||||||||
|
Subroutine
valid_file_max_bytes: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | $self->isa('Data::FormValidator::Results') || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | die "first argument is not a Data::FormValidator::Results object."; | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | my $max_bytes_ref = shift; | |||||||||||||||||||||||||||
| 197 | 0 | my $max_bytes; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ((ref $max_bytes_ref) and defined $$max_bytes_ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $max_bytes_ref and defined $$max_bytes_ref
MC/DC0%0/2
ref $max_bytes_ref and defined $$max_bytes_ref
ref $max_bytes_ref
defined $$max_bytes_ref
Truth table0%0/3
A and B
A= ref $max_bytes_refB= defined $$max_bytes_ref
| |||||||||||||||||||||||||||||
| 200 | 0 | $max_bytes = $$max_bytes_ref; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||
| 203 | 0 | $max_bytes = 1024*1024; # default to 1 Meg | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | my $q = $self->get_filtered_data; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | ## retrieve upload fh for field | ||||||||||||||||||||||||||||
| 211 | 0 | my $fh = _get_upload_fh($self); | |||||||||||||||||||||||||||
| 212 | 0 | ▶ | if (!$fh) { warn "Failed to load filehandle for $field" && return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
"Failed to load filehandle for $field" && (return undef)
MC/DC0%0/1
"Failed to load filehandle for $field" && (return undef)
"Failed to load filehandle for $field"
Truth table0%0/2
A
A= "Failed to load filehandle for $field"
| |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | ## retrieve size | ||||||||||||||||||||||||||||
| 215 | 0 | my $file_size = (stat ($fh))[7]; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Add the size to the valid hash | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | my $info = $self->meta($field) || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->meta($field) || {}
MC/DC0%0/1
$self->meta($field) || {}
$self->meta($field)
Truth table0%0/2
A
A= $self->meta($field)
| |||||||||||||||||||||||||||||
| 219 | 0 | $info = { %$info, bytes => $file_size }; | |||||||||||||||||||||||||||
| 220 | 0 | $self->meta($field,$info); | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | return ($file_size <= $max_bytes); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub valid_image_min_dimensions { | ||||||||||||||||||||||||||
|
Subroutine
valid_image_min_dimensions: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 226 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 227 | 0 | ▶ | $self->isa('Data::FormValidator::Results') || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | die "image_min_dimensions: first argument is not a Data::FormValidator::Results object. "; | ||||||||||||||||||||||||||||
| 229 | 0 | ▶ | my $min_width_ref = shift || | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_min_dimensions: missing minimum width value')
MC/DC0%0/1
shift() || die('image_min_dimensions: missing minimum width value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 230 | die 'image_min_dimensions: missing minimum width value'; | ||||||||||||||||||||||||||||
| 231 | 0 | ▶ | my $min_height_ref = shift || | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('image_min_dimensions: missing minimum height value')
MC/DC0%0/1
shift() || die('image_min_dimensions: missing minimum height value')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 232 | die 'image_min_dimensions: missing minimum height value'; | ||||||||||||||||||||||||||||
| 233 | 0 | my $min_width = $$min_width_ref; | |||||||||||||||||||||||||||
| 234 | 0 | my $min_height = $$min_height_ref; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | ## do these matter? | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | ($min_width > 0) || die 'image_min_dimensions: minimum width must be > 0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | ($min_height > 0) || die 'image_min_dimensions: minimum height must be > 0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | my $q = $self->get_filtered_data; | |||||||||||||||||||||||||||
| 241 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 242 | 0 | my ($width, $height) = _get_img_size($self); | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 0 | ▶ | unless ($width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | warn "image failed processing"; | |||||||||||||||||||||||||||
| 246 | 0 | return undef; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Add the dimensions to the valid hash | ||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $info = $self->meta($field) || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->meta($field) || {}
MC/DC0%0/1
$self->meta($field) || {}
$self->meta($field)
Truth table0%0/2
A
A= $self->meta($field)
| |||||||||||||||||||||||||||||
| 251 | 0 | $info = { %$info, width => $width, height => $height }; | |||||||||||||||||||||||||||
| 252 | 0 | $self->meta($field,$info); | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | ▶ | return (($width >= $min_width) and ($height >= $min_height)); | ||||||||||||||||||||||||||
|
Condition0%0/3
$width >= $min_width && $height >= $min_height
MC/DC0%0/2
$width >= $min_width && $height >= $min_height
$width >= $min_width
$height >= $min_height
Truth table0%0/3
A && B
A= $width >= $min_widthB= $height >= $min_height
| |||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | sub _get_img_size | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_img_size: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 259 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 260 | 0 | my $q = $self->get_filtered_data; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | ## setup caller to make can errors more useful | ||||||||||||||||||||||||||||
| 263 | 0 | my $caller = (caller(1))[3]; | |||||||||||||||||||||||||||
| 264 | 0 | my $pkg = __PACKAGE__ . "::"; | |||||||||||||||||||||||||||
| 265 | 0 | $caller =~ s/$pkg//g; | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | ## retrieve filehandle from query object. | ||||||||||||||||||||||||||||
| 270 | 0 | my $fh = _get_upload_fh($self); | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | ## check error | ||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if (not $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 0 | warn "Unable to load filehandle"; | |||||||||||||||||||||||||||
| 275 | 0 | return undef; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | require Image::Size; | |||||||||||||||||||||||||||
| 279 | 0 | import Image::Size; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | ## check size | ||||||||||||||||||||||||||||
| 282 | 0 | my ($width, $height, $err) = imgsize($fh); | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | unless ($width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | warn "$caller: imgsize test failed: $err"; | |||||||||||||||||||||||||||
| 286 | 0 | return undef; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 0 | return ($width, $height); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | ## fetch filehandle for use with various file type checking | ||||||||||||||||||||||||||||
| 293 | ## call it with (_get_upload_fh($self)) since kind of mock object | ||||||||||||||||||||||||||||
| 294 | sub _get_upload_fh | ||||||||||||||||||||||||||||
| 295 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_upload_fh: not called CC=3 SCAR=20.3
| |||||||||||||||||||||||||||||
| 296 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 297 | 0 | my $q = $self->get_filtered_data; | |||||||||||||||||||||||||||
| 298 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | # convert the FH for the filtered data into a -seekable- handle; | ||||||||||||||||||||||||||||
| 301 | # depending on whether we're using CGI::Simple, CGI, or Apache::Request | ||||||||||||||||||||||||||||
| 302 | # we might not have something -seekable-. | ||||||||||||||||||||||||||||
| 303 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # If we we already have an IO::File object, return it, otherwise create one. | ||||||||||||||||||||||||||||
| 306 | 0 | require Scalar::Util; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 0 | ▶ | if ( Scalar::Util::blessed($q->{$field}) && $q->{$field}->isa('IO::File') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($q->{$field}) and $q->{$field}->isa("IO::File")
MC/DC0%0/2
builtin::blessed($q->{$field}) and $q->{$field}->isa("IO::File")
builtin::blessed($q->{$field})
$q->{$field}->isa("IO::File")
Truth table0%0/3
A and B
A= builtin::blessed($q->{$field})B= $q->{$field}->isa("IO::File")
| |||||||||||||||||||||||||||||
| 309 | 0 | return $q->{$field}; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | else { | ||||||||||||||||||||||||||||
| 312 | 0 | return IO::File->new_from_fd(fileno($q->{$field}), 'r'); | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | ## returns mime type if included as part of the send | ||||||||||||||||||||||||||||
| 317 | ## | ||||||||||||||||||||||||||||
| 318 | ## NOTE: retrieves from original uploaded, -UNFILTERED- data | ||||||||||||||||||||||||||||
| 319 | sub _get_upload_mime_type | ||||||||||||||||||||||||||||
| 320 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_upload_mime_type: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 321 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 322 | 0 | my $q = $self->get_input_data; | |||||||||||||||||||||||||||
| 323 | 0 | my $field = $self->get_current_constraint_field; | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | ▶ | if ($q->isa('CGI')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 0 | my $fn = $q->param($field); | |||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | ## nicely check for info | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | if ($q->uploadInfo($fn)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | return $q->uploadInfo($fn)->{'Content-Type'} | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 0 | return undef; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | if ($q->isa('CGI::Simple')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 0 | my $fn = $q->param($field); | |||||||||||||||||||||||||||
| 338 | 0 | return $q->upload_info($fn, 'mime'); | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | if ($q->isa('Apache::Request')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 0 | my $upload = $q->upload($field); | |||||||||||||||||||||||||||
| 343 | 0 | return $upload->info('Content-type'); | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 0 | return undef; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 1 | 1; | |||||||||||||||||||||||||||
| 351 | __END__ | ||||||||||||||||||||||||||||