line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormHandler::Field::Upload; |
2
|
|
|
|
|
|
|
# ABSTRACT: file upload field |
3
|
|
|
|
|
|
|
$HTML::FormHandler::Field::Upload::VERSION = '0.40068'; |
4
|
1
|
|
|
1
|
|
837
|
use Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
5
|
1
|
|
|
1
|
|
7110
|
use Moose::Util::TypeConstraints; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
extends 'HTML::FormHandler::Field'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has '+widget' => ( default => 'Upload', ); |
11
|
|
|
|
|
|
|
has min_size => ( is => 'rw', isa => 'Maybe[Int]', default => 1 ); |
12
|
|
|
|
|
|
|
has max_size => ( is => 'rw', isa => 'Maybe[Int]', default => 1048576 ); |
13
|
|
|
|
|
|
|
has '+type_attr' => ( default => 'file' ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
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
|
|
|
0
|
0
|
|
my $self = shift; |
23
|
|
|
|
|
|
|
return { |
24
|
0
|
|
|
|
|
|
%{ $self->next::method }, |
|
0
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
%$class_messages, |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub validate { |
30
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $upload = $self->value; |
33
|
0
|
|
|
|
|
|
my $size = 0; |
34
|
0
|
0
|
0
|
|
|
|
if( blessed $upload && $upload->can('size') ) { |
|
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
return $self->add_error($self->get_message('upload_file_empty')) |
44
|
|
|
|
|
|
|
unless $size > 0; |
45
|
|
|
|
|
|
|
|
46
|
0
|
0
|
0
|
|
|
|
if( defined $self->min_size && $size < $self->min_size ) { |
47
|
0
|
|
|
|
|
|
$self->add_error( $self->get_message('upload_file_too_small'), $self->min_size ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
0
|
0
|
0
|
|
|
|
if( defined $self->max_size && $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
|
|
|
0
|
0
|
|
my $fh = shift; |
59
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
my $reftype = Scalar::Util::reftype($fh) or return; |
61
|
0
|
0
|
0
|
|
|
|
if( $reftype eq 'IO' |
|
|
|
0
|
|
|
|
|
62
|
0
|
|
|
|
|
|
or $reftype eq 'GLOB' && *{$fh}{IO} ){ |
63
|
0
|
|
|
|
|
|
my $m_fileno = $fh->fileno; |
64
|
0
|
0
|
|
|
|
|
return unless defined $m_fileno; |
65
|
0
|
0
|
|
|
|
|
return unless $m_fileno >= 0; |
66
|
0
|
|
|
|
|
|
my $f_fileno = fileno($fh); |
67
|
0
|
0
|
|
|
|
|
return unless defined $f_fileno; |
68
|
0
|
0
|
|
|
|
|
return unless $f_fileno >= 0; |
69
|
0
|
|
|
|
|
|
return 1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
0
|
|
|
|
|
|
return; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
77
|
1
|
|
|
1
|
|
2538
|
use namespace::autoclean; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
12
|
|
78
|
|
|
|
|
|
|
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.40068 |
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 |