| 1 | package Uniform::Upload::File; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use File::Copy qw(copy move); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Uniform::Exceptions; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 4 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 13 | 6 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | my $self = { | ||||||||||||||||||||||||||||||||
| 16 | name => $args{name}, | ||||||||||||||||||||||||||||||||
| 17 | filename => $args{filename}, | ||||||||||||||||||||||||||||||||
| 18 | tmp_path => $args{tmp_path}, | ||||||||||||||||||||||||||||||||
| 19 | size => $args{size} || 0, | ||||||||||||||||||||||||||||||||
| 20 | type => $args{type} || 'application/octet-stream', | ||||||||||||||||||||||||||||||||
| 21 | max_size => $args{max_size}, | ||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | allowed_types => $args{allowed_types} || [], | ||||||||||||||||||||||||||||||
|
Condition83%5/6
$args{'size'} || 0
$args{'type'} || 'application/octet-stream'
$args{'allowed_types'} || []
MC/DC0%0/1
$args{'size'} || 0
$args{'size'}
MC/DC100%1/1
$args{'type'} || 'application/octet-stream'
$args{'type'}
MC/DC100%1/1
$args{'allowed_types'} || []
$args{'allowed_types'}
Truth table50%1/2
A
A= $args{'size'}
Truth table100%2/2
A
A= $args{'type'}
Truth table100%2/2
A
A= $args{'allowed_types'}
| |||||||||||||||||||||||||||||||||
| 23 | error => undef, | ||||||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 6 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 27 | 6 | $self->_validate; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 6 | return $self; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub name { $_[0]->{name} } | ||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub filename { $_[0]->{filename} } | ||||||||||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub tmp_path { $_[0]->{tmp_path} } | ||||||||||||||||||||||||||||||
|
Subroutine
tmp_path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub size { $_[0]->{size} } | ||||||||||||||||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub type { $_[0]->{type} } | ||||||||||||||||||||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | sub error { $_[0]->{error} } | ||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 38 | 4 | ▶ | sub is_valid { !defined $_[0]->{error} } | ||||||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | sub sanitized_filename { | ||||||||||||||||||||||||||||||
|
Subroutine
sanitized_filename: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 41 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | return '' unless defined $self->{filename}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 2 | my $clean = basename($self->{filename}); | |||||||||||||||||||||||||||||||
| 45 | 2 | $clean =~ s/[^\w\.\-]/_/g; | |||||||||||||||||||||||||||||||
| 46 | 2 | return $clean; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub _validate { | ||||||||||||||||||||||||||||||
|
Subroutine
_validate: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 50 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | if (defined $self->{max_size} && $self->{size} > $self->{max_size}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->{"max_size"} and $self->{"size"} > $self->{"max_size"}
MC/DC100%2/2
defined $self->{"max_size"} and $self->{"size"} > $self->{"max_size"}
defined $self->{"max_size"}
$self->{"size"} > $self->{"max_size"}
Truth table100%3/3
A and B
A= defined $self->{"max_size"}B= $self->{"size"} > $self->{"max_size"}
| |||||||||||||||||||||||||||||||||
| 53 | 1 | $self->{error} = sprintf("File size (%d bytes) exceeds maximum limit (%d bytes)", $self->{size}, $self->{max_size}); | |||||||||||||||||||||||||||||||
| 54 | 1 | return; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | if (@{ $self->{allowed_types} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 2 | my %allowed = map { $_ => 1 } @{ $self->{allowed_types} }; | |||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | unless ($allowed{ $self->{type} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | 1 | $self->{error} = sprintf("MIME type '%s' is not allowed", $self->{type}); | |||||||||||||||||||||||||||||||
| 61 | 1 | return; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub copy_to { | ||||||||||||||||||||||||||||||
|
Subroutine
copy_to: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | 1 | my ($self, $dest) = @_; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | croak "Cannot copy invalid file: " . $self->{error} | ||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | unless $self->is_valid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | copy($self->{tmp_path}, $dest) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | or Uniform::Exceptions->throw("Failed to copy upload file to $dest: $!"); | ||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub move_to { | ||||||||||||||||||||||||||||||
|
Subroutine
move_to: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 77 | 0 | my ($self, $dest) = @_; | |||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | croak "Cannot move invalid file: " . $self->{error} | ||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | unless $self->is_valid; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | move($self->{tmp_path}, $dest) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | or Uniform::Exceptions->throw("Failed to move upload file to $dest: $!"); | ||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 4 | 1; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||