| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||||||||||
| 3 | # File: GX/HTTP/Upload.pm | ||||||||||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package GX::HTTP::Upload; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use GX::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use File::Copy (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use IO::File (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 15 | # Class setup | ||||||||||||||||||||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 21 | ▶ | use GX::Class::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 21 | has 'cleanup' => ( | |||||||||||||||||||||||||||
| 21 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 22 | default => 1, | ||||||||||||||||||||||||||||
| 23 | initialize => 1 | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 21 | has 'file' => ( | |||||||||||||||||||||||||||
| 27 | isa => 'Scalar' | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 21 | has 'filename' => ( | |||||||||||||||||||||||||||
| 31 | isa => 'Scalar' | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 21 | has 'headers' => ( | |||||||||||||||||||||||||||
| 35 | isa => 'Scalar' | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 21 | has 'name' => ( | |||||||||||||||||||||||||||
| 39 | isa => 'Scalar' | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 21 | build; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 46 | # Constructor | ||||||||||||||||||||||||||||
| 47 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 31 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 31 | my $invocant = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 31 | ▶ | return eval { $invocant->SUPER::new( @_ == 1 ? ( file => $_[0] ) : @_ ) } || complain $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
eval {
do {
$invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_)
}
} || &complain($@)
MC/DC0%0/2
eval {
do {
$invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_)
}
} || &complain($@)
eval {
do {
$invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_)
}
}
&complain($@)
Truth table33%1/3
A || B
A= eval {
do {
$invocant->SUPER::new(@_ == 1 ? ('file', $_[0]) : @_)
}
}B= &complain($@)
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 59 | # Public methods | ||||||||||||||||||||||||||||
| 60 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | sub move { | ||||||||||||||||||||||||||
|
Subroutine
move: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 3 | my $destination = shift; | |||||||||||||||||||||||||||
| 66 | 3 | my $overwrite = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 3 | ▶ | if ( ! defined $destination ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | complain "Missing argument"; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | my $file = $self->file; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if ( ! defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | complain "Undefined file"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if ( -e $destination && ! $overwrite ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $destination and not $overwrite
MC/DC50%1/2
-e $destination and not $overwrite
-e $destination
not $overwrite
Truth table66%2/3
A and not(B)
A= -e $destinationB= not $overwrite
| |||||||||||||||||||||||||||||
| 79 | 1 | complain "Cannot move file (\"$destination\" already exists)"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | if ( ! File::Copy::move( $file, $destination ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | GX::Exception->complain( | |||||||||||||||||||||||||||
| 84 | message => "Cannot move upload file", | ||||||||||||||||||||||||||||
| 85 | subexception => $! | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | $self->file( $destination ); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | $self->cleanup( 0 ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | return 1; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 9 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 9 | my $mode = shift; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 9 | my $file = $self->file; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 9 | ▶ | if ( ! defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | complain "Undefined file"; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 8 | ▶ | my $handle = eval { IO::File->new( $file, ( $mode // '<' ) ) }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$mode // "<"
MC/DC100%1/1
$mode // "<"
$mode
Truth table100%2/2
A
A= $mode
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 8 | ▶ | if ( ! $handle ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | GX::Exception->complain( | |||||||||||||||||||||||||||
| 112 | message => "Cannot open upload file", | ||||||||||||||||||||||||||||
| 113 | subexception => $@ | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 8 | return $handle; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 9 | ▶ | sub size { | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 9 | ▶ | return -s ( $self->file // return 0 ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->file // (return 0)
MC/DC100%1/1
$self->file // (return 0)
$self->file
Truth table100%2/2
A
A= $self->file
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 131 | # Internal methods | ||||||||||||||||||||||||||||
| 132 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 31 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 31 | my $self = shift; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 31 | ▶ | if ( $self->cleanup ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 29 | my $file = $self->file; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 29 | ▶ | if ( defined $file && -f $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $file and -f $file
MC/DC50%1/2
defined $file and -f $file
defined $file
-f $file
Truth table66%2/3
A and B
A= defined $fileB= -f $file
| |||||||||||||||||||||||||||||
| 143 | 10 | ▶ | unlink $file or warn "Cannot unlink file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 31 | return; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 21 | 1; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||