1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
package HTML::FormFu::Constraint::File::Size;
use Moose;
use MooseX::Attribute::Chained;
use MooseX::Aliases;
extends 'HTML::FormFu::Constraint';
use Carp qw( croak );
use Scalar::Util qw( blessed );
has minimum => (
is => 'rw',
alias => 'min',
traits => ['Chained'],
);
has maximum => (
is => 'rw',
alias => 'max',
traits => ['Chained'],
);
*min_kilobyte = \&minimum_kilobyte;
*max_kilobyte = \&maximum_kilobyte;
*min_megabyte = \&minimum_megabyte;
*max_megabyte = \&maximum_megabyte;
sub constrain_value {
my ( $self, $value ) = @_;
return 1 if !defined $value || $value eq '';
return if !blessed($value) || !$value->isa('HTML::FormFu::Upload');
my $min = $self->minimum;
my $max = $self->maximum;
my $size = $value->size || 0;
if ( defined $min ) {
return 0 if $size < $min;
}
if ( defined $max ) {
return 0 if $size > $max;
}
return 1;
}
sub _localize_args {
my ($self) = @_;
return $self->min, $self->max;
}
sub minimum_kilobyte {
my ( $self, $kb ) = @_;
croak "minimum_kilobyte() cannot be used as a getter"
if @_ != 2;
return $self->minimum( $kb * 1024 );
}
sub minimum_megabyte {
my ( $self, $kb ) = @_;
croak "minimum_megabyte() cannot be used as a getter"
if @_ != 2;
return $self->minimum( $kb * 1_048_576 );
}
sub maximum_kilobyte {
my ( $self, $kb ) = @_;
croak "maximum_kilobyte() cannot be used as a getter"
if @_ != 2;
return $self->maximum( $kb * 1024 );
}
sub maximum_megabyte {
my ( $self, $kb ) = @_;
croak "maximum_megabyte() cannot be used as a getter"
if @_ != 2;
return $self->maximum( $kb * 1_048_576 );
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=head1 NAME
HTML::FormFu::Constraint::File::Size - File Size Constraint
=head1 DESCRIPTION
Ensure that an uploaded file meets minimum or maximum size constraints.
=head1 METHODS
=head2 minimum
=head2 min
Optional.
The minimum file size in bytes.
L</min> is an alias for L</minimum>.
=head2 maximum
=head2 max
Optional.
The maximum file size in bytes.
L</max> is an alias for L</maximum>.
=head2 minimum_kilobyte
=head2 min_kilobyte
Shortcut for C<< $constraint->minimum( $value * 1024 ) >>.
L</min_kilobyte> is an alias for L</minimum_kilobyte>.
=head2 maximum_kilobyte
=head2 max_kilobyte
Shortcut for C<< $constraint->maximum( $value * 1024 ) >>.
L</max_kilobyte> is an alias for L</maximum_kilobyte>.
=head2 minimum_megabyte
=head2 min_megabyte
Shortcut for C<< $constraint->minimum( $value * 1_048_576 ) >>.
L</min_megabyte> is an alias for L</minimum_megabyte>.
=head2 maximum_megabyte
=head2 max_megabyte
Shortcut for C<< $constraint->maximum( $value * 1_048_576 ) >>.
L</max_megabyte> is an alias for L</maximum_megabyte>.
=head1 SEE ALSO
Is a sub-class of, and inherits methods from L<HTML::FormFu::Constraint>
L<HTML::FormFu>
=head1 AUTHOR
Carl Franks, C<cfranks@cpan.org>
=head1 LICENSE
This library is free software, you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|