Fixed bugs in storage causing multiple storage locations to be created any time a file was created or updated. Fixed a bug which prevents files from potentially breaking on operating systems that are not case sensitive.

This commit is contained in:
Frank Dillon 2007-05-04 19:19:01 +00:00
parent 1f0dd18f61
commit 9a8207fee0
5 changed files with 196 additions and 41 deletions

View file

@ -55,11 +55,15 @@ Override the default method in order to deal with attachments.
sub addRevision {
my $self = shift;
my $newSelf = $self->SUPER::addRevision(@_);
if ($self->get("storageId")) {
my $properties = shift;
if ($self->get("storageId") ne "") {
my $newStorage = WebGUI::Storage->get($self->session,$self->get("storageId"))->copy;
$newSelf->update({storageId=>$newStorage->getId});
$properties->{storageId} = $newStorage->getId;
}
my $newSelf = $self->SUPER::addRevision($properties);
return $newSelf;
}
@ -243,12 +247,26 @@ sub prepareView {
#-------------------------------------------------------------------
sub processPropertiesFromFormPost {
my $self = shift;
my $self = shift;
my $session = $self->session;
$self->SUPER::processPropertiesFromFormPost;
#Get the storage location out of memory. If you call getStorageLocation you risk creating another one.
my $storageLocation = $self->{_storageLocation};
my $storageId = undef;
$storageId = $storageLocation->getId if(defined $storageLocation);
#Now remove the storage location to prevent wierd caching stuff.
delete $self->{_storageLocation};
my $fileStorageId = WebGUI::Form::File->new($self->session, {name => 'newFile'})->getValueFromPost;
my $storage = WebGUI::Storage->get($self->session, $fileStorageId);
#Clear the storage location if a file was uploaded.
if($session->form->get("newFile_file") ne "") {
$storageLocation->clear();
}
#Pass in the storage Id to prevent another one from being created.
my $fileStorageId = WebGUI::Form::File->new($session, {name => 'newFile', value=>$storageId })->getValueFromPost;
my $storage = WebGUI::Storage->get($session, $fileStorageId);
if (defined $storage) {
$storage->setPrivileges($self->get('ownerUserId'), $self->get('groupIdView'), $self->get('groupIdEdit'));
@ -258,9 +276,9 @@ sub processPropertiesFromFormPost {
my %data;
$data{filename} = $filename;
$data{storageId} = $storage->getId;
$data{title} = $filename unless ($self->session->form->process("title"));
$data{menuTitle} = $filename unless ($self->session->form->process("menuTitle"));
$data{url} = $self->getParent->get('url').'/'.$filename unless ($self->session->form->process("url"));
$data{title} = $filename unless ($session->form->process("title"));
$data{menuTitle} = $filename unless ($session->form->process("menuTitle"));
$data{url} = $self->getParent->get('url').'/'.$filename unless ($session->form->process("url"));
$self->update(\%data);
$self->setSize($storage->getFileSize($filename));
}
@ -306,11 +324,13 @@ sub purgeRevision {
sub setSize {
my $self = shift;
my $fileSize = shift || 0;
my $storage = $self->getStorageLocation;
foreach my $file (@{$storage->getFiles}) {
$fileSize += $storage->getFileSize($file);
my $storage = $self->{_storageLocation};
if (defined $storage) {
foreach my $file (@{$storage->getFiles}) {
$fileSize += $storage->getFileSize($file);
}
}
$self->SUPER::setSize($fileSize);
return $self->SUPER::setSize($fileSize);
}
#-------------------------------------------------------------------

View file

@ -224,10 +224,13 @@ sub processPropertiesFromFormPost {
#-------------------------------------------------------------------
sub setSize {
my $self = shift;
my $self = shift;
my $input = shift;
my $storage = $self->getStorageLocation;
my $size = ($input > $storage->getFileSize($self->get("filename"))) ? $input : $storage->getFileSize($self->get("filename"));
my $size = 0;
my $storage = $self->{_storageLocation};
if (defined $storage) {
$size = ($input > $storage->getFileSize($self->get("filename"))) ? $input : $storage->getFileSize($self->get("filename"));
}
return $self->SUPER::setSize($size);
}

View file

@ -139,15 +139,17 @@ sub editSave {
##This is a hack. File uploads should go through the WebGUI::Form::File API
my $tempFileStorageId = WebGUI::Form::File->new($self->session,{name => 'file'})->getValueFromPost;
my $tempStorage = WebGUI::Storage->get($self->session, $tempFileStorageId);
my $tempStorage = WebGUI::Storage::Image->get($self->session, $tempFileStorageId);
foreach my $filename (@{$tempStorage->getFiles}) {
my $storage = WebGUI::Storage::Image->create($self->session);
$storage->addFileFromFilesystem($tempStorage->getPath($filename));
#my $storage = WebGUI::Storage::Image->create($self->session);
#$storage->addFileFromFilesystem($tempStorage->getPath($filename));
#$storage->setPrivileges($self->getParent->get("ownerUserId"),$self->getParent->get("groupIdView"),$self->getParent->get("groupIdEdit"));
my %data;
my $selfName = 'WebGUI::Asset::File';
$selfName = "WebGUI::Asset::File::Image" if ($storage->isImage($filename));
$selfName = "WebGUI::Asset::File::Image" if ($tempStorage->isImage($filename));
foreach my $definition (@{$selfName->definition($self->session)}) {
foreach my $property (keys %{$definition->{properties}}) {
$data{$property} = $self->session->form->process(
@ -157,9 +159,9 @@ sub editSave {
);
}
}
$storage->setPrivileges($data{"ownerUserId"},$data{"groupIdView"},$data{"groupIdEdit"});
$data{className} = $selfName;
$data{storageId} = $storage->getId;
#$data{storageId} = $storage->getId;
$data{filename} = $data{title} = $data{menuTitle} = $filename;
$data{templateId} = 'PBtmpl0000000000000024';
if ($selfName eq "WebGUI::Asset::File::Image") {
@ -168,22 +170,34 @@ sub editSave {
# Resize image if it is bigger than the max allowed image size.
my $maxSize = $self->session->setting->get("maxImageSize");
my ($width, $height) = $storage->getSizeInPixels($filename);
my ($width, $height) = $tempStorage->getSizeInPixels($filename);
if($width > $maxSize || $height > $maxSize) {
if($width > $height) {
$storage->resize($filename, $maxSize);
$tempStorage->resize($filename, $maxSize);
}
else {
$storage->resize($filename, 0, $maxSize);
$tempStorage->resize($filename, 0, $maxSize);
}
}
}
$data{url} = $self->getParent->get('url').'/'.$filename;
#Create the new asset
my $newAsset = $self->getParent->addChild(\%data);
delete $newAsset->{_storageLocation};
$newAsset->setSize($storage->getFileSize($filename));
#Get the current storage location
my $storage = $newAsset->getStorageLocation();
$storage->addFileFromFilesystem($tempStorage->getPath($filename));
$storage->setPrivileges($data{"ownerUserId"},$data{"groupIdView"},$data{"groupIdEdit"});
$newAsset->setSize($tempStorage->getFileSize($filename));
$newAsset->generateThumbnail if ($selfName eq "WebGUI::Asset::File::Image");
$newAsset->update({ storageId=> $storage->getId });
#Now remove the reference to the storeage location to prevent problems with different revisions.
delete $newAsset->{_storageLocation};
}
$tempStorage->delete;
return $self->getParent->www_manageAssets if ($self->session->form->process("proceed") eq "manageAssets");