CDN: perltidy on new/changed code
This commit is contained in:
parent
d309dbce6a
commit
2fc4f3b54d
6 changed files with 291 additions and 279 deletions
|
|
@ -42,6 +42,18 @@ These methods are available from this class:
|
|||
|
||||
=cut
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 DESTROY ( )
|
||||
|
||||
Deconstructor.
|
||||
|
||||
=cut
|
||||
|
||||
sub DESTROY {
|
||||
my $self = shift;
|
||||
undef $self;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -64,20 +76,6 @@ sub fromHex {
|
|||
return $id;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 DESTROY ( )
|
||||
|
||||
Deconstructor.
|
||||
|
||||
=cut
|
||||
|
||||
sub DESTROY {
|
||||
my $self = shift;
|
||||
undef $self;
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 getValidator
|
||||
|
|
|
|||
|
|
@ -148,11 +148,14 @@ sub extras {
|
|||
my $cdnCfg = $self->session->config->get('cdn');
|
||||
if ( $cdnCfg and $cdnCfg->{'enabled'} and $cdnCfg->{'extrasCdn'} ) {
|
||||
unless ( $path and grep $path =~ m/$_/, @{ $cdnCfg->{'extrasExclude'} } ) {
|
||||
if ($cdnCfg->{'extrasSsl'} and
|
||||
($self->session->env->get('HTTPS') eq 'on' or
|
||||
$self->session->env->get('SSLPROXY'))) {
|
||||
if ($cdnCfg->{'extrasSsl'}
|
||||
and ( $self->session->env->get('HTTPS') eq 'on'
|
||||
or $self->session->env->get('SSLPROXY') )
|
||||
)
|
||||
{
|
||||
$url = $cdnCfg->{'extrasSsl'};
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$url = $cdnCfg->{'extrasCdn'};
|
||||
}
|
||||
} # if excluded, stick with regular extrasURL
|
||||
|
|
@ -160,7 +163,7 @@ sub extras {
|
|||
$url .= '/' . $path;
|
||||
$url =~ s$(?<!:)/{2,}$/$g; ##Remove //, unless it's after a :, which can't be a valid URL character
|
||||
return $url;
|
||||
}
|
||||
} ## end sub extras
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -105,7 +105,6 @@ sub _addError {
|
|||
$self->session->errorHandler->error($errorMessage);
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 _cdnAdd ( )
|
||||
|
|
@ -125,15 +124,16 @@ sub _cdnAdd {
|
|||
my $dest;
|
||||
if ( open $dest, '>', $cdnFile ) {
|
||||
close $dest; # created empty file
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$self->_addError( "CDN: Couldn't open file $cdnFile for writing due to error: " . $! );
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$self->_addError('Invalid CDN configuration - missing queuePath');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} ## end sub _cdnAdd
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -165,15 +165,16 @@ sub _cdnDel {
|
|||
if ( open $dest, '>', $cdnFile ) {
|
||||
print $dest "deleted\n";
|
||||
close $dest;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$self->_addError( "Couldn't open file $cdnFile for writing due to error: " . $! );
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$self->_addError('Invalid CDN configuration - missing queuePath');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} ## end if ( $cdnCfg and $cdnCfg...
|
||||
} ## end sub _cdnDel
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -685,8 +686,10 @@ Also delete the related file in config/cdn/queuePath.
|
|||
sub deleteFromCdn {
|
||||
my $self = shift;
|
||||
my $cdnCfg = $self->session->config->get('cdn');
|
||||
if ($cdnCfg and $cdnCfg->{'enabled'}
|
||||
and $cdnCfg->{'syncProgram'}) {
|
||||
if ( $cdnCfg
|
||||
and $cdnCfg->{'enabled'}
|
||||
and $cdnCfg->{'syncProgram'} )
|
||||
{
|
||||
my $id = $self->session->id->toHex( $self->getId );
|
||||
my $cmd = sprintf( $cdnCfg->{'deleteProgram'}, $id );
|
||||
if ( $cmd =~ /$id/ ) { # sanity check, no rm -rf /
|
||||
|
|
@ -697,12 +700,12 @@ sub deleteFromCdn {
|
|||
if ( $cdnCfg->{'queuePath'} ) {
|
||||
unlink $cdnCfg->{'queuePath'} . '/' . $self->session->id->toHex( $self->getId );
|
||||
}
|
||||
} else { # Presume configuration error, missing %s
|
||||
}
|
||||
else { # Presume configuration error, missing %s
|
||||
$self->_addError("CDN deleteProgram: storage ID missing from command: $cmd");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} ## end if ( $cdnCfg and $cdnCfg...
|
||||
} ## end sub deleteFromCdn
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -820,8 +823,7 @@ sub getCdnFileIterator {
|
|||
if ( $cdnCfg and $cdnCfg->{'enabled'} ) {
|
||||
if ( $cdnCfg->{'queuePath'} ) {
|
||||
if ( opendir my $DH, $cdnCfg->{'queuePath'} ) {
|
||||
my @ids = grep { !/^\.+$/ }
|
||||
readdir($DH);
|
||||
my @ids = grep { !/^\.+$/ } readdir($DH);
|
||||
close $DH;
|
||||
my $sub = sub {
|
||||
my $id = shift @ids;
|
||||
|
|
@ -829,15 +831,16 @@ sub getCdnFileIterator {
|
|||
return $class->get( $session, $session->id->fromHex($id) );
|
||||
};
|
||||
return $sub;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$session->errorHandler->warn("CDN: cannot read directory $cdnCfg->{'queuePath'}");
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$session->errorHandler->warn("CDN: enabled but no queuePath");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} ## end if ( $cdnCfg and $cdnCfg...
|
||||
} ## end sub getCdnFileIterator
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -1187,32 +1190,34 @@ If specified, we'll return a URL to the file rather than the storage location.
|
|||
sub getUrl {
|
||||
my $self = shift;
|
||||
my $file = shift;
|
||||
my $url = $self->session->config->get("uploadsURL")
|
||||
. '/'
|
||||
. $self->getPathFrag;
|
||||
my $url = $self->session->config->get("uploadsURL") . '/' . $self->getPathFrag;
|
||||
my $cdnCfg = $self->session->config->get('cdn');
|
||||
if ($cdnCfg and $cdnCfg->{'enabled'} and $cdnCfg->{'url'}
|
||||
and -e $self->getPath . '/.cdn') {
|
||||
if ( $cdnCfg
|
||||
and $cdnCfg->{'enabled'}
|
||||
and $cdnCfg->{'url'}
|
||||
and -e $self->getPath . '/.cdn' )
|
||||
{
|
||||
my $sep = '/'; # separator, if not already present trailing
|
||||
if ($cdnCfg->{'sslAlt'} and
|
||||
($self->session->env->get('HTTPS') eq 'on' or
|
||||
$self->session->env->get('SSLPROXY'))) {
|
||||
if ($cdnCfg->{'sslAlt'}
|
||||
and ( $self->session->env->get('HTTPS') eq 'on'
|
||||
or $self->session->env->get('SSLPROXY') )
|
||||
)
|
||||
{
|
||||
if ( $cdnCfg->{'sslUrl'} ) {
|
||||
substr( $cdnCfg->{'sslUrl'}, -1 ) eq '/' and $sep = '';
|
||||
$url = $cdnCfg->{'sslUrl'} . $sep . $self->session->id->toHex( $self->getId );
|
||||
} # else do NOT override $url with CDN URL ($url = $sslUrl || $url)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
substr( $cdnCfg->{'url'}, -1 ) eq '/' and $sep = '';
|
||||
$url = $cdnCfg->{'url'} . $sep . $self->session->id->toHex( $self->getId );
|
||||
}
|
||||
}
|
||||
} ## end if ( $cdnCfg and $cdnCfg...
|
||||
if ( defined $file ) {
|
||||
$url .= '/' . $file;
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
|
||||
} ## end sub getUrl
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
@ -1602,8 +1607,10 @@ the related file in config/cdn/queuePath.
|
|||
sub syncToCdn {
|
||||
my $self = shift;
|
||||
my $cdnCfg = $self->session->config->get('cdn');
|
||||
if ($cdnCfg and $cdnCfg->{'enabled'}
|
||||
and $cdnCfg->{'syncProgram'}) {
|
||||
if ( $cdnCfg
|
||||
and $cdnCfg->{'enabled'}
|
||||
and $cdnCfg->{'syncProgram'} )
|
||||
{
|
||||
my $originalDir = Cwd::cwd();
|
||||
my $locDir = join '/', $self->session->config->get('uploadsPath'), @{ $self->{_pathParts} }[ 0 .. 1 ];
|
||||
chdir $locDir or croak 'Unable to chdir to ' . $locDir . " : $!";
|
||||
|
|
@ -1611,7 +1618,8 @@ sub syncToCdn {
|
|||
system($cmd);
|
||||
if ($?) {
|
||||
$self->_addError("Error running CDN syncProgram: $?");
|
||||
} elsif ($cdnCfg->{'queuePath'}) {
|
||||
}
|
||||
elsif ( $cdnCfg->{'queuePath'} ) {
|
||||
unlink $cdnCfg->{'queuePath'} . '/' . $self->session->id->toHex( $self->getId );
|
||||
}
|
||||
chdir $originalDir;
|
||||
|
|
@ -1619,12 +1627,12 @@ sub syncToCdn {
|
|||
my $cdnFile = $self->getPath . '/.cdn';
|
||||
if ( open $dest, '>', $cdnFile ) {
|
||||
close $dest; # created empty file
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$self->_addError( "Couldn't open file $cdnFile for writing due to error: " . $! );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} ## end if ( $cdnCfg and $cdnCfg...
|
||||
} ## end sub syncToCdn
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -34,11 +34,10 @@ GetOptions(
|
|||
'quiet' => \$quiet,
|
||||
);
|
||||
|
||||
|
||||
if ( $configFile eq "" ) {
|
||||
printHelp();
|
||||
exit 4;
|
||||
};
|
||||
}
|
||||
|
||||
if ($help) {
|
||||
printHelp();
|
||||
|
|
@ -97,7 +96,7 @@ EXIT STATUS
|
|||
Content Delivery Network (CDN) is not enabled.
|
||||
|
||||
STOP
|
||||
}
|
||||
} ## end sub printHelp
|
||||
|
||||
if ( !( $^O =~ /^Win/i ) && $> != 0 && !$override ) {
|
||||
print "You must be the super user to use this utility.\n";
|
||||
|
|
@ -118,7 +117,8 @@ unless ($cdnCfg and $cdnCfg->{'enabled'} and $cdnCfg->{'queuePath'}) {
|
|||
# Here is the core of the script
|
||||
if ($migrate) {
|
||||
syncUploads($session);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
syncQueue( $session, $cdnCfg );
|
||||
}
|
||||
|
||||
|
|
@ -129,7 +129,6 @@ $session->close();
|
|||
print "OK\n" unless ($quiet);
|
||||
exit 0;
|
||||
|
||||
|
||||
#-----------------------------------------
|
||||
# syncQueue(session, cdnConfig)
|
||||
#-----------------------------------------
|
||||
|
|
@ -143,7 +142,8 @@ sub syncQueue {
|
|||
if ( -r $ctrlFile and -s $ctrlFile < 12 ) {
|
||||
if ( !-s $ctrlFile ) { # Empty means sync/add/update
|
||||
$store->syncToCdn;
|
||||
} else { # expect "deleted" but be careful.
|
||||
}
|
||||
else { # expect "deleted" but be careful.
|
||||
if ( open my $ctrlFH, "<$ctrlFile" ) {
|
||||
my $directive = <$ctrlFH>;
|
||||
chomp $directive;
|
||||
|
|
@ -151,24 +151,27 @@ sub syncQueue {
|
|||
if ( $directive =~ m/^deleted$/i ) {
|
||||
$store->deleteFromCdn;
|
||||
} # else unknown - ignore
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
warn "Cannot read CDN control file $ctrlFile.";
|
||||
$session->errorHandler->warn("Cannot read CDN control file $ctrlFile.");
|
||||
}
|
||||
}
|
||||
} else { # missing or invalid
|
||||
print "No recognizable CDN control file $ctrlFile.\n";
|
||||
}
|
||||
} ## end if ( -r $ctrlFile and ...
|
||||
else { # missing or invalid
|
||||
warn "No recognizable CDN control file $ctrlFile.";
|
||||
$session->errorHandler->warn("No recognizable CDN control file $ctrlFile.");
|
||||
}
|
||||
} ## end while ( my $store = $locIter...
|
||||
} # end syncQueue
|
||||
|
||||
|
||||
#-----------------------------------------
|
||||
# syncUploads(session)
|
||||
#-----------------------------------------
|
||||
|
||||
sub syncUploads {
|
||||
my $session = shift;
|
||||
|
||||
# Alternate approach would be touch queue files, then run queue.
|
||||
my $uDir = $session->config->get('uploadsPath');
|
||||
if ( opendir my $DH, $uDir ) {
|
||||
|
|
@ -180,27 +183,28 @@ sub syncUploads {
|
|||
if ( opendir my $S2, "$uDir/$subdir/$sub2" ) {
|
||||
my @fileId = grep { !/^\.+$/ } readdir($S2);
|
||||
foreach my $fileId (@fileId) {
|
||||
my $store = WebGUI::Storage->get($session,
|
||||
$session->id->fromHex($fileId));
|
||||
my $store = WebGUI::Storage->get( $session, $session->id->fromHex($fileId) );
|
||||
$store->syncToCdn; # here is the meat
|
||||
}
|
||||
close $S2;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$session->errorHandler->warn("Unable to open $sub2 for directory reading");
|
||||
}
|
||||
}
|
||||
close $SD;
|
||||
} else {
|
||||
} ## end if ( opendir my $SD, "$uDir/$subdir")
|
||||
else {
|
||||
$session->errorHandler->warn("Unable to open $subdir for directory reading");
|
||||
}
|
||||
}
|
||||
} ## end foreach my $subdir (@part1)
|
||||
close $DH;
|
||||
} else {
|
||||
} ## end if ( opendir my $DH, $uDir)
|
||||
else {
|
||||
$session->errorHandler->warn("Unable to open $uDir for directory reading");
|
||||
}
|
||||
} # end syncUploads
|
||||
|
||||
|
||||
__DATA__
|
||||
This exists so flock() code above works.
|
||||
DO NOT REMOVE THIS DATA SECTION.
|
||||
|
|
|
|||
|
|
@ -381,7 +381,8 @@ foreach my $iconTest (@{ $fileIconTests }) {
|
|||
#
|
||||
####################################################
|
||||
|
||||
my $cdnCfg = { "enabled" => 1,
|
||||
my $cdnCfg = {
|
||||
"enabled" => 1,
|
||||
"url" => "file:///data/storage",
|
||||
"queuePath" => "/data/cdnqueue",
|
||||
"syncProgram" => "cp -r -- '%s' /data/storage/",
|
||||
|
|
@ -430,8 +431,6 @@ if (is(ref($locIter), 'CODE', 'CDN: getCdnFileIterator to return sub ref')) {
|
|||
}
|
||||
}
|
||||
is ($found, 1, 'CDN: getCdnFileIterator found storage');
|
||||
# TODO: I'm not sure how to make these run on MS-Windows.
|
||||
# Should we SKIP in the meantime? ($^O eq 'MSWin32')
|
||||
### syncToCdn
|
||||
$cdnStorage->syncToCdn;
|
||||
ok( (-e $cdnPath and -d $cdnPath), 'CDN: target directory created');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue