diff --git a/lib/MirrorCache/WebAPI/Plugin/Dir.pm b/lib/MirrorCache/WebAPI/Plugin/Dir.pm index 226100ad..609f6469 100644 --- a/lib/MirrorCache/WebAPI/Plugin/Dir.pm +++ b/lib/MirrorCache/WebAPI/Plugin/Dir.pm @@ -313,7 +313,7 @@ sub _render_from_db { $dirname = $dm->root_subtree . ($folder_or_pattern? $path : $f->dirname) unless $dirname; $c->log->error($c->dumper('dirname:', $dirname, 'path:', $path, 'trail:', $trailing_slash)) if $MCDEBUG; if (my $folder = $rsFolder->find_folder_or_redirect($dirname)) { - $c->log->error('found redirect : ', $folder->{pathto}) if $MCDEBUG && $folder->{pathto}; + $c->log->error("found redirect : $dirname -> ", $folder->{pathto}) if $MCDEBUG && $folder->{pathto}; # return $dm->redirect($folder->{pathto} . $trailing_slash) if $folder->{pathto}; my $folder_path = $folder->{pathto} ? $folder->{pathto} : $folder->{path}; return $c->render(status => 404, text => "path {$path} not found!!") unless $folder_path; @@ -323,6 +323,7 @@ sub _render_from_db { } else { $realpath_subtree = $root->realpath($dm->root_subtree . ($folder_or_pattern? $path : $f->dirname)) // $dirname; } + $c->log->error('RENDER - REALPATH_SUBTREE : ', $realpath_subtree) if $MCDEBUG; if ($dirname eq $realpath_subtree) { if ($dirname eq $f->dirname || $folder_or_pattern) { $dm->folder_id($folder->{id}); @@ -331,17 +332,17 @@ sub _render_from_db { } } else { my $another_folder = $rsFolder->find({path => $realpath_subtree}); - $c->log->error($c->dumper('another_folder:', $another_folder->{id})) if $MCDEBUG; + $c->log->error($c->dumper('RENDER - another_folder:', $another_folder->id)) if $MCDEBUG; return undef unless $another_folder || $it_must_be_folder; # nothing found, proceed to _guess_what_to_render - $dm->folder_id($another_folder->{id}) if $another_folder; + $dm->real_folder_id($another_folder->id) if $another_folder; if ($folder->{id}) { - $dm->real_folder_id($folder->{id}); + $dm->folder_id($folder->{id}); $dm->folder_sync_last($folder->{sync_last}); $dm->folder_scan_last($folder->{scan_last}); } } if ($it_must_be_folder && !$file_pattern_in_folder) { - $dm->real_folder_id($folder->{id}) if $folder->{id}; + $dm->folder_id($folder->{id}) if $folder->{id}; return $c->mirrorcache->render_dir_mirrorlist($path, $dm) if $dm->mirrorlist; return _render_dir($dm, $path, $rsFolder); } diff --git a/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm b/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm index a12a4e58..2c45499a 100644 --- a/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm +++ b/lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm @@ -77,11 +77,12 @@ sub register { my $f = Mojo::File->new($filepath); my $dirname = $f->dirname; my $realfolder_id = $dm->real_folder_id; + $c->log->error($c->dumper('RENDER REAL ID: ', $realfolder_id)) if $MCDEBUG && $realfolder_id; my $realdirname; unless ($realfolder_id) { $realdirname = $root->realpath($f->dirname); + $realdirname = $dirname unless $realdirname; } - $realdirname = $dirname unless $realdirname; my $basename = $f->basename; $basename = $file->{name} if $file; @@ -108,11 +109,14 @@ sub register { } $schema->resultset('Folder')->set_wanted($folder_id) if $need_update; } - if (!$realfolder_id && $realdirname ne $dirname) { + if ($realfolder_id) { + my $realfolder = $schema->resultset('Folder')->find({id => $realfolder_id}); + $realdirname = $realfolder->path if $realfolder; + } elsif (($realdirname // $dirname) ne $dirname) { my $realfolder = $schema->resultset('Folder')->find({path => $realdirname}); $realfolder_id = $realfolder->id if $realfolder; - $c->log->error($c->dumper('RENDER FOLDER REAL', $realfolder_id ? $realfolder_id : 'NULL')) if $MCDEBUG; } + $c->log->error($c->dumper('RENDER FOLDER REAL', $realfolder_id ? $realfolder_id : 'NULL')) if $MCDEBUG; if ($folder || $realfolder_id) { my $fldid = ($realfolder_id? $realfolder_id : $folder_id); $folder_id = $fldid unless $folder_id; @@ -159,7 +163,7 @@ sub register { } if ($dm->torrent || $dm->zsync || $dm->magnet) { if ($baseurl) { - $fullurl = $baseurl . '/' . $filepath; + $fullurl = $baseurl . '/' . (($folder && $folder->path)? $folder->path : $realdirname) . '/' . $basename; } else { ($fullurl = $c->req->url->to_abs->to_string) =~ s/\.(torrent|zsync|magnet)$//; } @@ -216,18 +220,18 @@ sub register { $origin = $origin . ":" . $originurl->port if $originurl->port && $originurl->port != "80"; $origin = $origin . $dm->route; } - $origin = $origin . $filepath; + $origin = $origin . (($folder && $folder->path)? $folder->path : $realdirname) . '/' . $basename; my $xml; if ($dm->meta4) { $xml = _build_meta4( - $dm, (($folder && $folder->path)? $folder->path : $realdirname), $file, $country, $region, \@mirrors_country, \@mirrors_region, + $dm, $realdirname, $file, $country, $region, \@mirrors_country, \@mirrors_region, \@mirrors_rest, $origin, 'MirrorCache', $baseurl); $c->res->headers->content_disposition('attachment; filename="' .$basename. '.meta4"'); $c->render(data => $xml, format => 'meta4'); return 1; } $xml = _build_metalink( - $dm, (($folder && $folder->path)? $folder->path : $realdirname), $file, $country, $region, \@mirrors_country, \@mirrors_region, + $dm, $realdirname, $file, $country, $region, \@mirrors_country, \@mirrors_region, \@mirrors_rest, $origin, 'MirrorCache', $baseurl); $c->res->headers->content_disposition('attachment; filename="' .$basename. '.metalink"'); $c->render(data => $xml, format => 'metalink'); @@ -241,7 +245,7 @@ sub register { if ($country and !$dm->avoid_countries || !(grep { $country eq $_ } $dm->avoid_countries)) { for my $m (@mirrors_country) { push @mirrordata, - { + { url => $m->{url}, hostname => $m->{hostname}, location => uc($m->{country}), @@ -301,7 +305,7 @@ sub register { my $redirect = $root->redirect($dm, $filepath); if ($redirect) { $fileorigin = $redirect; - $fileoriginpath = ($folder? $folder->path : $realdirname) . '/' . $file->{name}; + $fileoriginpath = $realdirname . '/' . $file->{name}; } else { my $url = $c->req->url->to_abs; $fileorigin = $dm->scheme . '://' . $url->host; diff --git a/t/environ/02-files-hashes-import.sh b/t/environ/02-files-hashes-import.sh index a67798b8..f7cdbe9e 100755 --- a/t/environ/02-files-hashes-import.sh +++ b/t/environ/02-files-hashes-import.sh @@ -192,5 +192,6 @@ mc6/curl -I /link1/child1/child1.1.dat | grep -E "$($ap4/print_address)|$($ap3/p mc6/curl /link1/child1/child1.1.dat.meta4 \ | grep -C20 $($ap3/print_address)/link1/child1/child1.1.dat \ | grep -C20 $($ap3/print_address)/folder1/child1/child1.1.dat \ + | grep -C20 $($mc9/print_address)/folder1/child1/child1.1.dat \ echo success diff --git a/t/environ/15-local-symlink-subtree-rootredirect-vpn.sh b/t/environ/15-local-symlink-subtree-rootredirect-vpn.sh index 2fd2c3b8..2e5a44ad 100755 --- a/t/environ/15-local-symlink-subtree-rootredirect-vpn.sh +++ b/t/environ/15-local-symlink-subtree-rootredirect-vpn.sh @@ -59,7 +59,7 @@ $mc/backstage/shoot $mc/backstage/job mirror_scan_schedule $mc/backstage/shoot -$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 notexists.mirror1.cc/folder1/file1.1.dat | grep -C10 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat +$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 notexists.mirror1.cc/folder1/file1.1.dat | grep -C10 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat rc=0 $mcsub/curl /tool/v1/file1.1.dat.metalink | grep $($ap7/print_address) || rc=$? @@ -68,10 +68,10 @@ rc=0 $mcsub/curl /tool/v1/file1.1.dat.metalink | grep $($ap8/print_address) || rc=$? test $rc -gt 0 -$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C20 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat +$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C20 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat -$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 notexists.mirror1.cc/folder1/file1.1.dat | grep -C30 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat +$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 notexists.mirror1.cc/folder1/file1.1.dat | grep -C30 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat rc=0 $mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep $($ap7/print_address) || rc=$? @@ -80,7 +80,7 @@ rc=0 $mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep $($ap8/print_address) || rc=$? test $rc -gt 0 -$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat +$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat # test request from ROOT_COUNTRY is just served $mc/curl -i /download/folder1/file1.1.dat?COUNTRY=us | grep '200 OK' diff --git a/t/environ/15-local-symlink-subtree-rootredirect.sh b/t/environ/15-local-symlink-subtree-rootredirect.sh index 19cc4bf9..5b735428 100755 --- a/t/environ/15-local-symlink-subtree-rootredirect.sh +++ b/t/environ/15-local-symlink-subtree-rootredirect.sh @@ -56,11 +56,11 @@ $mc/backstage/shoot $mc/backstage/job mirror_scan_schedule $mc/backstage/shoot -$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 $($ap7/print_address)/folder1/file1.1.dat | grep -C10 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat -$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C10 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat +$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 $($ap7/print_address)/folder1/file1.1.dat | grep -C10 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat +$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C10 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat -$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 $($ap7/print_address)/folder1/file1.1.dat | grep -C30 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat -$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat +$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 $($ap7/print_address)/folder1/file1.1.dat | grep -C30 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat +$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat # test request from ROOT_COUNTRY is just served $mc/curl -i /download/folder1/file1.1.dat?COUNTRY=us | grep '200 OK' diff --git a/t/environ/23-folder-metalink.sh b/t/environ/23-folder-metalink.sh new file mode 100755 index 00000000..f6390ed1 --- /dev/null +++ b/t/environ/23-folder-metalink.sh @@ -0,0 +1,78 @@ +#!lib/test-in-container-environ.sh +set -exo pipefail + +mc=$(environ mc $(pwd)) + +# $mc/gen_env MIRRORCACHE_BRANDING=openSUSE + +$mc/start + +ap8=$(environ ap8) +ap7=$(environ ap7) +ap6=$(environ ap6) +ap5=$(environ ap5) +ap4=$(environ ap4) +ap3=$(environ ap3) + +for x in $mc $ap7 $ap8 $ap6 $ap5 $ap4 $ap3; do + mkdir -p $x/dt/{folder1,folder2,folder3} + mkdir -p $x/dt/project1/{folder1,folder2,folder3} + mkdir -p $x/dt/project2/{folder1,folder2,folder3} + echo $x/dt/{folder1,folder2,folder3}/{file1.1,file2.1}.dat | xargs -n 1 touch + echo $x/dt/project1/{folder1,folder2,folder3}/{file1.1,file2.1}.dat | xargs -n 1 touch + echo $x/dt/project2/{folder1,folder2,folder3}/{file1.1,file2.1}.dat | xargs -n 1 touch +done + +$ap3/start +$ap4/start +$ap5/start +$ap6/start +$ap7/start +$ap8/start + +# remove some files and folders +rm $ap7/dt/project1/folder2/file2.1.dat +rm $ap7/dt/project2/folder2/file2.1.dat +rm -r $ap5/dt/project2/folder2/ +rm -r $ap5/dt/project1/ +rm -r $ap4/dt/project2/ + +$mc/sql "insert into server(hostname,sponsor,sponsor_url,urldir,enabled,country,region) select '$($ap6/print_address)','sponsor1','www.sponsor.org','','t','us','na'" +$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap7/print_address)','','t','us','na'" +$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap8/print_address)','','t','de','eu'" +$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap5/print_address)','','t','cn','as'" +$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap4/print_address)','','t','jp','as'" +$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap3/print_address)','','f','jp','as'" + +$mc/sql "insert into project(name,path,etalon) select '2.0 1','/project2/folder1', 3" +$mc/sql "insert into project(name,path,etalon) select 'proj1','/project1', 3" +$mc/sql "insert into project(name,path,etalon) select '2.0 2','/project2/folder2', 3" + +$mc/backstage/job -e folder_sync -a '["/project1/folder1"]' +$mc/backstage/job -e mirror_scan -a '["/project1/folder1"]' +$mc/backstage/job -e folder_sync -a '["/project1/folder2"]' +$mc/backstage/job -e mirror_scan -a '["/project1/folder2"]' +$mc/backstage/job -e folder_sync -a '["/project2/folder1"]' +$mc/backstage/job -e mirror_scan -a '["/project2/folder1"]' +$mc/backstage/job -e folder_sync -a '["/project2/folder2"]' +$mc/backstage/job -e mirror_scan -a '["/project2/folder2"]' +$mc/backstage/shoot + +$mc/backstage/job -e report -a '["once"]' +$mc/backstage/shoot + +$mc/curl /report/mirrors | tidy --drop-empty-elements no | \ + grep -A5 -F '