| 1 | package Mojo::Darkpan::Util; | ||||||||||||||
| 2 | 2 | ▶ | use v5.20; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 2 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use Mojo::Darkpan::Config; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use Mojo::IOLoop -signatures; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use File::Temp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use IO::Zlib; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 2 | ▶ | use File::Basename; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use FindBin; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use Cwd; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | has controller => ( is => 'rw', required => 1 ); | |||||||||||||
| 15 | 2 | has userinfo => ( is => 'lazy' ); | |||||||||||||
| 16 | 2 | has upload => ( is => 'lazy' ); | |||||||||||||
| 17 | 2 | has author => ( is => 'lazy' ); | |||||||||||||
| 18 | 2 | has indexer => ( is => 'lazy' ); | |||||||||||||
| 19 | 2 | has config => ( is => 'lazy' ); | |||||||||||||
| 20 | 2 | has packageFile => ( is => 'lazy' ); | |||||||||||||
| 21 | |||||||||||||||
| 22 | 13 | ▶ | sub authorized { | ||||||||||||
|
Subroutine
authorized: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 23 | 13 | my $self = shift; | |||||||||||||
| 24 | 13 | ▶ | if ( $self->config->basic_auth ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | my ( $hash_ref, $auth_ok ) = $self->controller->basic_auth( | ||||||||||||||
| 27 | $self->config->basic_auth->{realm} => | ||||||||||||||
| 28 | 6 | $self->config->basic_auth->{config} ); | |||||||||||||
| 29 | |||||||||||||||
| 30 | 6 | ▶ | if ( !$auth_ok ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 4 | $self->controller->res->headers->www_authenticate('Basic'); | |||||||||||||
| 32 | 4 | $self->controller->render( | |||||||||||||
| 33 | text => 'Authentication required!', | ||||||||||||||
| 34 | status => 401 | ||||||||||||||
| 35 | ); | ||||||||||||||
| 36 | 4 | return undef; | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | else { | ||||||||||||||
| 39 | 2 | return $auth_ok; | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | else { | ||||||||||||||
| 44 | 7 | return 1; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 5 | ▶ | sub publish { | ||||||||||||
|
Subroutine
publish: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 49 | 5 | my $self = shift; | |||||||||||||
| 50 | 5 | my $module; | |||||||||||||
| 51 | 5 | my $author = $self->author; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 5 | ▶ | if ( $self->upload ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | |||||||||||||||
| 55 | # get a Mojo::Asset::File ref | ||||||||||||||
| 56 | 1 | my $file = $self->upload->asset->to_file; | |||||||||||||
| 57 | |||||||||||||||
| 58 | # request from CPAN::Uploader | ||||||||||||||
| 59 | 1 | my $tempdir = File::Temp::tempdir( CLEANUP => 1 ); | |||||||||||||
| 60 | |||||||||||||||
| 61 | 1 | $module = File::Spec->catfile( $tempdir, $self->upload->filename ); | |||||||||||||
| 62 | 1 | $file->move_to($module); | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | else { | ||||||||||||||
| 65 | 4 | $module = $self->controller->param('module'); # can be a git repo. | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 5 | ▶ | if ( !defined($module) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 1 | die("no module provided in request"); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 4 | my $injector = OrePAN2::Injector->new( | |||||||||||||
| 73 | directory => $self->config->directory, | ||||||||||||||
| 74 | author => $author, | ||||||||||||||
| 75 | ); | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 4 | my $filename = fileparse($module); | |||||||||||||
| 78 | 4 | $self->controller->app->log->trace("injecting module: $filename"); | |||||||||||||
| 79 | 4 | my $archive = $injector->inject($module); | |||||||||||||
| 80 | |||||||||||||||
| 81 | 4 | ▶ | if ( -f $self->packageFile ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 3 | $self->addToIndex( $archive, $filename ); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | else { | ||||||||||||||
| 85 | 1 | $self->createIndex(); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 3 | ▶ | sub addToIndex { | ||||||||||||
|
Subroutine
addToIndex: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 90 | 3 | my $self = shift; | |||||||||||||
| 91 | 3 | my $archive = shift; | |||||||||||||
| 92 | 3 | my $filename = shift; | |||||||||||||
| 93 | |||||||||||||||
| 94 | 3 | $self->controller->app->log->info("adding $filename to index"); | |||||||||||||
| 95 | 3 | $archive = $self->config->directory . "/$archive"; | |||||||||||||
| 96 | 3 | my $index = OrePAN2::Index->new(); | |||||||||||||
| 97 | 3 | $index->load( $self->packageFile ); | |||||||||||||
| 98 | 3 | $self->indexer->add_index( $index, $archive ); | |||||||||||||
| 99 | 3 | $self->indexer->write_index( $index, $self->config->no_compress ); | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 1 | ▶ | sub createIndex { | ||||||||||||
|
Subroutine
createIndex: called CC=1 SCAR=1.7
| |||||||||||||||
| 103 | 1 | my $self = shift; | |||||||||||||
| 104 | |||||||||||||||
| 105 | # use a non-blocking process to reindex since this could take a while | ||||||||||||||
| 106 | Mojo::IOLoop->subprocess->run_p( | ||||||||||||||
| 107 | 0 | ▶ | sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 108 | # reindex to create the modules file | ||||||||||||||
| 109 | 0 | $self->controller->app->log->info("starting to index modules..."); | |||||||||||||
| 110 | 0 | $self->indexer->make_index( | |||||||||||||
| 111 | no_compress => $self->config->no_compress, ); | ||||||||||||||
| 112 | 0 | return "indexing completed"; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | )->then( | ||||||||||||||
| 115 | 1 | ▶ | sub ($msg) { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 116 | 1 | $self->controller->app->log->info($msg); | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | )->catch( | ||||||||||||||
| 119 | 0 | ▶ | sub ($err) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 120 | 0 | $self->controller->app->log->error($err); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | 1 | ); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 3 | ▶ | sub list { | ||||||||||||
|
Subroutine
list: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||
| 126 | 3 | my $self = shift; | |||||||||||||
| 127 | 3 | my $data = {}; | |||||||||||||
| 128 | 3 | my $packageFile = $self->packageFile; | |||||||||||||
| 129 | 3 | ▶ | return $data unless -f $packageFile; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 2 | my $fh = IO::Zlib->new( $packageFile, "rb" ); | |||||||||||||
| 131 | 2 | ▶ | while (<$fh>) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 23 | ▶ | next if ( $_ !~ m/\.tar\.gz$/ ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 5 | my ( $name, $version, $file ) = split( '\s+', $_ ); | |||||||||||||
| 134 | 5 | ▶ | if ( eval($version) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | 5 | my $dir = File::Basename::dirname($file); | |||||||||||||
| 136 | 5 | my $archive = $file =~ s/$dir\///gr; | |||||||||||||
| 137 | 5 | $data->{$file}->{version} = $version; | |||||||||||||
| 138 | 5 | $data->{$file}->{archive} = $archive; | |||||||||||||
| 139 | 5 | $data->{$file}->{dir} = $dir; | |||||||||||||
| 140 | $data->{$file}->{other_versions} = | ||||||||||||||
| 141 | 5 | $self->_getFileList( $dir, $archive ); | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | else { | ||||||||||||||
| 144 | # say "$current, $name"; | ||||||||||||||
| 145 | 0 | push( @{ $data->{$file}->{provides} }, $name ); | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 2 | return $data; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 5 | ▶ | sub _getFileList { | ||||||||||||
|
Subroutine
_getFileList: called CC=6 SCAR=17.9
| |||||||||||||||
| 153 | 5 | my $self = shift; | |||||||||||||
| 154 | 5 | my $path = shift; | |||||||||||||
| 155 | 5 | my $current = shift; | |||||||||||||
| 156 | |||||||||||||||
| 157 | 5 | my $base = getcwd; | |||||||||||||
| 158 | 5 | my $pkgdir = | |||||||||||||
| 159 | File::Spec->catfile( $self->config->directory, 'authors', 'id', $path ); | ||||||||||||||
| 160 | |||||||||||||||
| 161 | 5 | ▶ | opendir my $dir, $pkgdir or die "Cannot open directory: $pkgdir"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 5 | my @files = readdir $dir; | |||||||||||||
| 163 | 5 | closedir $dir; | |||||||||||||
| 164 | |||||||||||||||
| 165 | 5 | my $prefix = reverse( ( split( '-', reverse($current), 2 ) )[1] ); | |||||||||||||
| 166 | |||||||||||||||
| 167 | 5 | my @data; | |||||||||||||
| 168 | 5 | for (@files) { | |||||||||||||
| 169 | 28 | ▶ | next if $_ =~ m/^\./; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 170 | 18 | ▶ | next if $_ !~ m/^\Q$prefix\E-v?\d/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 7 | ▶ | next if $_ eq $current; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | 2 | push( @data, $_ ); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | 5 | return \@data; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | ▶ | sub _build_userinfo { | ||||||||||||
|
Subroutine
_build_userinfo: not called CC=1 SCAR=6.9
| |||||||||||||||
| 179 | 0 | my $self = shift; | |||||||||||||
| 180 | my ( $username, $password ) = | ||||||||||||||
| 181 | 0 | split( ':', $self->req->url->base->{userinfo} ); | |||||||||||||
| 182 | return { | ||||||||||||||
| 183 | 0 | username => $username, | |||||||||||||
| 184 | password => $password | ||||||||||||||
| 185 | }; | ||||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | 5 | ▶ | sub _build_upload { | ||||||||||||
|
Subroutine
_build_upload: called CC=3 SCAR=11.1
| |||||||||||||||
| 189 | 5 | my $self = shift; | |||||||||||||
| 190 | 5 | for my $file ( @{ $self->controller->req->uploads('files') } ) { | |||||||||||||
| 191 | 1 | ▶ | if ( $file->name eq 'pause99_add_uri_httpupload' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 192 | 1 | return $file; | |||||||||||||
| 193 | } | ||||||||||||||
| 194 | } | ||||||||||||||
| 195 | } | ||||||||||||||
| 196 | |||||||||||||||
| 197 | 5 | ▶ | sub _build_author { | ||||||||||||
|
Subroutine
_build_author: called CC=3 SCAR=11.0
| |||||||||||||||
| 198 | 5 | my $self = shift; | |||||||||||||
| 199 | 5 | my $author = 'DUMMY'; | |||||||||||||
| 200 | |||||||||||||||
| 201 | 5 | ▶ | if ( $self->controller->param("HIDDENNAME") ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 202 | 1 | $author = uc( $self->controller->param("HIDDENNAME") ); | |||||||||||||
| 203 | } | ||||||||||||||
| 204 | elsif ( $self->controller->param("author") ) { | ||||||||||||||
| 205 | 3 | $author = uc( $self->controller->param("author") ); | |||||||||||||
| 206 | } | ||||||||||||||
| 207 | |||||||||||||||
| 208 | 5 | return $author; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | 3 | ▶ | sub _build_indexer { | ||||||||||||
|
Subroutine
_build_indexer: called CC=1 SCAR=0.0
| |||||||||||||||
| 212 | 3 | my $self = shift; | |||||||||||||
| 213 | 3 | return OrePAN2::Indexer->new( directory => $self->config->directory ); | |||||||||||||
| 214 | } | ||||||||||||||
| 215 | |||||||||||||||
| 216 | 13 | ▶ | sub _build_config { | ||||||||||||
|
Subroutine
_build_config: called CC=1 SCAR=0.0
| |||||||||||||||
| 217 | 13 | return Mojo::Darkpan::Config->new; | |||||||||||||
| 218 | } | ||||||||||||||
| 219 | |||||||||||||||
| 220 | 7 | ▶ | sub _build_packageFile { | ||||||||||||
|
Subroutine
_build_packageFile: called CC=2 SCAR=7.0
| |||||||||||||||
| 221 | 7 | my $self = shift; | |||||||||||||
| 222 | 7 | my $dir = $self->config->directory; | |||||||||||||
| 223 | 7 | ▶ | my $pkgfname = File::Spec->catfile( $dir, 'modules', | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | $self->config->no_compress | ||||||||||||||
| 225 | ? '02packages.details.txt' | ||||||||||||||
| 226 | : '02packages.details.txt.gz' ); | ||||||||||||||
| 227 | 7 | return $pkgfname; | |||||||||||||
| 228 | } | ||||||||||||||
| 229 | |||||||||||||||
| 230 | 2 | 1; | |||||||||||||