Debian Codegraph @

lib/HTML/FormHandler/Field/Upload.pm

raw
1 package HTML::FormHandler::Field::Upload;
2 # ABSTRACT: file upload field
3 $HTML::FormHandler::Field::Upload::VERSION = '0.410002';
4 use Moose;
5 use Moose::Util::TypeConstraints;
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 my $self = shift;
23 return {
24 %{ $self->next::method },
25 %$class_messages,
26 }
27 }
28
29 sub validate {
30 my $self = shift;
31
32 my $upload = $self->value;
33 my $size = 0;
34 if( blessed $upload && $upload->can('size') ) {
35 $size = $upload->size;
36 }
37 elsif( is_real_fh( $upload ) ) {
38 $size = -s $upload;
39 }
40 else {
41 return $self->add_error($self->get_message('upload_file_not_found'));
42 }
43 return $self->add_error($self->get_message('upload_file_empty'))
44 unless $size > 0;
45
46 if( defined $self->min_size && $size < $self->min_size ) {
47 $self->add_error( $self->get_message('upload_file_too_small'), $self->min_size );
48 }
49
50 if( defined $self->max_size && $size > $self->max_size ) {
51 $self->add_error( $self->get_message('upload_file_too_big'), $self->max_size );
52 }
53 return;
54 }
55
56 # stolen from Plack::Util::is_real_fh
57 sub is_real_fh {
58 my $fh = shift;
59
60 my $reftype = Scalar::Util::reftype($fh) or return;
61 if( $reftype eq 'IO'
62 or $reftype eq 'GLOB' && *{$fh}{IO} ){
63 my $m_fileno = $fh->fileno;
64 return unless defined $m_fileno;
65 return unless $m_fileno >= 0;
66 my $f_fileno = fileno($fh);
67 return unless defined $f_fileno;
68 return unless $f_fileno >= 0;
69 return 1;
70 }
71 else {
72 return;
73 }
74 }
75
76 __PACKAGE__->meta->make_immutable;
77 use namespace::autoclean;
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.410002
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