Readabalize code. Will fix bugs later today.
This commit is contained in:
parent
9cacb28c69
commit
a2b5a14ce3
1 changed files with 192 additions and 155 deletions
|
|
@ -178,18 +178,21 @@ 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);
|
||||||
} else {
|
} else {
|
||||||
print "Warning: Parent asset '".$parentAssetId."' does not exist. Cannot import files.\n";
|
print "Warning: Parent asset '".$parentAssetId."' does not exist. Cannot import files.\n";
|
||||||
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;
|
||||||
|
|
||||||
|
|
@ -204,138 +208,160 @@ exit 0;
|
||||||
# addFiles(dbHandler, filelistHashRef, webguiSettingsHashRef, pathToCopyFrom)
|
# addFiles(dbHandler, filelistHashRef, webguiSettingsHashRef, pathToCopyFrom)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub addFiles {
|
sub addFiles {
|
||||||
my $filelist = shift;
|
my $filelist = shift;
|
||||||
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.
|
|
||||||
print "\tAdding ".$file->{relpath}.$slash.$file->{filename}." to the database.\n" unless ($quiet);
|
|
||||||
my $templateId = 'PBtmpl0000000000000024';
|
|
||||||
if (isIn(lc($file->{ext}),@nailable)) {
|
|
||||||
$class = 'WebGUI::Asset::File::Image';
|
|
||||||
$templateId = 'PBtmpl0000000000000088'
|
|
||||||
}
|
|
||||||
my $url = $parent->getUrl.'/'.$file->{filename};
|
|
||||||
|
|
||||||
# TB : Possibly detect if the Asset exists already.
|
# TB : add the path relative to $pathToFile in the message.
|
||||||
my ($replaceAsset, $replaceAssetId, $child) = (0,"","");
|
print "\tAdding ".$file->{relpath}.$slash.$file->{filename}." to the database.\n" unless ($quiet);
|
||||||
($replaceAsset, $replaceAssetId, $child) = &checkAssetExists($url) if ($overwrite);
|
|
||||||
|
|
||||||
if ($replaceAsset == 1) {
|
my $templateId = 'PBtmpl0000000000000024';
|
||||||
# TB : If the Asset exists, just copy the file.
|
if (isIn(lc($file->{ext}),@nailable)) {
|
||||||
# To be check.
|
$class = 'WebGUI::Asset::File::Image';
|
||||||
my $storage = WebGUI::Storage->get($session, $replaceAssetId);
|
$templateId = 'PBtmpl0000000000000088'
|
||||||
print "\t\tAsset exists already. Replace the file.\n" unless ($quiet);
|
}
|
||||||
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
|
my $url = $parent->getUrl.'/'.$file->{filename};
|
||||||
$child->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
|
|
||||||
$child->setSize($storage->getFileSize($filename));
|
# TB : Possibly detect if the Asset exists already.
|
||||||
} else {
|
my ($replaceAsset, $replaceAssetId, $child) = (0,"","");
|
||||||
print "\t\tCreate the new asset.\n" unless ($quiet);
|
($replaceAsset, $replaceAssetId, $child) = &checkAssetExists($url) if ($overwrite);
|
||||||
my $storage = WebGUI::Storage->create($session);
|
|
||||||
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
|
if ($replaceAsset == 1) {
|
||||||
# TB : possibly remove the extension if the ignoreExtInName feature enabled.
|
# TB : If the Asset exists, just copy the file.
|
||||||
my $filenameTitle = $filename;
|
# To be check.
|
||||||
$filenameTitle =~ s/\.$file->{ext}// if ($ignoreExtInName);
|
my $storage = WebGUI::Storage->get($session, $replaceAssetId);
|
||||||
my $newAsset = $parent->addChild({
|
print "\t\tAsset exists already. Replace the file.\n" unless ($quiet);
|
||||||
className=>$class,
|
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
|
||||||
title=>$filenameTitle,
|
$child->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
|
||||||
menuTitle=>$filenameTitle,
|
$child->setSize($storage->getFileSize($filename));
|
||||||
filename=>$filename,
|
}
|
||||||
storageId=>$storage->getId,
|
else {
|
||||||
isHidden=>1,
|
print "\t\tCreate the new asset.\n" unless ($quiet);
|
||||||
url=>$url,
|
my $storage = WebGUI::Storage->create($session);
|
||||||
groupIdView=>$groupToView,
|
my $filename = $storage->addFileFromFilesystem("$pathToFiles$slash$file->{relpath}$slash$file->{filename}");
|
||||||
groupIdEdit=>$groupToEdit,
|
|
||||||
templateId=>$templateId,
|
# TB : possibly remove the extension if the ignoreExtInName feature enabled.
|
||||||
endDate=>32472169200,
|
my $filenameTitle = $filename;
|
||||||
ownerUserId=>$owner
|
$filenameTitle =~ s/\.$file->{ext}// if ($ignoreExtInName);
|
||||||
});
|
|
||||||
$newAsset->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
|
my $newAsset = $parent->addChild({
|
||||||
$newAsset->setSize($storage->getFileSize($filename));
|
className => $class,
|
||||||
}
|
title => $filenameTitle,
|
||||||
}
|
menuTitle => $filenameTitle,
|
||||||
print "Finished adding.\n" unless ($quiet);
|
filename => $filename,
|
||||||
|
storageId => $storage->getId,
|
||||||
|
isHidden => 1,
|
||||||
|
url => $url,
|
||||||
|
groupIdView => $groupToView,
|
||||||
|
groupIdEdit => $groupToEdit,
|
||||||
|
templateId => $templateId,
|
||||||
|
endDate => 32472169200,
|
||||||
|
ownerUserId => $owner,
|
||||||
|
});
|
||||||
|
|
||||||
|
$newAsset->generateThumbnail if ($class eq 'WebGUI::Asset::File::Image');
|
||||||
|
$newAsset->setSize($storage->getFileSize($filename));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print "Finished adding.\n" unless ($quiet);
|
||||||
}
|
}
|
||||||
|
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
# setPrivileges()
|
# setPrivileges()
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
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") {
|
|
||||||
unless (system("chown -R ".$webUser." ".$session->config->get("uploadsPath"))) {
|
if ($session->os->get("type") eq "Linuxish") {
|
||||||
print "Privileges set.\n" unless ($quiet);
|
unless (system("chown -R ".$webUser." ".$session->config->get("uploadsPath"))) {
|
||||||
} else {
|
print "Privileges set.\n" unless ($quiet);
|
||||||
print "Could not set privileges.\n";
|
}
|
||||||
}
|
else {
|
||||||
} else {
|
print "Could not set privileges.\n";
|
||||||
print "Cannot set privileges on this platform.\n" unless ($quiet)
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
print "Cannot set privileges on this platform.\n" unless ($quiet)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
# buildFileListWrap(pathToImportFiles)
|
# buildFileListWrap(pathToImportFiles)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub buildFileListWrap {
|
sub buildFileListWrap {
|
||||||
my ($path) = @_;
|
my ($path) = @_;
|
||||||
my (@filelist);
|
my (@filelist);
|
||||||
print "Building file list.\n" unless ($quiet);
|
|
||||||
@filelist = &buildFileList($now,$path);
|
print "Building file list.\n" unless ($quiet);
|
||||||
# &filelistCheck(@filelist);
|
@filelist = &buildFileList($now,$path);
|
||||||
print "File list complete.\n" unless ($quiet);
|
# &filelistCheck(@filelist);
|
||||||
return \@filelist;
|
print "File list complete.\n" unless ($quiet);
|
||||||
|
|
||||||
|
return \@filelist;
|
||||||
}
|
}
|
||||||
|
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
# buildFileList(time,pathToImportFiles)
|
# buildFileList(time,pathToImportFiles)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
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)) {
|
|
||||||
@files = readdir(FILES);
|
if (opendir(FILES,$path)) {
|
||||||
foreach $file (@files) {
|
@files = readdir(FILES);
|
||||||
next if ($file eq "." || $file eq "..");
|
|
||||||
my $fullpathfile = "$path$slash$file";
|
foreach $file (@files) {
|
||||||
if (-f "$fullpathfile") {
|
next if ($file eq "." || $file eq "..");
|
||||||
$file =~ /(.*?)\.(.*?)$/;
|
my $fullpathfile = "$path$slash$file";
|
||||||
$filename = $1;
|
if (-f "$fullpathfile") {
|
||||||
$ext = $2;
|
$file =~ /(.*?)\.(.*?)$/;
|
||||||
# TB : filter process : skip files due to options : skipOlderThan and findByExt
|
$filename = $1;
|
||||||
next if (&skipFilter($fullpathfile,$ext,$now));
|
$ext = $2;
|
||||||
# TB : due to recursive search. Not really nice.
|
|
||||||
my $relpath = &trailFile("$path");
|
# TB : filter process : skip files due to options : skipOlderThan and findByExt
|
||||||
# TB : check is the filelist doesn't contains two times the same file (file with the same name)
|
next if (&skipFilter($fullpathfile,$ext,$now));
|
||||||
# due to recursive call, this can happen.
|
|
||||||
if (exists $filelisthash{$file}) {
|
# TB : due to recursive search. Not really nice.
|
||||||
print "Error: file \"$file\" exists at several locations.
|
my $relpath = &trailFile("$path");
|
||||||
Both \"$filelisthash{$file}\" and \"$relpath\" contains it.
|
|
||||||
Exit at the first error of this type.\n" unless ($quiet);
|
# TB : check is the filelist doesn't contains two times the same file (file with the same name)
|
||||||
exit 2;
|
# due to recursive call, this can happen.
|
||||||
|
if (exists $filelisthash{$file}) {
|
||||||
|
|
||||||
|
print "Error: file \"$file\" exists at several locations.
|
||||||
|
Both \"$filelisthash{$file}\" and \"$relpath\" contains it.
|
||||||
|
Exit at the first error of this type.\n" unless ($quiet);
|
||||||
|
exit 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
push(@filelist,{ext=>$ext, filename=>$file, relpath=>$relpath});
|
||||||
|
$filelisthash{$file} = $relpath;
|
||||||
|
print "Found file $file.\n" unless ($quiet);
|
||||||
|
}
|
||||||
|
# TB : the recursive call
|
||||||
|
push(@filelist,&buildFileList($now,"$fullpathfile")) if ((-d "$fullpathfile") && $recursive);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(FILES);
|
||||||
|
return @filelist;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print "Error: Could not open folder $path.\n" unless ($quiet);
|
||||||
|
exit 2;
|
||||||
}
|
}
|
||||||
push(@filelist,{ext=>$ext, filename=>$file, relpath=>$relpath});
|
|
||||||
$filelisthash{$file} = $relpath;
|
|
||||||
print "Found file $file.\n" unless ($quiet);
|
|
||||||
}
|
|
||||||
# TB : the recursive call
|
|
||||||
push(@filelist,&buildFileList($now,"$fullpathfile")) if ((-d "$fullpathfile") && $recursive);
|
|
||||||
}
|
|
||||||
closedir(FILES);
|
|
||||||
return @filelist;
|
|
||||||
} else {
|
|
||||||
print "Error: Could not open folder $path.\n" unless ($quiet);
|
|
||||||
exit 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
# getType(filename)
|
# getType(filename)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub getType {
|
sub getType {
|
||||||
my ($extension);
|
my ($extension);
|
||||||
$extension = $_[0];
|
$extension = $_[0];
|
||||||
$extension =~ s/.*\.(.*?)$/$1/;
|
$extension =~ s/.*\.(.*?)$/$1/;
|
||||||
return $extension;
|
|
||||||
|
return $extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -343,22 +369,26 @@ sub getType {
|
||||||
# skipFilter(file,ext,time)
|
# skipFilter(file,ext,time)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub skipFilter {
|
sub skipFilter {
|
||||||
my ($file,$ext,$now) = @_;
|
my ($file,$ext,$now) = @_;
|
||||||
# TB : stat in Windows has a strange behaviour relativelly to Unix
|
|
||||||
# the output of stat si an array of array.
|
# TB : stat in Windows has a strange behaviour relativelly to Unix
|
||||||
# to be check on Unix if this work correctly.
|
# the output of stat si an array of array.
|
||||||
my @dev = stat "$file";
|
# to be check on Unix if this work correctly.
|
||||||
# TB : option skipOlderThan
|
my @dev = stat "$file";
|
||||||
if ($now - $dev[0][9] > $skipOlderThan) {
|
|
||||||
print "Found file $file.\n\tBut older than $skipOlderThan. Skip it.\n" unless ($quiet);
|
# TB : option skipOlderThan
|
||||||
return 1;
|
if ($now - $dev[0][9] > $skipOlderThan) {
|
||||||
}
|
print "Found file $file.\n\tBut older than $skipOlderThan. Skip it.\n" unless ($quiet);
|
||||||
# TB : option findByExt
|
return 1;
|
||||||
if (($findByExt ne "") && ($findByExt !~ /(^|,)$ext(,|$)/)) {
|
}
|
||||||
print "Found file in $file.\n\tBut Extension doesn't match findByExt tag. Skip it.\n" unless ($quiet);
|
|
||||||
return 1;
|
# TB : option findByExt
|
||||||
}
|
if (($findByExt ne "") && ($findByExt !~ /(^|,)$ext(,|$)/)) {
|
||||||
return 0;
|
print "Found file in $file.\n\tBut Extension doesn't match findByExt tag. Skip it.\n" unless ($quiet);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -366,19 +396,23 @@ sub skipFilter {
|
||||||
# trailFile(path)
|
# trailFile(path)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub trailFile {
|
sub trailFile {
|
||||||
# TB : this is ugly, but unable to find a better way to do it.
|
# TB : this is ugly, but unable to find a better way to do it.
|
||||||
# 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/) {
|
|
||||||
$pathToFiles_temp =~ s/\\/\\\\/g;
|
if ($^O =~ /Win/) {
|
||||||
} else {
|
$pathToFiles_temp =~ s/\\/\\\\/g;
|
||||||
$pathToFiles_temp =~ s/\//\\\//g;
|
}
|
||||||
}
|
else {
|
||||||
$path =~ s/$pathToFiles_temp//;
|
$pathToFiles_temp =~ s/\//\\\//g;
|
||||||
$path =~ s/.//;
|
}
|
||||||
$path = "." if ($path eq "");
|
|
||||||
return $path;
|
$path =~ s/$pathToFiles_temp//;
|
||||||
|
$path =~ s/.//;
|
||||||
|
$path = "." if ($path eq "");
|
||||||
|
|
||||||
|
return $path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -386,16 +420,18 @@ sub trailFile {
|
||||||
# checkAssetExists(self,url)
|
# checkAssetExists(self,url)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub checkAssetExists {
|
sub checkAssetExists {
|
||||||
my ($url) = @_;
|
my ($url) = @_;
|
||||||
my $replaceAsset = 0;
|
my $replaceAsset = 0;
|
||||||
my $replaceAssetId = "";
|
my $replaceAssetId = "";
|
||||||
my $child;
|
my $child;
|
||||||
if (exists $ListAssetExists{$url}) {
|
|
||||||
$child = $ListAssetExists{$url};
|
if (exists $ListAssetExists{$url}) {
|
||||||
$replaceAsset = 1;
|
$child = $ListAssetExists{$url};
|
||||||
$replaceAssetId = $child->getId;
|
$replaceAsset = 1;
|
||||||
}
|
$replaceAssetId = $child->getId;
|
||||||
return($replaceAsset, $replaceAssetId, $child);
|
}
|
||||||
|
|
||||||
|
return($replaceAsset, $replaceAssetId, $child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -403,13 +439,14 @@ sub checkAssetExists {
|
||||||
# buildListAssetExists(self,url)
|
# buildListAssetExists(self,url)
|
||||||
#-----------------------------------------
|
#-----------------------------------------
|
||||||
sub buildListAssetExists {
|
sub buildListAssetExists {
|
||||||
my ($parent) = @_;
|
my ($parent) = @_;
|
||||||
my @allelement = $parent->getLineage();
|
my @allelement = $parent->getLineage();
|
||||||
foreach my $elem ( @{$allelement[0]} ) {
|
|
||||||
my $child = WebGUI::Asset::File->newByDynamicClass($session,$elem);
|
foreach my $elem ( @{$allelement[0]} ) {
|
||||||
if (defined $child) {
|
my $child = WebGUI::Asset::File->newByDynamicClass($session,$elem);
|
||||||
$ListAssetExists{$child->getUrl} = $child;
|
if (defined $child) {
|
||||||
}
|
$ListAssetExists{$child->getUrl} = $child;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue