| 1 | package PAGI::Request::Upload; | ||||||||||||||||||||||||||||||||
| 2 | 26 | $PAGI::Request::Upload::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||
| 3 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 26 | ▶ | use File::Basename qw(fileparse); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 26 | ▶ | use File::Copy qw(move); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 26 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 26 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | # Constructor | ||||||||||||||||||||||||||||||||
| 13 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 14 | 25 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 15 | my $self = bless { | ||||||||||||||||||||||||||||||||
| 16 | field_name => $args{field_name} // croak("field_name is required"), | ||||||||||||||||||||||||||||||||
| 17 | filename => $args{filename} // '', | ||||||||||||||||||||||||||||||||
| 18 | content_type => $args{content_type} // 'application/octet-stream', | ||||||||||||||||||||||||||||||||
| 19 | data => $args{data}, # in-memory content | ||||||||||||||||||||||||||||||||
| 20 | temp_path => $args{temp_path}, # on-disk path | ||||||||||||||||||||||||||||||||
| 21 | size => $args{size}, | ||||||||||||||||||||||||||||||||
| 22 | 25 | ▶ | _cleaned_up => 0, | ||||||||||||||||||||||||||||||
|
Condition50%3/6
$args{"field_name"} // croak("field_name is required")
$args{"filename"} // ""
$args{"content_type"} // "application/octet-stream"
MC/DC0%0/1
$args{"field_name"} // croak("field_name is required")
$args{"field_name"}
MC/DC0%0/1
$args{"filename"} // ""
$args{"filename"}
MC/DC0%0/1
$args{"content_type"} // "application/octet-stream"
$args{"content_type"}
Truth table50%1/2
A
A= $args{"field_name"}
Truth table50%1/2
A
A= $args{"filename"}
Truth table50%1/2
A
A= $args{"content_type"}
| |||||||||||||||||||||||||||||||||
| 23 | }, $class; | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | # Calculate size if not provided | ||||||||||||||||||||||||||||||||
| 26 | 25 | ▶ | if (!defined $self->{size}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 22 | ▶ | if (defined $self->{data}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 28 | 22 | $self->{size} = length($self->{data}); | |||||||||||||||||||||||||||||||
| 29 | ▶ | } elsif (defined $self->{temp_path} && -f $self->{temp_path}) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $self->{"temp_path"} and -f $self->{"temp_path"}
MC/DC0%0/2
defined $self->{"temp_path"} and -f $self->{"temp_path"}
defined $self->{"temp_path"}
-f $self->{"temp_path"}
Truth table0%0/3
A and B
A= defined $self->{"temp_path"}B= -f $self->{"temp_path"}
| |||||||||||||||||||||||||||||||||
| 30 | 0 | $self->{size} = -s $self->{temp_path}; | |||||||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||||||
| 32 | 0 | $self->{size} = 0; | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 25 | return $self; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | # Accessors | ||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub field_name { my ($self) = @_; $self->{field_name} } | ||||||||||||||||||||||||||||||
|
Subroutine
field_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 41 | 10 | ▶ | sub filename { my ($self) = @_; $self->{filename} } | ||||||||||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | sub content_type { my ($self) = @_; $self->{content_type} } | ||||||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | sub size { my ($self) = @_; $self->{size} } | ||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | sub temp_path { my ($self) = @_; $self->{temp_path} } | ||||||||||||||||||||||||||||||
|
Subroutine
temp_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | # Basename - strips Windows and Unix paths | ||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | sub basename { | ||||||||||||||||||||||||||||||
|
Subroutine
basename: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 48 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 49 | 3 | my $filename = $self->{filename}; | |||||||||||||||||||||||||||||||
| 50 | 3 | ▶ | return '' unless $filename; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | # Strip Windows paths (C:\Users\... or \\server\share\...) | ||||||||||||||||||||||||||||||||
| 53 | 3 | $filename =~ s/.*[\\\/]//; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 3 | return $filename; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | # Predicates | ||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub is_empty { | ||||||||||||||||||||||||||||||
|
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 60 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 61 | 2 | return $self->{size} == 0; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | sub is_in_memory { | ||||||||||||||||||||||||||||||
|
Subroutine
is_in_memory: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 65 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 66 | 10 | return defined($self->{data}); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 29 | ▶ | sub is_on_disk { | ||||||||||||||||||||||||||||||
|
Subroutine
is_on_disk: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 70 | 29 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 71 | 29 | return defined($self->{temp_path}); | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | # Content access - slurp | ||||||||||||||||||||||||||||||||
| 75 | 5 | ▶ | sub slurp { | ||||||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 76 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | if ($self->is_in_memory) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 78 | 3 | return $self->{data}; | |||||||||||||||||||||||||||||||
| 79 | } elsif ($self->is_on_disk) { | ||||||||||||||||||||||||||||||||
| 80 | open my $fh, '<:raw', $self->{temp_path} | ||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | or croak("Cannot read $self->{temp_path}: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 2 | my $content = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||
| 83 | 2 | close $fh; | |||||||||||||||||||||||||||||||
| 84 | 2 | return $content; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | 0 | return ''; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | # Content access - filehandle | ||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub fh { | ||||||||||||||||||||||||||||||
|
Subroutine
fh: called CC=5 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 91 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if ($self->is_in_memory) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 93 | open my $fh, '<', \$self->{data} | ||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | or croak("Cannot create filehandle from memory: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 1 | return $fh; | |||||||||||||||||||||||||||||||
| 96 | } elsif ($self->is_on_disk) { | ||||||||||||||||||||||||||||||||
| 97 | open my $fh, '<:raw', $self->{temp_path} | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | or croak("Cannot open $self->{temp_path}: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 0 | return $fh; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | 0 | croak("No content available"); | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | # Move upload to destination (BLOCKING - performs synchronous file I/O) | ||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub move_to { | ||||||||||||||||||||||||||||||
|
Subroutine
move_to: called CC=7 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 106 | 2 | my ($self, $destination) = @_; | |||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | # Ensure destination directory exists | ||||||||||||||||||||||||||||||||
| 109 | 2 | my ($name, $dir) = fileparse($destination); | |||||||||||||||||||||||||||||||
| 110 | 2 | ▶ | if ($dir && !-d $dir) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dir and not -d $dir
MC/DC0%0/2
$dir and not -d $dir
$dir
not -d $dir
Truth table33%1/3
A and not(B)
A= $dirB= not -d $dir
| |||||||||||||||||||||||||||||||||
| 111 | 0 | require File::Path; | |||||||||||||||||||||||||||||||
| 112 | 0 | File::Path::make_path($dir); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | if ($self->is_in_memory) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 116 | # Write data to destination (blocking I/O) | ||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | open my $fh, '>:raw', $destination | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | or croak("Cannot open $destination for writing: $!"); | ||||||||||||||||||||||||||||||||
| 119 | 1 | print $fh $self->{data}; | |||||||||||||||||||||||||||||||
| 120 | 1 | close $fh; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | # Mark as cleaned up so destructor doesn't touch the saved file | ||||||||||||||||||||||||||||||||
| 123 | 1 | delete $self->{data}; | |||||||||||||||||||||||||||||||
| 124 | 1 | $self->{_cleaned_up} = 1; | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 127 | } elsif ($self->is_on_disk) { | ||||||||||||||||||||||||||||||||
| 128 | # Use File::Copy::move (typically a rename, very fast) | ||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | move($self->{temp_path}, $destination) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | or croak("Cannot move to $destination: $!"); | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | # Mark as cleaned up so destructor doesn't touch the saved file | ||||||||||||||||||||||||||||||||
| 133 | 1 | delete $self->{temp_path}; | |||||||||||||||||||||||||||||||
| 134 | 1 | $self->{_cleaned_up} = 1; | |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 0 | croak("No content to move"); | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | # Discard the upload | ||||||||||||||||||||||||||||||||
| 143 | 25 | ▶ | sub discard { | ||||||||||||||||||||||||||||||
|
Subroutine
discard: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 144 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 145 | 25 | ▶ | return if $self->{_cleaned_up}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 23 | ▶ | if ($self->is_on_disk && -f $self->{temp_path}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->is_on_disk and -f $self->{"temp_path"}
MC/DC50%1/2
$self->is_on_disk and -f $self->{"temp_path"}
$self->is_on_disk
-f $self->{"temp_path"}
Truth table66%2/3
A and B
A= $self->is_on_diskB= -f $self->{"temp_path"}
| |||||||||||||||||||||||||||||||||
| 148 | 2 | unlink $self->{temp_path}; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | 23 | delete $self->{data}; | |||||||||||||||||||||||||||||||
| 152 | 23 | delete $self->{temp_path}; | |||||||||||||||||||||||||||||||
| 153 | 23 | $self->{_cleaned_up} = 1; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | # Destructor - cleanup temp files | ||||||||||||||||||||||||||||||||
| 157 | 25 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 158 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 159 | 25 | $self->discard; | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 26 | 1; | |||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||||||