| 1 | package HTML::FormHandler::Field::Upload; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: file upload field | ||||||||||||||||||||||||||||||||
| 3 | 1 | $HTML::FormHandler::Field::Upload::VERSION = '0.410002'; | |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | extends 'HTML::FormHandler::Field'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 1 | has '+widget' => ( default => 'Upload', ); | |||||||||||||||||||||||||||||||
| 11 | 1 | has min_size => ( is => 'rw', isa => 'Maybe[Int]', default => 1 ); | |||||||||||||||||||||||||||||||
| 12 | 1 | has max_size => ( is => 'rw', isa => 'Maybe[Int]', default => 1048576 ); | |||||||||||||||||||||||||||||||
| 13 | 1 | has '+type_attr' => ( default => 'file' ); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 1 | our $class_messages = { | |||||||||||||||||||||||||||||||
| 16 | 'upload_file_not_found' => 'File not found for upload field', | ||||||||||||||||||||||||||||||||
| 17 | 'upload_file_empty' => 'File uploaded is empty', | ||||||||||||||||||||||||||||||||
| 18 | 'upload_file_too_small' => 'File is too small (< [_1] bytes)', | ||||||||||||||||||||||||||||||||
| 19 | 'upload_file_too_big' => 'File is too big (> [_1] bytes)', | ||||||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub get_class_messages { | ||||||||||||||||||||||||||||||
|
Subroutine
get_class_messages: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 22 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 23 | return { | ||||||||||||||||||||||||||||||||
| 24 | 0 | %{ $self->next::method }, | |||||||||||||||||||||||||||||||
| 25 | %$class_messages, | ||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub validate { | ||||||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 30 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 0 | my $upload = $self->value; | |||||||||||||||||||||||||||||||
| 33 | 0 | my $size = 0; | |||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if( blessed $upload && $upload->can('size') ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
builtin::blessed($upload) and $upload->can("size")
MC/DC0%0/2
builtin::blessed($upload) and $upload->can("size")
builtin::blessed($upload)
$upload->can("size")
Truth table0%0/3
A and B
A= builtin::blessed($upload)B= $upload->can("size")
| |||||||||||||||||||||||||||||||||
| 35 | 0 | $size = $upload->size; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | elsif( is_real_fh( $upload ) ) { | ||||||||||||||||||||||||||||||||
| 38 | 0 | $size = -s $upload; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | else { | ||||||||||||||||||||||||||||||||
| 41 | 0 | return $self->add_error($self->get_message('upload_file_not_found')); | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | return $self->add_error($self->get_message('upload_file_empty')) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | unless $size > 0; | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if( defined $self->min_size && $size < $self->min_size ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->min_size and $size < $self->min_size
MC/DC0%0/2
defined $self->min_size and $size < $self->min_size
defined $self->min_size
$size < $self->min_size
Truth table0%0/3
A and B
A= defined $self->min_sizeB= $size < $self->min_size
| |||||||||||||||||||||||||||||||||
| 47 | 0 | $self->add_error( $self->get_message('upload_file_too_small'), $self->min_size ); | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if( defined $self->max_size && $size > $self->max_size ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->max_size and $size > $self->max_size
MC/DC0%0/2
defined $self->max_size and $size > $self->max_size
defined $self->max_size
$size > $self->max_size
Truth table0%0/3
A and B
A= defined $self->max_sizeB= $size > $self->max_size
| |||||||||||||||||||||||||||||||||
| 51 | 0 | $self->add_error( $self->get_message('upload_file_too_big'), $self->max_size ); | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | 0 | return; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | # stolen from Plack::Util::is_real_fh | ||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub is_real_fh { | ||||||||||||||||||||||||||||||
|
Subroutine
is_real_fh: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 58 | 0 | my $fh = shift; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my $reftype = Scalar::Util::reftype($fh) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if( $reftype eq 'IO' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
$reftype eq "GLOB" and *{$fh;}{"IO"}
MC/DC0%0/2
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
$reftype eq "IO"
$reftype eq "GLOB"
Truth table0%0/3
A or B
A= $reftype eq "IO"B= $reftype eq "GLOB"
| |||||||||||||||||||||||||||||||||
| 62 | 0 | or $reftype eq 'GLOB' && *{$fh}{IO} ){ | |||||||||||||||||||||||||||||||
| 63 | 0 | my $m_fileno = $fh->fileno; | |||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return unless defined $m_fileno; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return unless $m_fileno >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 0 | my $f_fileno = fileno($fh); | |||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return unless defined $f_fileno; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return unless $f_fileno >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||||||
| 72 | 0 | return; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 78 | 1 | 1; | |||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||||||