| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | # | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | package Rex::Repositorio::Server::Yum::File; | ||
| 10 | |||
| 11 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 12 | use File::Spec; | ||
| 13 | use File::Path 'make_path'; | ||
| 14 | use File::Basename qw'dirname'; | ||
| 15 | use Mojo::UserAgent; | ||
| 16 | |||
| 17 | our $VERSION = '1.2.1'; # VERSION | ||
| 18 | |||
| 19 | sub serve { | ||
| 20 | my ($self) = @_; | ||
| 21 | |||
| 22 | my $file = $self->req->url; | ||
| 23 | $self->app->log->debug("Serving: $file"); | ||
| 24 | |||
| 25 | # my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 26 | my $repo_dir = File::Spec->rel2abs( $self->config->{RepositoryRoot} ); | ||
| 27 | |||
| 28 | $self->app->log->debug("Path: $repo_dir"); | ||
| 29 | |||
| 30 | my $serve_dir = File::Spec->catdir( $repo_dir, $file ); | ||
| 31 | $self->app->log->debug("Serving URL: $serve_dir"); | ||
| 32 | |||
| 33 | my $orig_url = $file; | ||
| 34 | my $local_part = $self->repo->{local}; | ||
| 35 | my $repo_url = $self->repo->{url}; | ||
| 36 | |||
| 37 | if ($repo_url) { | ||
| 38 | $repo_url =~ s/\/$//; | ||
| 39 | |||
| 40 | $orig_url =~ s/^\/([^\/]+)\/\Q$local_part\E//; | ||
| 41 | |||
| 42 | if ( $repo_url !~ m/\/$/ ) { | ||
| 43 | $repo_url .= "/"; | ||
| 44 | } | ||
| 45 | |||
| 46 | $orig_url = $repo_url . $orig_url; | ||
| 47 | |||
| 48 | $self->app->log->debug("Orig-URL: $orig_url"); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $do_proxy = lc( $self->repo->{proxy} || "false" ); | ||
| 52 | |||
| 53 | if ( -d $serve_dir ) { | ||
| 54 | my @entries; | ||
| 55 | opendir( my $dh, $serve_dir ) or die($!); | ||
| 56 | while ( my $entry = readdir($dh) ) { | ||
| 57 | next if ( $entry =~ m/^\./ ); | ||
| 58 | push @entries, | ||
| 59 | { | ||
| 60 | name => $entry, | ||
| 61 | file => ( -f File::Spec->catfile( $serve_dir, $entry ) ), | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | closedir($dh); | ||
| 65 | |||
| 66 | @entries = | ||
| 67 | sort { "$a->{file}-$a->{name}" cmp "$b->{file}-$b->{name}" } @entries; | ||
| 68 | |||
| 69 | $self->stash( path => $file ); | ||
| 70 | $self->stash( entries => \@entries ); | ||
| 71 | |||
| 72 | $self->render("file/serve"); | ||
| 73 | } | ||
| 74 | elsif ( -f $serve_dir ) { | ||
| 75 | $self->app->log->debug("File-Download: $serve_dir"); | ||
| 76 | |||
| 77 | if ( -f "$serve_dir.etag" && ( $do_proxy eq "true" || $do_proxy eq "on" ) ) | ||
| 78 | { | ||
| 79 | |||
| 80 | # there is an etag file, so the file was downloaded via proxy | ||
| 81 | # check upstream if file is out-of-date | ||
| 82 | my ($etag) = eval { local ( @ARGV, $/ ) = ("$serve_dir.etag"); <>; }; | ||
| 83 | $self->app->log->debug( | ||
| 84 | "Making ETag request to upstream ($orig_url): ETag: $etag."); | ||
| 85 | my $tx = $self->ua->head( $orig_url, | ||
| 86 | { Accept => '*/*', 'If-None-Match' => $etag } ); | ||
| 87 | if ( $tx->success ) { | ||
| 88 | my $upstream = $tx->res->headers->header('ETag'); | ||
| 89 | $upstream =~ s/"//g; | ||
| 90 | if ( $upstream ne $etag ) { | ||
| 91 | $self->app->log->debug( | ||
| 92 | "Upstream ETag and local ETag does not match: $upstream != $etag"); | ||
| 93 | return $self->_proxy_url( $orig_url, $serve_dir ); | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | $self->app->log->debug( | ||
| 97 | "Upstream ETag and local ETag are the same: $upstream == $etag"); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | return $self->render_file( filepath => $serve_dir ); | ||
| 103 | } | ||
| 104 | elsif ( $do_proxy eq "true" || $do_proxy eq "on" ) { | ||
| 105 | $self->app->log->debug("Need to get file from upstream: $orig_url"); | ||
| 106 | return $self->_proxy_url( $orig_url, $serve_dir ); | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | $self->render( text => "Not found", status => 404 ); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub index { | ||
| 114 | my ($self) = @_; | ||
| 115 | |||
| 116 | my $repo_dir = File::Spec->rel2abs( $self->config->{RepositoryRoot} ); | ||
| 117 | |||
| 118 | # get tags | ||
| 119 | opendir( my $dh, $repo_dir ) or die($!); | ||
| 120 | my @tags; | ||
| 121 | while ( my $entry = readdir($dh) ) { | ||
| 122 | next if ( $entry =~ m/^\./ ); | ||
| 123 | if ( -d File::Spec->catdir( $repo_dir, $entry, $self->repo->{name} ) ) { | ||
| 124 | push @tags, $entry; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | closedir($dh); | ||
| 128 | |||
| 129 | $self->stash( "path", "/" ); | ||
| 130 | $self->stash( "tags", \@tags ); | ||
| 131 | $self->stash( repo_name => $self->repo->{name} ); | ||
| 132 | |||
| 133 | $self->render("file/index"); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub _proxy_url { | ||
| 137 | my ( $self, $orig_url, $serve_dir ) = @_; | ||
| 138 | |||
| 139 | return $self->proxy_to( | ||
| 140 | $orig_url, | ||
| 141 | sub { | ||
| 142 | my ( $c, $tx ) = @_; | ||
| 143 | $c->app->log->debug("Got data from upstream ($orig_url)..."); | ||
| 144 | make_path( dirname($serve_dir) ); | ||
| 145 | open my $fh, '>', $serve_dir or die($!); | ||
| 146 | binmode $fh; | ||
| 147 | print $fh $tx->res->body; | ||
| 148 | close $fh; | ||
| 149 | |||
| 150 | my $etag = $tx->res->headers->header('ETag') || ''; | ||
| 151 | if($etag) { | ||
| 152 | $etag =~ s/"//g; | ||
| 153 | open my $fh_e, '>', "$serve_dir.etag" or die($!); | ||
| 154 | print $fh_e $etag; | ||
| 155 | close $fh_e; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | ); | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; |