| 1 | package App::Followme::UploadFtp; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.008005; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | ▶ | use lib '../..'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | use base qw(App::Followme::ConfiguredObject); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use Net::FTP; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use File::Spec::Functions qw(abs2rel splitdir catfile); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | our $VERSION = "2.03"; | |||||||||||||
| 14 | |||||||||||||||
| 15 | #---------------------------------------------------------------------- | ||||||||||||||
| 16 | # Read the default parameter values | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 0 | ▶ | sub parameters { | ||||||||||||
|
Subroutine
parameters: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 19 | 0 | my ($pkg) = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | return ( | ||||||||||||||
| 22 | 0 | ftp_url => '', | |||||||||||||
| 23 | remote_directory => '', | ||||||||||||||
| 24 | ftp_debug => 0, | ||||||||||||||
| 25 | remote_pkg => 'File::Spec::Unix', | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | #---------------------------------------------------------------------- | ||||||||||||||
| 30 | # Add a directory to the remote site | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 0 | ▶ | sub add_directory { | ||||||||||||
|
Subroutine
add_directory: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 33 | 0 | my ($self, $dir) = @_; | |||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | my $status; | |||||||||||||
| 36 | 0 | $dir = $self->remote_name($dir); | |||||||||||||
| 37 | |||||||||||||||
| 38 | 0 | ▶ | if ($self->{ftp}->ls($dir)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 39 | 0 | $status = 1; | |||||||||||||
| 40 | } elsif ($self->{ftp}->mkdir($dir)) { | ||||||||||||||
| 41 | 0 | $status = 1; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 0 | return $status; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | #---------------------------------------------------------------------- | ||||||||||||||
| 48 | # Add a file to the remote site | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | sub add_file { | ||||||||||||
|
Subroutine
add_file: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 51 | 0 | my ($self, $local_filename, $remote_filename) = @_; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 0 | my $status; | |||||||||||||
| 54 | 0 | $remote_filename = $self->remote_name($remote_filename); | |||||||||||||
| 55 | |||||||||||||||
| 56 | # Delete file if already there | ||||||||||||||
| 57 | 0 | ▶ | if ($self->{ftp}->mdtm($remote_filename)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 0 | $self->{ftp}->delete($remote_filename); | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | # Change upload mode if necessary | ||||||||||||||
| 62 | 0 | ▶ | if (-B $local_filename) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 63 | 0 | ▶ | if ($self->{ascii}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 0 | $self->{ftp}->binary(); | |||||||||||||
| 65 | 0 | $self->{ascii} = 0; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | } elsif (! $self->{ascii}) { | ||||||||||||||
| 69 | 0 | $self->{ftp}->ascii(); | |||||||||||||
| 70 | 0 | $self->{ascii} = 1; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # Upload the file | ||||||||||||||
| 74 | 0 | ▶ | if ($self->{ftp}->put($local_filename, $remote_filename)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 0 | $status = 1; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 0 | return $status; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | #---------------------------------------------------------------------- | ||||||||||||||
| 82 | # Close the ftp connection | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | ▶ | sub close { | ||||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 85 | 0 | my ($self) = @_; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 0 | $self->{ftp}->quit(); | |||||||||||||
| 88 | 0 | undef $self->{ftp}; | |||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | return; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | #---------------------------------------------------------------------- | ||||||||||||||
| 94 | # Delete a directory on the remote site, including contents | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | ▶ | sub delete_directory { | ||||||||||||
|
Subroutine
delete_directory: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 97 | 0 | my ($self, $dir) = @_; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | my $status; | |||||||||||||
| 100 | 0 | $dir = $self->remote_name($dir); | |||||||||||||
| 101 | |||||||||||||||
| 102 | 0 | ▶ | if ($self->{ftp}->ls($dir)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | ▶ | if ($self->{ftp}->rmdir($dir)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | $status = 1; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | } else { | ||||||||||||||
| 108 | 0 | $status = 1; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | return $status; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | #---------------------------------------------------------------------- | ||||||||||||||
| 115 | # Delete a file on the remote site | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | ▶ | sub delete_file { | ||||||||||||
|
Subroutine
delete_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 118 | 0 | my ($self, $filename) = @_; | |||||||||||||
| 119 | |||||||||||||||
| 120 | 0 | my $status; | |||||||||||||
| 121 | 0 | $filename = $self->remote_name($filename); | |||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | ▶ | if ($self->{ftp}->mdtm($filename)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 0 | ▶ | if ($self->{ftp}->delete($filename)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 125 | 0 | $status = 1; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | } else { | ||||||||||||||
| 129 | 0 | $status = 1; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 0 | return 1; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | #---------------------------------------------------------------------- | ||||||||||||||
| 136 | # Open the connection to the remote site | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 0 | ▶ | sub open { | ||||||||||||
|
Subroutine
open: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 139 | 0 | my ($self, $user, $password) = @_; | |||||||||||||
| 140 | |||||||||||||||
| 141 | # Open the ftp connection | ||||||||||||||
| 142 | |||||||||||||||
| 143 | my $ftp = Net::FTP->new($self->{ftp_url}, Debug => $self->{ftp_debug}) | ||||||||||||||
| 144 | 0 | ▶ | or die "Cannot connect to $self->{ftp_url}: $@"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | ▶ | $ftp->login($user, $password) or die "Cannot login ", $ftp->message; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | |||||||||||||||
| 148 | $ftp->cwd($self->{remote_directory}) | ||||||||||||||
| 149 | 0 | ▶ | or die "Cannot change remote directory ", $ftp->message; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | $ftp->binary(); | |||||||||||||
| 152 | |||||||||||||||
| 153 | 0 | $self->{ftp} = $ftp; | |||||||||||||
| 154 | 0 | $self->{ascii} = 0; | |||||||||||||
| 155 | |||||||||||||||
| 156 | 0 | return; | |||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | #---------------------------------------------------------------------- | ||||||||||||||
| 160 | # Get the name of the file on the remote system | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | ▶ | sub remote_name { | ||||||||||||
|
Subroutine
remote_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 163 | 0 | my ($self, $remote_filename) = @_; | |||||||||||||
| 164 | |||||||||||||||
| 165 | 0 | my @path = splitdir($remote_filename); | |||||||||||||
| 166 | 0 | $remote_filename = $self->{remote}->catfile(@path); | |||||||||||||
| 167 | 0 | return $remote_filename; | |||||||||||||
| 168 | } | ||||||||||||||
| 169 | |||||||||||||||
| 170 | 1 | 1; | |||||||||||||
| 171 | __END__ | ||||||||||||||