From 67df78608d6c475d61a51de8ecd5596f437f2481 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 20 May 2009 18:37:00 +0000 Subject: [PATCH] Add a method to Storage to return the hex value of the storageId. Add a test for it. Convert Storage to use that everywhere, instead of calculating and recalculating it. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Storage.pm | 37 +++++++++++++++++++++++++------------ t/Storage.t | 5 +++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index c4a5275d4..548d24991 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -17,6 +17,7 @@ - fixed Thread subscription groups not cleaned up during purge. - fixed: Story subtitle is supposed to be a text field, not a text area. - fixed: Display of short pagination bar when there's less than 1 page of data in StoryArchive + - added: Storage now has a getHexId method for returning a cached hexadecimal version of the storageId. 7.7.6 - Added mobile style template. If enabled in settings, will serve alternate style templates diff --git a/lib/WebGUI/Storage.pm b/lib/WebGUI/Storage.pm index 46f01436f..505a50617 100644 --- a/lib/WebGUI/Storage.pm +++ b/lib/WebGUI/Storage.pm @@ -120,7 +120,7 @@ sub _cdnAdd { my $cdnCfg = $self->session->config->get('cdn'); if ( $cdnCfg and $cdnCfg->{'enabled'} ) { if ( $cdnCfg->{'queuePath'} ) { - my $cdnFile = $cdnCfg->{'queuePath'} . '/' . $self->session->id->toHex( $self->getId ); + my $cdnFile = $cdnCfg->{'queuePath'} . '/' . $self->getHexId; my $dest; if ( open $dest, '>', $cdnFile ) { close $dest; # created empty file @@ -160,7 +160,7 @@ sub _cdnDel { unlink $cdnFile; } if ( $cdnCfg->{'queuePath'} ) { - $cdnFile = $cdnCfg->{'queuePath'} . '/' . $self->session->id->toHex( $self->getId ); + $cdnFile = $cdnCfg->{'queuePath'} . '/' . $self->getHexId; my $dest; if ( open $dest, '>', $cdnFile ) { print $dest "deleted\n"; @@ -617,7 +617,7 @@ sub createTemp { my $path = $session->id->toHex($id); $path =~ m/^(.{2})/; - my $self = {_session=>$session, _id => $id, _pathParts => ['temp', $1, $path], _errors => []}; + my $self = {_session=>$session, _id => $id, _Hexid => $path, _pathParts => ['temp', $1, $path], _errors => []}; bless $self, ref($class)||$class; $self->_makePath; return $self; @@ -690,7 +690,7 @@ sub deleteFromCdn { and $cdnCfg->{'enabled'} and $cdnCfg->{'syncProgram'} ) { - my $id = $self->session->id->toHex( $self->getId ); + my $id = $self->getHexId; my $cmd = sprintf( $cdnCfg->{'deleteProgram'}, $id ); if ( $cmd =~ /$id/ ) { # sanity check, no rm -rf / system($cmd); @@ -698,7 +698,7 @@ sub deleteFromCdn { $self->_addError("Error running CDN deleteProgram: $?"); } if ( $cdnCfg->{'queuePath'} ) { - unlink $cdnCfg->{'queuePath'} . '/' . $self->session->id->toHex( $self->getId ); + unlink $cdnCfg->{'queuePath'} . '/' . $self->getHexId; } } else { # Presume configuration error, missing %s @@ -730,7 +730,7 @@ sub get { my $id = shift; return undef unless $id; - my $self = bless {_session=>$session, _id => $id, _errors => []}, $class; + my $self = bless {_session=>$session, _id => $id, _hexId => $session->id->toHex($id), _errors => []}, $class; my $uploadsRoot = $session->config->get('uploadsPath'); my @parts = ($id =~ m/^((.{2})(.{2}).+)/)[1,2,0]; @@ -739,8 +739,7 @@ sub get { return $self; } if (!-e join('/', $uploadsRoot, @parts)) { - my $hexId = $session->id->toHex($id); - @parts = ($hexId =~ m/^((.{2})(.{2}).+)/)[1,2,0]; + @parts = ($self->getHexId =~ m/^((.{2})(.{2}).+)/)[1,2,0]; } $self->{_pathParts} = \@parts; # create the folder in case it got deleted somehow @@ -1047,6 +1046,20 @@ sub getFileId { #------------------------------------------------------------------- +=head2 getHexId ( ) + +Returns the unique identifier of this storage location in hexadecimal format +instead of base64. + +=cut + +sub getHexId { + my $self = shift; + return $self->{_hexId}; +} + +#------------------------------------------------------------------- + =head2 getId ( ) Returns the unique identifier of this storage location. @@ -1205,12 +1218,12 @@ sub getUrl { { if ( $cdnCfg->{'sslUrl'} ) { substr( $cdnCfg->{'sslUrl'}, -1 ) eq '/' and $sep = ''; - $url = $cdnCfg->{'sslUrl'} . $sep . $self->session->id->toHex( $self->getId ); + $url = $cdnCfg->{'sslUrl'} . $sep . $self->getHexId; } # else do NOT override $url with CDN URL ($url = $sslUrl || $url) } else { substr( $cdnCfg->{'url'}, -1 ) eq '/' and $sep = ''; - $url = $cdnCfg->{'url'} . $sep . $self->session->id->toHex( $self->getId ); + $url = $cdnCfg->{'url'} . $sep . $self->getHexId; } } ## end if ( $cdnCfg and $cdnCfg... if ( defined $file ) { @@ -1614,13 +1627,13 @@ sub syncToCdn { 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 . " : $!"; - my $cmd = sprintf( $cdnCfg->{'syncProgram'}, $self->session->id->toHex( $self->getId ) ); + my $cmd = sprintf( $cdnCfg->{'syncProgram'}, $self->getHexId ); system($cmd); if ($?) { $self->_addError("Error running CDN syncProgram: $?"); } elsif ( $cdnCfg->{'queuePath'} ) { - unlink $cdnCfg->{'queuePath'} . '/' . $self->session->id->toHex( $self->getId ); + unlink $cdnCfg->{'queuePath'} . '/' . $self->getHexId; } chdir $originalDir; my $dest; diff --git a/t/Storage.t b/t/Storage.t index e57659ac6..c288d0f07 100644 --- a/t/Storage.t +++ b/t/Storage.t @@ -29,7 +29,7 @@ my $cwd = Cwd::cwd(); my ($extensionTests, $fileIconTests) = setupDataDrivenTests($session); -my $numTests = 102; # increment this value for each test you create +my $numTests = 103; # increment this value for each test you create plan tests => $numTests + scalar @{ $extensionTests } + scalar @{ $fileIconTests }; my $uploadDir = $session->config->get('uploadsPath'); @@ -142,7 +142,7 @@ undef $storage3; #################################################### # -# create +# create, getHexId # #################################################### @@ -150,6 +150,7 @@ $storage1 = WebGUI::Storage->create($session); isa_ok( $storage1, "WebGUI::Storage"); ok($session->id->valid($storage1->getId), 'create returns valid sessionIds'); +is($storage1->getHexId, $session->id->toHex($storage1->getId), 'getHexId, returns the hexadecimal value of the GUID'); is( $storage1->getErrorCount, 0, "No errors during object creation");