MuForm/Field/Upload.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 94 SCAR 48.8
CC
11
Cov
0%
validate
42.0
is_real_fh
42.0
build_input_type
2.0
CRAP 132.0
1 package Data::MuForm::Field::Upload;
2 # ABSTRACT: file upload field
3  
4 use Moo;
5 use Data::MuForm::Meta;
6 extends 'Data::MuForm::Field';
7  
8 use Scalar::Util ('blessed');
9  
10  
11 has min_size => ( is => 'rw', default => 1 );
12 has max_size => ( is => 'rw', default => 1048576 );
13 sub build_input_type { '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 return;
72 }
73  
74 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 Data::MuForm::Field::Upload - file upload field
85  
86 =head1 VERSION
87  
88 version 0.04
89  
90 =head1 DESCRIPTION
91  
92 This field is designed to be used with a blessed object with a 'size' method,
93 such as L<Catalyst::Request::Upload>, or a filehandle.
94 Validates that the file is not empty and is within the 'min_size'
95 and 'max_size' limits (limits are in bytes).
96 A form containing this field must have the enctype set.
97  
98 package My::Form::Upload;
99 use Moo;
100 use Data::MuForm::Meta;
101 extends 'Data::MuForm';
102  
103 has '+enctype' => ( default => 'multipart/form-data');
104  
105 has_field 'file' => ( type => 'Upload', max_size => '2000000' );
106 has_field 'submit' => ( type => 'Submit', value => 'Upload' );
107  
108 In your controller:
109  
110 my $form = My::Form::Upload->new;
111 my $params = $c->req->body_parameters;
112 $params->{file} = $c->req->upload('file') if $c->req->method eq 'POST';
113 $form->process( params => $params );
114 return unless ( $form->validated );
115  
116 You can set the min_size and max_size limits to undef if you don't want them to be validated.
117  
118 =head1 DEPENDENCIES
119  
120 =head2 layout_type
121  
122 Layout type is 'upload'
123  
124 =head1 AUTHOR
125  
126 Gerda Shank
127  
128 =head1 COPYRIGHT AND LICENSE
129  
130 This software is copyright (c) 2017 by Gerda Shank.
131  
132 This is free software; you can redistribute it and/or modify it under
133 the same terms as the Perl 5 programming language system itself.
134  
135 =cut