X-Git-Url: http://www.dxcluster.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=cmd%2Fdownload.pl;fp=cmd%2Fdownload.pl;h=46cb627b22c6f6ab22ba18daf7912d4816572d40;hb=54775ee86e54343a0fba66a0bbca3567de96cf46;hp=0000000000000000000000000000000000000000;hpb=433209e1efed23493f25f1357d9c003696c1fb59;p=spider.git diff --git a/cmd/download.pl b/cmd/download.pl new file mode 100644 index 00000000..46cb627b --- /dev/null +++ b/cmd/download.pl @@ -0,0 +1,58 @@ +# +# download a file from t'internet +# +# A build in, non-spawning, wget -Qn +# +# Copyright 2023 Dirk Koopman G1TLH +# + +#use IO::Socket::SSL; +use File::Copy; + +my %h; + +sub handle +{ + my $self = shift; + return (1, $self->msg('e5')) if $self->priv < 9 || $self->remotecmd; + my $url = unpad(shift); + my $dest = unpad(shift) if @_; + dbg("download: url $url"); + my $ua = Mojo::UserAgent->new->insecure(1)->max_redirects(5); + my $res = $ua->get($url => sub {finish(@_, $self, $ua)}); + $self->{$res} = $res; + dbg("ua $ua start: $url") if isdbg('download'); +} + +sub finish { + my ($ua, $tx, $self, $ua) = @_; + +# $DB::single = 1; + + my $res = $tx->res; + + if ($res->is_success) { + #dbg("body: " . $res->body) if isdbg('download'); + my $tmp = localdata("tmp"); + mkdir $tmp, 0777 unless -e $tmp; + my $path = $tx->req->url->to_abs->path; + my @parts = split m|/|, $path; + my $fn = $parts[-1]; + dbg("ua $ua temp file: $tmp/$fn") if isdbg('download'); + $res->save_to("$tmp/$fn"); + my $target = localdata($fn); + if (-e "$tmp/$fn") { + LogDbg('dxcommand', "moving $tmp/$fn -> $target from "); + move "$tmp/$fn", $target; + unlink "$tmp/$fn"; + } + dbg("download: $target successfully downloaded") if isdbg('progress') + } elsif ($res->is_error) { + dbg("ua $ua err: " . $res->error) if isdbg('download'); + } elsif ($res->code == 301) { + dbg("redirect: " . $res->headers->location) + } else { + dbg("something else: " . $res->error->{message}); + } + delete $self->{$res}; +}