Update Asset::File for Moose
This commit is contained in:
parent
6863f66e12
commit
387866cfcb
3 changed files with 68 additions and 80 deletions
|
|
@ -15,8 +15,40 @@ package WebGUI::Asset::File;
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use base 'WebGUI::Asset';
|
|
||||||
use Carp;
|
use Carp;
|
||||||
|
|
||||||
|
|
||||||
|
use WebGUI::Definition::Asset;
|
||||||
|
extends 'WebGUI::Asset';
|
||||||
|
|
||||||
|
aspect assetName => ['assetName', 'Asset_File'];
|
||||||
|
aspect tableName => 'FileAsset';
|
||||||
|
property cacheTimeout => (
|
||||||
|
tab => "display",
|
||||||
|
fieldType => "interval",
|
||||||
|
default => 3600,
|
||||||
|
uiLevel => 8,
|
||||||
|
label => ["cache timeout", 'Asset_File'],
|
||||||
|
hoverHelp => ["cache timeout help", 'Asset_File'],
|
||||||
|
);
|
||||||
|
property filename => (
|
||||||
|
noFormPost => 1,
|
||||||
|
fieldType => 'hidden',
|
||||||
|
default => '',
|
||||||
|
);
|
||||||
|
property storageId => (
|
||||||
|
noFormPost => 1,
|
||||||
|
fieldType => 'hidden',
|
||||||
|
default => '',
|
||||||
|
);
|
||||||
|
property templateId => (
|
||||||
|
fieldType => 'template',
|
||||||
|
default => 'PBtmpl0000000000000024',
|
||||||
|
label => ['file template', 'Asset_File'],
|
||||||
|
hoverHelp => ['file template description', 'Asset_File'],
|
||||||
|
namespace => "FileAsset",
|
||||||
|
);
|
||||||
|
|
||||||
use WebGUI::Storage;
|
use WebGUI::Storage;
|
||||||
use WebGUI::SQL;
|
use WebGUI::SQL;
|
||||||
use WebGUI::Utility;
|
use WebGUI::Utility;
|
||||||
|
|
@ -55,8 +87,8 @@ sub addRevision {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $newSelf = $self->SUPER::addRevision(@_);
|
my $newSelf = $self->SUPER::addRevision(@_);
|
||||||
|
|
||||||
if ($newSelf->get("storageId") && $newSelf->get("storageId") eq $self->get('storageId')) {
|
if ($newSelf->storageId && $newSelf->storageId eq $self->storageId) {
|
||||||
my $newStorage = $self->getStorageClass->get($self->session,$self->get("storageId"))->copy;
|
my $newStorage = $self->getStorageClass->get($self->session, $self->storageId)->copy;
|
||||||
$newSelf->update({storageId => $newStorage->getId});
|
$newSelf->update({storageId => $newStorage->getId});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,61 +109,11 @@ A hash reference of optional parameters. None at this time.
|
||||||
|
|
||||||
sub applyConstraints {
|
sub applyConstraints {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
$self->getStorageLocation->setPrivileges($self->get('ownerUserId'), $self->get('groupIdView'), $self->get('groupIdEdit'));
|
$self->getStorageLocation->setPrivileges($self->ownerUserId, $self->groupIdView, $self->groupIdEdit);
|
||||||
$self->setSize;
|
$self->setSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
|
||||||
|
|
||||||
=head2 definition ( definition )
|
|
||||||
|
|
||||||
Defines the properties of this asset.
|
|
||||||
|
|
||||||
=head3 definition
|
|
||||||
|
|
||||||
A hash reference passed in from a subclass definition.
|
|
||||||
|
|
||||||
=cut
|
|
||||||
|
|
||||||
sub definition {
|
|
||||||
my $class = shift;
|
|
||||||
my $session = shift;
|
|
||||||
my $definition = shift;
|
|
||||||
my $i18n = WebGUI::International->new($session,"Asset_File");
|
|
||||||
push(@{$definition}, {
|
|
||||||
assetName=>$i18n->get('assetName'),
|
|
||||||
tableName=>'FileAsset',
|
|
||||||
className=>'WebGUI::Asset::File',
|
|
||||||
properties=>{
|
|
||||||
cacheTimeout => {
|
|
||||||
tab => "display",
|
|
||||||
fieldType => "interval",
|
|
||||||
defaultValue => 3600,
|
|
||||||
uiLevel => 8,
|
|
||||||
label => $i18n->get("cache timeout"),
|
|
||||||
hoverHelp => $i18n->get("cache timeout help")
|
|
||||||
},
|
|
||||||
filename=>{
|
|
||||||
noFormPost=>1,
|
|
||||||
fieldType=>'hidden',
|
|
||||||
defaultValue=>'',
|
|
||||||
},
|
|
||||||
storageId=>{
|
|
||||||
noFormPost=>1,
|
|
||||||
fieldType=>'hidden',
|
|
||||||
defaultValue=>'',
|
|
||||||
},
|
|
||||||
templateId=>{
|
|
||||||
fieldType=>'template',
|
|
||||||
defaultValue=>'PBtmpl0000000000000024'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return $class->SUPER::definition($session, $definition);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
=head2 duplicate
|
=head2 duplicate
|
||||||
|
|
@ -160,7 +142,7 @@ See WebGUI::AssetPackage::exportAssetData() for details.
|
||||||
sub exportAssetData {
|
sub exportAssetData {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $data = $self->SUPER::exportAssetData;
|
my $data = $self->SUPER::exportAssetData;
|
||||||
push(@{$data->{storage}}, $self->get("storageId")) if ($self->get("storageId") ne "");
|
push(@{$data->{storage}}, $self->storageId) if ($self->storageId ne "");
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,8 +177,8 @@ sub exportWriteFile {
|
||||||
WebGUI::Error->throw(error => "could not make directory " . $parent->absolute->stringify);
|
WebGUI::Error->throw(error => "could not make directory " . $parent->absolute->stringify);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! File::Copy::copy($self->getStorageLocation->getPath($self->get('filename')), $dest->stringify) ) {
|
if ( ! File::Copy::copy($self->getStorageLocation->getPath($self->filename), $dest->stringify) ) {
|
||||||
WebGUI::Error->throw(error => "can't copy " . $self->getStorageLocation->getPath($self->get('filename'))
|
WebGUI::Error->throw(error => "can't copy " . $self->getStorageLocation->getPath($self->filename)
|
||||||
. ' to ' . $dest->absolute->stringify . ": $!");
|
. ' to ' . $dest->absolute->stringify . ": $!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -238,9 +220,9 @@ sub getEditFormUploadControl {
|
||||||
my $i18n = WebGUI::International->new($session, 'Asset_File');
|
my $i18n = WebGUI::International->new($session, 'Asset_File');
|
||||||
my $html = '';
|
my $html = '';
|
||||||
|
|
||||||
if ($self->get("filename") ne "") {
|
if ($self->filename ne "") {
|
||||||
$html .= WebGUI::Form::readOnly( $session, {
|
$html .= WebGUI::Form::readOnly( $session, {
|
||||||
value => '<p style="display:inline;vertical-align:middle;"><a href="'.$self->getFileUrl.'"><img src="'.$self->getFileIconUrl.'" alt="'.$self->get("filename").'" style="border-style:none;vertical-align:middle;" /> '.$self->get("filename").'</a></p>'
|
value => '<p style="display:inline;vertical-align:middle;"><a href="'.$self->getFileUrl.'"><img src="'.$self->getFileIconUrl.'" alt="'.$self->filename.'" style="border-style:none;vertical-align:middle;" /> '.$self->filename.'</a></p>'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -265,7 +247,7 @@ Returns the URL for the file stored in the storage location.
|
||||||
sub getFileUrl {
|
sub getFileUrl {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
#return $self->get("url");
|
#return $self->get("url");
|
||||||
return $self->getStorageLocation->getUrl($self->get("filename"));
|
return $self->getStorageLocation->getUrl($self->filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
@ -279,8 +261,8 @@ file, then it returns undef.
|
||||||
|
|
||||||
sub getFileIconUrl {
|
sub getFileIconUrl {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
return undef unless $self->get("filename"); ## Why do I have to do this when creating new Files?
|
return undef unless $self->filename; ## Why do I have to do this when creating new Files?
|
||||||
return $self->getStorageLocation->getFileIconUrl($self->get("filename"));
|
return $self->getStorageLocation->getFileIconUrl($self->filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -368,7 +350,7 @@ Indexing the content of the attachment. See WebGUI::Asset::indexContent() for ad
|
||||||
sub indexContent {
|
sub indexContent {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $indexer = $self->SUPER::indexContent;
|
my $indexer = $self->SUPER::indexContent;
|
||||||
$indexer->addFile($self->getStorageLocation->getPath($self->get("filename")));
|
$indexer->addFile($self->getStorageLocation->getPath($self->filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -383,7 +365,7 @@ See WebGUI::Asset::prepareView() for details.
|
||||||
sub prepareView {
|
sub prepareView {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
$self->SUPER::prepareView();
|
$self->SUPER::prepareView();
|
||||||
my $template = WebGUI::Asset::Template->new($self->session, $self->get("templateId"));
|
my $template = WebGUI::Asset::Template->new($self->session, $self->templateId);
|
||||||
$template->prepare($self->getMetaDataAsTemplateVariables);
|
$template->prepare($self->getMetaDataAsTemplateVariables);
|
||||||
$self->{_viewTemplate} = $template;
|
$self->{_viewTemplate} = $template;
|
||||||
}
|
}
|
||||||
|
|
@ -545,7 +527,7 @@ sub setStorageLocation {
|
||||||
$self->update({storageId=>$self->{_storageLocation}->getId});
|
$self->update({storageId=>$self->{_storageLocation}->getId});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$self->{_storageLocation} = $self->getStorageClass->get($self->session,$self->get("storageId"));
|
$self->{_storageLocation} = $self->getStorageClass->get($self->session,$self->storageId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -560,18 +542,18 @@ We override the update method from WebGUI::Asset in order to handle file system
|
||||||
sub update {
|
sub update {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my %before = (
|
my %before = (
|
||||||
owner => $self->get("ownerUserId"),
|
owner => $self->ownerUserId,
|
||||||
view => $self->get("groupIdView"),
|
view => $self->groupIdView,
|
||||||
edit => $self->get("groupIdEdit"),
|
edit => $self->groupIdEdit,
|
||||||
storageId => $self->get('storageId'),
|
storageId => $self->storageId,
|
||||||
);
|
);
|
||||||
$self->SUPER::update(@_);
|
$self->SUPER::update(@_);
|
||||||
##update may have entered a new storageId. Reset the cached one just in case.
|
##update may have entered a new storageId. Reset the cached one just in case.
|
||||||
if ($self->get("storageId") ne $before{storageId}) {
|
if ($self->storageId ne $before{storageId}) {
|
||||||
$self->setStorageLocation;
|
$self->setStorageLocation;
|
||||||
}
|
}
|
||||||
if ($self->get("ownerUserId") ne $before{owner} || $self->get("groupIdEdit") ne $before{edit} || $self->get("groupIdView") ne $before{view}) {
|
if ($self->ownerUserId ne $before{owner} || $self->groupIdEdit ne $before{edit} || $self->groupIdView ne $before{view}) {
|
||||||
$self->getStorageLocation->setPrivileges($self->get("ownerUserId"),$self->get("groupIdView"),$self->get("groupIdEdit"));
|
$self->getStorageLocation->setPrivileges($self->ownerUserId, $self->groupIdView, $self->groupIdEdit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -615,11 +597,11 @@ sub view {
|
||||||
$var{fileUrl} = $self->getFileUrl;
|
$var{fileUrl} = $self->getFileUrl;
|
||||||
$var{fileIcon} = $self->getFileIconUrl;
|
$var{fileIcon} = $self->getFileIconUrl;
|
||||||
$var{fileSize} = formatBytes($self->get("assetSize"));
|
$var{fileSize} = formatBytes($self->get("assetSize"));
|
||||||
my $out = $self->processTemplate(\%var,undef,$self->{_viewTemplate});
|
my $out = $self->processTemplate(\%var,undef,$self->{_viewTemplate});
|
||||||
if (!$self->session->var->isAdminOn && $self->get("cacheTimeout") > 10) {
|
if (!$self->session->var->isAdminOn && $self->get("cacheTimeout") > 10) {
|
||||||
eval{$self->session->cache->set("view_".$self->getId, $out, $self->get("cacheTimeout"))};
|
eval{$self->session->cache->set("view_".$self->getId, $out, $self->get("cacheTimeout"))};
|
||||||
}
|
}
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -639,7 +621,7 @@ sub www_edit {
|
||||||
my $i18n = WebGUI::International->new($self->session);
|
my $i18n = WebGUI::International->new($self->session);
|
||||||
my $tabform = $self->getEditForm;
|
my $tabform = $self->getEditForm;
|
||||||
$tabform->getTab("display")->template(
|
$tabform->getTab("display")->template(
|
||||||
-value=>$self->getValue("templateId"),
|
-value=>$self->templateId,
|
||||||
-hoverHelp=>$i18n->get('file template description','Asset_File'),
|
-hoverHelp=>$i18n->get('file template description','Asset_File'),
|
||||||
-namespace=>"FileAsset"
|
-namespace=>"FileAsset"
|
||||||
);
|
);
|
||||||
|
|
@ -660,14 +642,14 @@ sub www_view {
|
||||||
return $session->privilege->noAccess() unless $self->canView;
|
return $session->privilege->noAccess() unless $self->canView;
|
||||||
|
|
||||||
# Check to make sure it's not in the trash or some other weird place
|
# Check to make sure it's not in the trash or some other weird place
|
||||||
if ($self->get("state") ne "published") {
|
if ($self->state ne "published") {
|
||||||
my $i18n = WebGUI::International->new($session,'Asset_File');
|
my $i18n = WebGUI::International->new($session,'Asset_File');
|
||||||
$session->http->setStatus("404");
|
$session->http->setStatus("404");
|
||||||
return sprintf($i18n->get("file not found"), $self->getUrl());
|
return sprintf($i18n->get("file not found"), $self->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
$session->http->setRedirect($self->getFileUrl) unless $session->config->get('enableStreamingUploads');
|
$session->http->setRedirect($self->getFileUrl) unless $session->config->get('enableStreamingUploads');
|
||||||
$session->http->setStreamedFile($self->getStorageLocation->getPath($self->get("filename")));
|
$session->http->setStreamedFile($self->getStorageLocation->getPath($self->filename));
|
||||||
$session->http->sendHeader;
|
$session->http->sendHeader;
|
||||||
return 'chunked';
|
return 'chunked';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,11 @@ our $I18N = {
|
||||||
lastUpdated => 1184820764,
|
lastUpdated => 1184820764,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
'file template' => {
|
||||||
|
message => q|File Template|,
|
||||||
|
lastUpdated => 1264812976,
|
||||||
|
},
|
||||||
|
|
||||||
'fileSize' => {
|
'fileSize' => {
|
||||||
message => q|The size (in bytes/kilobytes/megabytes, etc) of the file.|,
|
message => q|The size (in bytes/kilobytes/megabytes, etc) of the file.|,
|
||||||
lastUpdated => 1148952092,
|
lastUpdated => 1148952092,
|
||||||
|
|
|
||||||
|
|
@ -65,15 +65,15 @@ my $asset = $defaultAsset->addChild($properties, $properties->{id});
|
||||||
############################################
|
############################################
|
||||||
|
|
||||||
ok($asset->getStorageLocation, 'File Asset getStorageLocation initialized');
|
ok($asset->getStorageLocation, 'File Asset getStorageLocation initialized');
|
||||||
ok($asset->get('storageId'), 'getStorageLocation updates asset object with storage location');
|
ok($asset->storageId, 'getStorageLocation updates asset object with storage location');
|
||||||
is($asset->get('storageId'), $asset->getStorageLocation->getId, 'Asset storageId and cached storageId agree');
|
is($asset->storageId, $asset->getStorageLocation->getId, 'Asset storageId and cached storageId agree');
|
||||||
|
|
||||||
$asset->update({
|
$asset->update({
|
||||||
storageId => $storage->getId,
|
storageId => $storage->getId,
|
||||||
filename => $filename,
|
filename => $filename,
|
||||||
});
|
});
|
||||||
|
|
||||||
is($storage->getId, $asset->get('storageId'), 'Asset updated with correct new storageId');
|
is($storage->getId, $asset->storageId, 'Asset updated with correct new storageId');
|
||||||
is($storage->getId, $asset->getStorageLocation->getId, 'Cached Asset storage location updated with correct new storageId');
|
is($storage->getId, $asset->getStorageLocation->getId, 'Cached Asset storage location updated with correct new storageId');
|
||||||
|
|
||||||
$versionTag->commit;
|
$versionTag->commit;
|
||||||
|
|
@ -87,6 +87,7 @@ $versionTag->commit;
|
||||||
my $fileStorage = WebGUI::Storage->create($session);
|
my $fileStorage = WebGUI::Storage->create($session);
|
||||||
my $guard2 = cleanupGuard($fileStorage);
|
my $guard2 = cleanupGuard($fileStorage);
|
||||||
$mocker->set_always('get', $fileStorage->getId);
|
$mocker->set_always('get', $fileStorage->getId);
|
||||||
|
$mocker->set_always('getValue', $fileStorage->getId);
|
||||||
my $fileFormStorage = $asset->getStorageFromPost();
|
my $fileFormStorage = $asset->getStorageFromPost();
|
||||||
isa_ok($fileFormStorage, 'WebGUI::Storage', 'Asset::File::getStorageFromPost');
|
isa_ok($fileFormStorage, 'WebGUI::Storage', 'Asset::File::getStorageFromPost');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue