From 5421f8e9d72a5be0a012c970390f0c7412002c04 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Tue, 30 Nov 2004 23:27:38 +0000 Subject: [PATCH] starting adding file asset --- docs/upgrades/upgrade_6.2.9-6.3.0.sql | 7 ++ lib/WebGUI/Asset.pm | 53 +++++---- lib/WebGUI/Asset/File.pm | 163 ++++++++++++++++++++++++++ lib/WebGUI/Asset/Redirect.pm | 2 +- lib/WebGUI/Asset/Wobject.pm | 2 +- lib/WebGUI/Storage.pm | 121 +++++++++++++++---- 6 files changed, 299 insertions(+), 49 deletions(-) create mode 100644 lib/WebGUI/Asset/File.pm diff --git a/docs/upgrades/upgrade_6.2.9-6.3.0.sql b/docs/upgrades/upgrade_6.2.9-6.3.0.sql index e2bbdba44..5039c6f1e 100644 --- a/docs/upgrades/upgrade_6.2.9-6.3.0.sql +++ b/docs/upgrades/upgrade_6.2.9-6.3.0.sql @@ -89,6 +89,13 @@ create table layout ( printableStyleTemplateId varchar(22) not null ); +create table FileAsset ( + assetId varchar(22) not null primary key, + storageId varchar(22) not null, + filename varchar(256) not null, + fileSize int not null +); + INSERT INTO settings VALUES ('commerceCheckoutCanceledTemplateId','1'); INSERT INTO settings VALUES ('commerceConfirmCheckoutTemplateId','1'); INSERT INTO settings VALUES ('commercePaymentPlugin','PayFlowPro'); diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index dfddfb4d3..b0db394fe 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -30,7 +30,7 @@ sub addChild { WebGUI::SQL->commit; my $className = $properties->{className}; my $newAsset = $className->new($id); - $newAsset->set($properties); + $newAsset->update($properties); return $newAsset; } @@ -503,30 +503,6 @@ sub purge { $self = undef; } -sub set { - my $self = shift; - my $properties = shift; - WebGUI::SQL->beginTransaction; - foreach my $definition (@{$self->definition}) { - my @setPairs; - foreach my $property (keys %{$definition->{properties}}) { - my $value = $properties->{$property} || $definition->{properties}{$property}{defaultValue}; - if (defined $value) { - if (exists $definition->{properties}{$property}{filter}) { - $value = $self->$definition->{properties}{$property}{filter}($value); - } - $self->{_properties}{$property} = $value; - push(@setPairs, $property."=".quote($value)); - } - } - if (scalar(@setPairs) > 0) { - WebGUI::SQL->write("update ".$definition->{tableName}." set ".join(",",@setPairs)." where assetId=".quote($self->getId)); - } - } - WebGUI::SQL->commit; - return 1; -} - sub setParent { my $self = shift; my $newParentId = shift; @@ -586,6 +562,31 @@ sub swapRank { } +sub update { + my $self = shift; + my $properties = shift; + WebGUI::SQL->beginTransaction; + foreach my $definition (@{$self->definition}) { + my @setPairs; + foreach my $property (keys %{$definition->{properties}}) { + my $value = $properties->{$property} || $definition->{properties}{$property}{defaultValue}; + if (defined $value) { + if (exists $definition->{properties}{$property}{filter}) { + $value = $self->$definition->{properties}{$property}{filter}($value); + } + $self->{_properties}{$property} = $value; + push(@setPairs, $property."=".quote($value)); + } + } + if (scalar(@setPairs) > 0) { + WebGUI::SQL->write("update ".$definition->{tableName}." set ".join(",",@setPairs)." where assetId=".quote($self->getId)); + } + } + WebGUI::SQL->commit; + return 1; +} + + sub www_copy { my $self = shift; return WebGUI::Privilege::insufficient() unless $self->canEdit; @@ -633,7 +634,7 @@ sub www_editSave { ); } } - $self->set(\%data); + $self->update(\%data); return ""; } diff --git a/lib/WebGUI/Asset/File.pm b/lib/WebGUI/Asset/File.pm new file mode 100644 index 000000000..4364819da --- /dev/null +++ b/lib/WebGUI/Asset/File.pm @@ -0,0 +1,163 @@ +package WebGUI::Asset::File; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2004 Plain Black Corporation. + ------------------------------------------------------------------- + Please read the legal notices (docs/legal.txt) and the license + (docs/license.txt) that came with this distribution before using + this software. + ------------------------------------------------------------------- + http://www.plainblack.com info@plainblack.com + ------------------------------------------------------------------- + +=cut + +use strict; +use WebGUI::Asset; +use WebGUI::HTTP; +use WebGUI::Session; +use WebGUI::Storage; + +our @ISA = qw(WebGUI::Asset); + + +=head1 NAME + +Package WebGUI::Asset::File + +=head1 DESCRIPTION + +Provides a mechanism to upload files to WebGUI. + +=head1 SYNOPSIS + +use WebGUI::Asset::File; + + +=head1 METHODS + +These methods are available from this class: + +=cut + + + +#------------------------------------------------------------------- + +=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 $definition = shift; + push(@{$definition}, { + tableName=>'FileAsset', + className=>'WebGUI::Asset::File', + properties=>{ + filename=>{ + fieldType=>'hidden', + defaultValue=>undef + }, + storageId=>{ + fieldType=>'hidden', + defaultValue=>undef + }, + fileSize=>{ + fieldType=>'hidden', + defaultValue=>undef + } + } + }); + return $class->SUPER::definition($definition); +} + + + +#------------------------------------------------------------------- + +=head2 getEditForm () + +Returns the TabForm object that will be used in generating the edit page for this asset. + +=cut + +sub getEditForm { + my $self = shift; + my $tabform = $self->SUPER::getEditForm(); + if ($self->get("filename") ne "") { + my $storage = WebGUI::Storage->new($self->get("storageId")); + + } + $tabform->getTab("properties")->url( + -name=>"file", + -label=>"File To Upload" + ); +} + + +#------------------------------------------------------------------- + +=head2 getName + +Returns the displayable name of this asset. + +=cut + +sub getName { + return "File"; +} + + +#------------------------------------------------------------------- + +=head2 www_editSave + +Gathers data from www_edit and persists it. + +=cut + +sub www_editSave { + my $self = shift; + $self->SUPER::www_editSave(); + my $storage = WebGUI::Storage->create; + my $filename = $storage->addFileFromFormPost("file"); + if (defined $filename) { + $self->update({ + filename=>$filename, + storageId=>$storage->getId, + fileSize=>$storage->getFileSize + }); + return ""; +} + + +#------------------------------------------------------------------- + +=head2 www_view + +A web executable method that redirects the user to the specified page, or displays the edit interface when admin mode is enabled. + +=cut + +sub www_view { + my $self = shift; + if ($session{var}{adminOn}) { + return $self->www_edit; + } + my $storage = WebGUI::Storage->new($self->get("storageId")); + WebGUI::HTTP::setRedirect($storage->getUrl($self->get("filename"))); + return ""; +} + + +1; + diff --git a/lib/WebGUI/Asset/Redirect.pm b/lib/WebGUI/Asset/Redirect.pm index 27eaea33a..ee3e9f193 100644 --- a/lib/WebGUI/Asset/Redirect.pm +++ b/lib/WebGUI/Asset/Redirect.pm @@ -106,7 +106,7 @@ sub getUiLevel { #------------------------------------------------------------------- -=head2 name +=head2 getName Returns the displayable name of this asset. diff --git a/lib/WebGUI/Asset/Wobject.pm b/lib/WebGUI/Asset/Wobject.pm index ed8d26ef1..807f1b988 100644 --- a/lib/WebGUI/Asset/Wobject.pm +++ b/lib/WebGUI/Asset/Wobject.pm @@ -313,7 +313,7 @@ sub www_createShortcut { my $self = shift; return WebGUI::Privilege::insufficient() unless ($self->canEdit); my $w = WebGUI::Wobject::WobjectProxy->new({wobjectId=>"new",namespace=>"WobjectProxy"}); - $w->set({ + $w->update({ pageId=>'2', templatePosition=>1, title=>$self->getValue("title"), diff --git a/lib/WebGUI/Storage.pm b/lib/WebGUI/Storage.pm index a44a1d1ba..fa0c87b54 100644 --- a/lib/WebGUI/Storage.pm +++ b/lib/WebGUI/Storage.pm @@ -39,6 +39,7 @@ This package provides a mechanism for storing and retrieving files that are not $store = WebGUI::Storage->get($id); $filename = $store->addFileFromFilesystem($pathToFile); + $filename = $store->addFileFromFormPost($formVarName); $filename = $store->addFileFromHashref($filename,$hashref); $filename = $store->addFileFromScalar($filename,$content); @@ -46,6 +47,7 @@ This package provides a mechanism for storing and retrieving files that are not $hashref = $store->getFileContentsAsHashref($filename); $string = $store->getFileContentsAsScalar($filename); $string = $store->getFileExtension($filename); + $url = $store->getFileIconUrl($filename); $arrayref = $store->getFiles; $string = $store->getFileSize($filename); $guid = $store->getId; @@ -120,7 +122,7 @@ Provide the local path to this file. =cut -sub saveFromFilesystem { +sub addFileFromFilesystem { my $self = shift; my $pathToFile = shift; my $filename; @@ -163,6 +165,54 @@ sub saveFromFilesystem { } +#------------------------------------------------------------------- + +=head2 addFileFromFormPost ( formVariableName ) + +Grabs an attachment from a form POST and saves it to this storage location. + +=head3 formVariableName + +Provide the form variable name to which the file being uploaded is assigned. + +=cut + +sub addFileFromFormPost{ + my $self = shift; + my $formVariableName = shift; + return "" if (WebGUI::HTTP::getStatus() =~ /^413/); + my $filename = $session{cgi}->upload($formVariableName); + if (defined $filename) { + if ($filename =~ /([^\/\\]+)$/) { + $filename = $1; + } else { + $filename = $filename; + } + my $type = $self->getFileExtension($filename); + if (isIn($type, qw(pl perl sh cgi php asp))) { # make us safe from malicious uploads + $filename =~ s/\./\_/g; + $filename .= ".txt"; + } + $filename = WebGUI::URL::makeCompliant($filename); + my $file = FileHandle->new(">".$self->getPath($filename)); + if (defined $file) { + my $buffer; + binmode $file; + while (my $bytesread=read($filename,$buffer,1024)) { + print $file $buffer; + } + close($file); + } else { + $self->_addError("Couldn't open file ".$self->getPath($filename)." for writing due to error: ".$!); + return undef; + } + close $filename; + return $filename; + } + return undef; +} + + #------------------------------------------------------------------- =head2 addFileFromHashref ( filename, hashref ) @@ -368,6 +418,50 @@ sub getFileContentsAsScalar { } +#------------------------------------------------------------------- + +=head2 getFileExtension ( filename ) + +Returns the extension or type of this file. + +=head3 filename + +The filename of the file you wish to find out the type for. + +=cut + +sub getFileExtension { + my $filename = shift; + my $extension = lc($filename); + $extension =~ s/.*\.(.*?)$/$1/; + return $extension; +} + + +#------------------------------------------------------------------- + +=head2 getFileIconUrl ( filename ) + +Returns the icon associated with this type of file. + +=head3 filename + +The name of the file to get the icon for. + +=cut + +sub getFileIconUrl { + my $self = shift; + my $filename = shift; + my $extension = $self->getFileExtension($filename); + my $path = $session{config}{extrasPath}.$session{os}{slash}."fileIcons".$session{os}{slash}.$extension.".gif"; + if (-f $path) { + return $session{config}{extrasURL}."/fileIcons/".$extension.".gif"; + } + return $session{config}{extrasURL}."/fileIcons/unkonwn.gif"; +} + + #------------------------------------------------------------------- =head2 getFileSize ( filename ) @@ -393,6 +487,7 @@ sub getFileSize { return $size; } + #------------------------------------------------------------------- =head2 getFiles ( ) @@ -401,7 +496,7 @@ Returns an array reference of the files in this storage location. =cut -sub getFiles ( ) { +sub getFiles { my $self = shift; my @list; if (opendir (DIR,$self->getPath)) { @@ -414,28 +509,10 @@ sub getFiles ( ) { } return \@list; } - return []; + return undef; } -#------------------------------------------------------------------- - -=head2 getFileExtension ( filename ) - -Returns the extension or type of this file. - -=head3 filename - -The filename of the file you wish to find out the type for. - -=cut - -sub getFileExtension { - my $filename = shift; - my $extension = lc($filename); - $extension =~ s/.*\.(.*?)$/$1/; - return $extension; -} #------------------------------------------------------------------- @@ -455,6 +532,8 @@ sub getHashref { return retrieve($self->getPath($filename)); } + + #------------------------------------------------------------------- =head2 getId ()