App-MBUtiny
view release on metacpan or search on metacpan
lib/App/MBUtiny/Storage/FTP.pm view on Meta::CPAN
my $self = shift;
my %params = @_;
if ($self->storage_status(STORAGE_SIGN) <= 0) { # SKIP and set SKIP
$self->maybe::next::method(%params);
return $self->storage_status(STORAGE_SIGN, -1);
}
my $name = $params{name}; # archive name
my $file = $params{file}; # destination archive file path
foreach my $storage ($self->ftp_storages) {
my $uri = new URI($storage->{url});
my $url_wop = $storage->{url_wop};
my $path = $uri->path // ""; $path =~ s/^\///;
my $attr = dclone($storage->{attr});
$attr->{Port} = $uri->port if $uri->port;
# Create object
my $ftp = new Net::FTP($uri->host, %$attr) or do {
$self->error(sprintf("Can't connect to %s: %s", $url_wop, $@));
next;
};
# Login
$ftp->login($uri->user || "anonymous", $uri->password || "anonymous\@example.com") or do {
$self->error(sprintf("Can't login to %s: %s", $url_wop, $ftp->message));
$ftp->quit if $ftp; # Quit
next;
};
# Change dir
if (length($path)) {
$ftp->cwd($path) or do {
$self->error(sprintf("Can't change directory %s on %s: %s", $path, $url_wop, $ftp->message));
$ftp->quit if $ftp; # Quit
next;
};
}
# Get file size
my $src_size = $ftp->size($name) || 0;
# Get file
$ftp->binary;
$ftp->get($name, $file) or do {
$self->error(sprintf("Can't get file %s from %s: %s", $name, $url_wop, $ftp->message));
$ftp->quit if $ftp; # Quit
next;
};
# Quit
$ftp->quit if $ftp;
# Check size
my $dst_size = filesize($file) // 0;
unless ($src_size == $dst_size) {
$self->error(sprintf("An error occurred while fetching data from %s. Sizes are different: SRC=%d; DST=%d", $url_wop, $src_size, $dst_size));
next;
}
# Validate
unless ($self->validate($file)) { # FAIL validation!
$self->error(sprintf("FTP storage %s failed: file %s is not valid!", $url_wop, $file));
next
}
# Done!
return $self->storage_status(STORAGE_SIGN, 1);
}
$self->storage_status(STORAGE_SIGN, 0);
$self->maybe::next::method(%params);
}
sub del {
my $self = shift;
my $name = shift;
$self->maybe::next::method($name);
return $self->storage_status(STORAGE_SIGN, -1) if $self->storage_status(STORAGE_SIGN) <= 0; # SKIP and set SKIP
my $status = 1;
foreach my $storage ($self->ftp_storages) {
my $uri = new URI($storage->{url});
my $url_wop = $storage->{url_wop};
my $path = $uri->path // ""; $path =~ s/^\///;
my $attr = dclone($storage->{attr});
$attr->{Port} = $uri->port if $uri->port;
my $ostat = 1;
# Create object
my $ftp = new Net::FTP($uri->host, %$attr) or do {
$self->error(sprintf("Can't connect to %s: %s", $url_wop, $@));
$ostat = 0;
};
# Login
if ($ostat) {
$ftp->login($uri->user || "anonymous", $uri->password || "anonymous\@example.com") or do {
$self->error(sprintf("Can't login to %s: %s", $storage->{url_wop}, $ftp->message));
$ostat = 0;
};
}
# Change dir
if ($ostat && length($path)) {
$ftp->cwd($path) or do {
$self->error(sprintf("Can't change directory %s on %s: %s", $path, $url_wop, $ftp->message));
$ostat = 0;
};
}
# Get list
my @ls = ();
if ($ostat) {
@ls = $ftp->ls();
}
# Delete file
if ($ostat && grep { $_ eq $name } @ls ) {
$ftp->delete($name) or do {
$self->error(sprintf("Can't delete file %s from %s: %s", $name, $url_wop, $ftp->message));
$ostat = 0;
};
( run in 0.610 second using v1.01-cache-2.11-cpan-39bf76dae61 )