Readabalize code. Will fix bugs later today.

This commit is contained in:
Martin Kamerbeek 2007-02-26 10:16:56 +00:00
parent 9cacb28c69
commit a2b5a14ce3

View file

@ -178,10 +178,12 @@ if (!($^O =~ /^Win/i) && $> != 0 && !$override) {
my %ListAssetExists; my %ListAssetExists;
my %filelisthash; my %filelisthash;
print "Starting..." unless ($quiet); print "Starting..." unless ($quiet);
my $session = WebGUI::Session->open($webguiRoot,$configFile); my $session = WebGUI::Session->open($webguiRoot,$configFile);
$session->user({userId=>3}); $session->user({userId=>3});
print "OK\n" unless ($quiet); print "OK\n" unless ($quiet);
my $parent = WebGUI::Asset::File->newByDynamicClass($session, $parentAssetId); my $parent = WebGUI::Asset::File->newByDynamicClass($session, $parentAssetId);
if (defined $parent) { if (defined $parent) {
&buildListAssetExists($parent); &buildListAssetExists($parent);
@ -190,6 +192,7 @@ if (defined $parent) {
exit 5; exit 5;
} }
print "End of the childs detection\n" unless ($quiet); print "End of the childs detection\n" unless ($quiet);
# TB : wrap buildFileList in buildFileListWrap function for recursive search. # TB : wrap buildFileList in buildFileListWrap function for recursive search.
&addFiles(&buildFileListWrap($pathToFiles)); &addFiles(&buildFileListWrap($pathToFiles));
&setPrivileges(); &setPrivileges();
@ -197,6 +200,7 @@ print "End of the childs detection\n" unless ($quiet);
print "Cleaning up..." unless ($quiet); print "Cleaning up..." unless ($quiet);
$session->var->end(); $session->var->end();
$session->close(); $session->close();
print "OK\n" unless ($quiet); print "OK\n" unless ($quiet);
exit 0; exit 0;
@ -208,8 +212,10 @@ sub addFiles {
print "Adding files...\n" unless ($quiet); print "Adding files...\n" unless ($quiet);
foreach my $file (@{$filelist}) { foreach my $file (@{$filelist}) {
my $class = 'WebGUI::Asset::File'; my $class = 'WebGUI::Asset::File';
# TB : add the path relative to $pathToFile in the message. # TB : add the path relative to $pathToFile in the message.
print "\tAdding ".$file->{relpath}.$slash.$file->{filename}." to the database.\n" unless ($quiet); print "\tAdding ".$file->{relpath}.$slash.$file->{filename}." to the database.\n" unless ($quiet);
my $templateId = 'PBtmpl0000000000000024'; my $templateId = 'PBtmpl0000000000000024';
if (isIn(lc($file->{ext}),@nailable)) { if (isIn(lc($file->{ext}),@nailable)) {
$class = 'WebGUI::Asset::File::Image'; $class = 'WebGUI::Asset::File::Image';
@ -229,13 +235,16 @@ sub addFiles {
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}"); my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
$child->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image'); $child->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
$child->setSize($storage->getFileSize($filename)); $child->setSize($storage->getFileSize($filename));
} else { }
else {
print "\t\tCreate the new asset.\n" unless ($quiet); print "\t\tCreate the new asset.\n" unless ($quiet);
my $storage = WebGUI::Storage->create($session); my $storage = WebGUI::Storage->create($session);
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}"); my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
# TB : possibly remove the extension if the ignoreExtInName feature enabled. # TB : possibly remove the extension if the ignoreExtInName feature enabled.
my $filenameTitle = $filename; my $filenameTitle = $filename;
$filenameTitle =~ s/\.$file->{ext}// if ($ignoreExtInName); $filenameTitle =~ s/\.$file->{ext}// if ($ignoreExtInName);
my $newAsset = $parent->addChild({ my $newAsset = $parent->addChild({
className => $class, className => $class,
title => $filenameTitle, title => $filenameTitle,
@ -248,12 +257,14 @@ sub addFiles {
groupIdEdit => $groupToEdit, groupIdEdit => $groupToEdit,
templateId => $templateId, templateId => $templateId,
endDate => 32472169200, endDate => 32472169200,
ownerUserId=>$owner ownerUserId => $owner,
}); });
$newAsset->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image'); $newAsset->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
$newAsset->setSize($storage->getFileSize($filename)); $newAsset->setSize($storage->getFileSize($filename));
} }
} }
print "Finished adding.\n" unless ($quiet); print "Finished adding.\n" unless ($quiet);
} }
@ -262,13 +273,16 @@ sub addFiles {
#----------------------------------------- #-----------------------------------------
sub setPrivileges { sub setPrivileges {
print "Setting filesystem privileges.\n" unless ($quiet); print "Setting filesystem privileges.\n" unless ($quiet);
if ($session->os->get("type") eq "Linuxish") { if ($session->os->get("type") eq "Linuxish") {
unless (system("chown -R ".$webUser." ".$session->config->get("uploadsPath"))) { unless (system("chown -R ".$webUser." ".$session->config->get("uploadsPath"))) {
print "Privileges set.\n" unless ($quiet); print "Privileges set.\n" unless ($quiet);
} else { }
else {
print "Could not set privileges.\n"; print "Could not set privileges.\n";
} }
} else { }
else {
print "Cannot set privileges on this platform.\n" unless ($quiet) print "Cannot set privileges on this platform.\n" unless ($quiet)
} }
} }
@ -279,10 +293,12 @@ sub setPrivileges {
sub buildFileListWrap { sub buildFileListWrap {
my ($path) = @_; my ($path) = @_;
my (@filelist); my (@filelist);
print "Building file list.\n" unless ($quiet); print "Building file list.\n" unless ($quiet);
@filelist = &buildFileList($now,$path); @filelist = &buildFileList($now,$path);
# &filelistCheck(@filelist); # &filelistCheck(@filelist);
print "File list complete.\n" unless ($quiet); print "File list complete.\n" unless ($quiet);
return \@filelist; return \@filelist;
} }
@ -292,8 +308,10 @@ sub buildFileListWrap {
sub buildFileList { sub buildFileList {
my ($now,$path) = @_; my ($now,$path) = @_;
my (@filelist, @files, $file, $filename, $ext); my (@filelist, @files, $file, $filename, $ext);
if (opendir(FILES,$path)) { if (opendir(FILES,$path)) {
@files = readdir(FILES); @files = readdir(FILES);
foreach $file (@files) { foreach $file (@files) {
next if ($file eq "." || $file eq ".."); next if ($file eq "." || $file eq "..");
my $fullpathfile = "$path$slash$file"; my $fullpathfile = "$path$slash$file";
@ -301,18 +319,23 @@ sub buildFileList {
$file =~ /(.*?)\.(.*?)$/; $file =~ /(.*?)\.(.*?)$/;
$filename = $1; $filename = $1;
$ext = $2; $ext = $2;
# TB : filter process : skip files due to options : skipOlderThan and findByExt # TB : filter process : skip files due to options : skipOlderThan and findByExt
next if (&skipFilter($fullpathfile,$ext,$now)); next if (&skipFilter($fullpathfile,$ext,$now));
# TB : due to recursive search. Not really nice. # TB : due to recursive search. Not really nice.
my $relpath = &trailFile("$path"); my $relpath = &trailFile("$path");
# TB : check is the filelist doesn't contains two times the same file (file with the same name) # TB : check is the filelist doesn't contains two times the same file (file with the same name)
# due to recursive call, this can happen. # due to recursive call, this can happen.
if (exists $filelisthash{$file}) { if (exists $filelisthash{$file}) {
print "Error: file \"$file\" exists at several locations. print "Error: file \"$file\" exists at several locations.
Both \"$filelisthash{$file}\" and \"$relpath\" contains it. Both \"$filelisthash{$file}\" and \"$relpath\" contains it.
Exit at the first error of this type.\n" unless ($quiet); Exit at the first error of this type.\n" unless ($quiet);
exit 2; exit 2;
} }
push(@filelist,{ext=>$ext, filename=>$file, relpath=>$relpath}); push(@filelist,{ext=>$ext, filename=>$file, relpath=>$relpath});
$filelisthash{$file} = $relpath; $filelisthash{$file} = $relpath;
print "Found file $file.\n" unless ($quiet); print "Found file $file.\n" unless ($quiet);
@ -320,9 +343,11 @@ sub buildFileList {
# TB : the recursive call # TB : the recursive call
push(@filelist,&buildFileList($now,"$fullpathfile")) if ((-d "$fullpathfile") && $recursive); push(@filelist,&buildFileList($now,"$fullpathfile")) if ((-d "$fullpathfile") && $recursive);
} }
closedir(FILES); closedir(FILES);
return @filelist; return @filelist;
} else { }
else {
print "Error: Could not open folder $path.\n" unless ($quiet); print "Error: Could not open folder $path.\n" unless ($quiet);
exit 2; exit 2;
} }
@ -335,6 +360,7 @@ sub getType {
my ($extension); my ($extension);
$extension = $_[0]; $extension = $_[0];
$extension =~ s/.*\.(.*?)$/$1/; $extension =~ s/.*\.(.*?)$/$1/;
return $extension; return $extension;
} }
@ -344,20 +370,24 @@ sub getType {
#----------------------------------------- #-----------------------------------------
sub skipFilter { sub skipFilter {
my ($file,$ext,$now) = @_; my ($file,$ext,$now) = @_;
# TB : stat in Windows has a strange behaviour relativelly to Unix # TB : stat in Windows has a strange behaviour relativelly to Unix
# the output of stat si an array of array. # the output of stat si an array of array.
# to be check on Unix if this work correctly. # to be check on Unix if this work correctly.
my @dev = stat "$file"; my @dev = stat "$file";
# TB : option skipOlderThan # TB : option skipOlderThan
if ($now - $dev[0][9] > $skipOlderThan) { if ($now - $dev[0][9] > $skipOlderThan) {
print "Found file $file.\n\tBut older than $skipOlderThan. Skip it.\n" unless ($quiet); print "Found file $file.\n\tBut older than $skipOlderThan. Skip it.\n" unless ($quiet);
return 1; return 1;
} }
# TB : option findByExt # TB : option findByExt
if (($findByExt ne "") && ($findByExt !~ /(^|,)$ext(,|$)/)) { if (($findByExt ne "") && ($findByExt !~ /(^|,)$ext(,|$)/)) {
print "Found file in $file.\n\tBut Extension doesn't match findByExt tag. Skip it.\n" unless ($quiet); print "Found file in $file.\n\tBut Extension doesn't match findByExt tag. Skip it.\n" unless ($quiet);
return 1; return 1;
} }
return 0; return 0;
} }
@ -370,14 +400,18 @@ sub trailFile {
# this is supposed to find the relative path to pathToFiles # this is supposed to find the relative path to pathToFiles
my ($path) = @_; my ($path) = @_;
my $pathToFiles_temp = $pathToFiles; my $pathToFiles_temp = $pathToFiles;
if ($^O =~ /Win/) { if ($^O =~ /Win/) {
$pathToFiles_temp =~ s/\\/\\\\/g; $pathToFiles_temp =~ s/\\/\\\\/g;
} else { }
else {
$pathToFiles_temp =~ s/\//\\\//g; $pathToFiles_temp =~ s/\//\\\//g;
} }
$path =~ s/$pathToFiles_temp//; $path =~ s/$pathToFiles_temp//;
$path =~ s/.//; $path =~ s/.//;
$path = "." if ($path eq ""); $path = "." if ($path eq "");
return $path; return $path;
} }
@ -390,11 +424,13 @@ sub checkAssetExists {
my $replaceAsset = 0; my $replaceAsset = 0;
my $replaceAssetId = ""; my $replaceAssetId = "";
my $child; my $child;
if (exists $ListAssetExists{$url}) { if (exists $ListAssetExists{$url}) {
$child = $ListAssetExists{$url}; $child = $ListAssetExists{$url};
$replaceAsset = 1; $replaceAsset = 1;
$replaceAssetId = $child->getId; $replaceAssetId = $child->getId;
} }
return($replaceAsset, $replaceAssetId, $child); return($replaceAsset, $replaceAssetId, $child);
} }
@ -405,6 +441,7 @@ sub checkAssetExists {
sub buildListAssetExists { sub buildListAssetExists {
my ($parent) = @_; my ($parent) = @_;
my @allelement = $parent->getLineage(); my @allelement = $parent->getLineage();
foreach my $elem ( @{$allelement[0]} ) { foreach my $elem ( @{$allelement[0]} ) {
my $child = WebGUI::Asset::File->newByDynamicClass($session,$elem); my $child = WebGUI::Asset::File->newByDynamicClass($session,$elem);
if (defined $child) { if (defined $child) {