| 1 | package HTML::FormHandler::Field::Upload; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: file upload field | ||||||||||||||||||||||||||||||||
| 3 | 1 | $HTML::FormHandler::Field::Upload::VERSION = '0.410000'; | |||||||||||||||||||||||||||||||
| 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 | sub get_class_messages { | ||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
get_class_messages: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 23 | return { | ||||||||||||||||||||||||||||||||
| 24 | 0 | %{ $self->next::method }, | |||||||||||||||||||||||||||||||
| 25 | %$class_messages, | ||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | sub validate { | ||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 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 | sub is_real_fh { | ||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $fh = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
is_real_fh: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | =pod | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | HTML::FormHandler::Field::Upload - file upload field | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | version 0.410000 | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | This field is designed to be used with a blessed object with a 'size' method, | ||||||||||||||||||||||||||||||||
| 97 | such as L<Catalyst::Request::Upload>, or a filehandle. | ||||||||||||||||||||||||||||||||
| 98 | Validates that the file is not empty and is within the 'min_size' | ||||||||||||||||||||||||||||||||
| 99 | and 'max_size' limits (limits are in bytes). | ||||||||||||||||||||||||||||||||
| 100 | A form containing this field must have the enctype set. | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | package My::Form::Upload; | ||||||||||||||||||||||||||||||||
| 103 | use HTML::FormHandler::Moose; | ||||||||||||||||||||||||||||||||
| 104 | extends 'HTML::FormHandler'; | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | has '+enctype' => ( default => 'multipart/form-data'); | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | has_field 'file' => ( type => 'Upload', max_size => '2000000' ); | ||||||||||||||||||||||||||||||||
| 109 | has_field 'submit' => ( type => 'Submit', value => 'Upload' ); | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | In your controller: | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | my $form = My::Form::Upload->new; | ||||||||||||||||||||||||||||||||
| 114 | my @params = ( file => $c->req->upload('file') ) | ||||||||||||||||||||||||||||||||
| 115 | if $c->req->method eq 'POST'; | ||||||||||||||||||||||||||||||||
| 116 | $form->process( params => { @params } ); | ||||||||||||||||||||||||||||||||
| 117 | return unless ( $form->validated ); | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | You can set the min_size and max_size limits to undef if you don't want them to be validated. | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | =head1 DEPENDENCIES | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | =head2 widget | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | Widget type is 'upload' | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | FormHandler Contributors - see HTML::FormHandler | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | This software is copyright (c) 2017 by Gerda Shank. | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 136 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||||||