Skip to content

Commit

Permalink
fix(downloads): fix some downloads edge cases
Browse files Browse the repository at this point in the history
issue #828
  • Loading branch information
frankiejol committed Sep 7, 2018
1 parent 34df162 commit d1bdf38
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions lib/Ravada/VM/KVM.pm
Expand Up @@ -928,7 +928,8 @@ sub _iso_name($self, $iso, $req, $verbose=1) {
,"Downloading ISO file for $iso_name "
." from $iso->{url}. It may take several minutes"
) if $req;
_download_file_external($iso->{url}, $device, $verbose);
_fill_url($iso);
$self->_download_file_external($iso->{url}, $device, $verbose);
$self->_refresh_storage_pools();
die "Download failed, file $device missing.\n"
if ! -e $device;
Expand Down Expand Up @@ -958,6 +959,16 @@ sub _iso_name($self, $iso, $req, $verbose=1) {
return $device;
}

sub _fill_url($iso) {
return if $iso->{url} =~ m{.*/[^/]+\.[^/]+$};
if ($iso->{file_re}) {
$iso->{url} .= "/" if $iso->{url} !~ m{/$};
$iso->{url} .= $iso->{file_re};
return;
}
confess "Error: Missing field file_re for ".$iso->{name};
}

sub _check_md5 {
my ($file, $md5 ) =@_;
return if !$md5;
Expand Down Expand Up @@ -1006,7 +1017,17 @@ sub _check_signature($file, $type, $expected) {
die "Unknown signature type $type";
}

sub _download_file_external($url, $device, $verbose=1) {
sub _download_file_external($self, $url, $device, $verbose=1) {
$url .= "/" if $url !~ m{/$} && $url !~ m{.*/([^/]+\.[^/]+)$};
if ( $url =~ m{/$} ) {
my ($filename) = $device =~ m{.*/(.*)};
$url = "$url$filename";
}
if ($url =~ m{[^*]}) {
my @found = $self->_search_url_file($url);
confess "No match for $url" if !scalar @found;
$url = $found[-1];
}
confess "ERROR: wget missing" if !$WGET;
my @cmd = ($WGET,'-nv',$url,'-O',$device);
my ($in,$out,$err) = @_;
Expand Down Expand Up @@ -1063,7 +1084,7 @@ sub _search_iso {

sub _download($self, $url) {
$url =~ s{(http://.*)//(.*)}{$1/$2};
if ($url =~ m{\*}) {
if ($url =~ m{[^*]}) {
my @found = $self->_search_url_file($url);
confess "No match for $url" if !scalar @found;
$url = $found[-1];
Expand Down Expand Up @@ -1092,9 +1113,10 @@ sub _match_url($self,$url) {
my ($url1, $match,$url2) = $url =~ m{(.*/)([^/]*\*[^/]*)/?(.*)};
$url2 = '' if !$url2;

confess "No url1 from $url" if !defined $url1;
my $ua = Mojo::UserAgent->new;
my $res = $ua->get(($url1 or '/'))->res;
die "ERROR ".$res->code." ".$res->message." : $url1"
confess "ERROR ".$res->code." ".$res->message." : $url1"
unless $res->code == 200 || $res->code == 301;

my @found;
Expand Down Expand Up @@ -1162,15 +1184,20 @@ sub _fetch_filename {
confess "No filename in $row->{name} $row->{url}" if !$file;

$row->{url} = $new_url;
$row->{file_re} = $file;
$row->{file_re} = "^$file";
}
confess "No file_re" if !$row->{file_re};
$row->{file_re} .= '$' if $row->{file_re} !~ m{\$$};

my @found = $self->search_volume_re(qr($row->{file_re}));
my @found;
if ($row->{rename_file}) {
@found = $self->search_volume_re(qr("^".$row->{rename_file}));
} else {
@found = $self->search_volume_re(qr($row->{file_re}));
}
if (@found) {
$row->{device} = $found[0]->get_path;
$row->{filename} = $found[0]->get_path =~ m{.*/(.*)};
($row->{filename}) = $found[0]->get_path =~ m{.*/(.*)};
return;
} else {
@found = $self->_search_url_file($row->{url}, $row->{file_re}) if !@found;
Expand Down Expand Up @@ -1223,7 +1250,7 @@ sub _match_file($self, $url, $file_re) {
eval { $res = $self->_web_user_agent->get($url)->res(); };
last if !$@ && $res && defined $res->code;
next if $@ && $@ =~ /timeout/i;
die $@ if $@;
confess $@ if $@;
}
return unless defined $res->code && $res->code == 200 || $res->code == 301;
Expand Down

0 comments on commit d1bdf38

Please sign in to comment.