Rewrote all the command line utilities.

This commit is contained in:
JT Smith 2003-03-16 08:33:13 +00:00
parent 3ead160201
commit 505aa5809c
9 changed files with 505 additions and 349 deletions

View file

@ -1,3 +1,5 @@
#!/usr/bin/perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2003 Plain Black LLC.
#-------------------------------------------------------------------
@ -8,153 +10,172 @@
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
our ($webguiRoot, $webUser, @nailable);
our ($webguiRoot, @nailable);
BEGIN {
$webguiRoot = "..";
$webUser = "apache";
@nailable = qw(jpg jpeg png gif tif tiff bmp);
unshift (@INC, $webguiRoot."/lib");
}
#-----------------------------------------
# NO NEED TO MODIFY BELOW THIS LINE
$| = 1;
use File::stat;
use Image::Magick;
use DBI;
use Mysql;
use Data::Config;
use WebGUI::SQL;
use File::Copy qw(cp);
use File::Path;
use File::stat;
use FileHandle;
use Getopt::Long;
use Image::Magick;
use POSIX;
use strict;
use WebGUI::Attachment;
use WebGUI::DateTime;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Utility;
my ($filelist, $dbh, $settings, $config);
if ($ARGV[0] ne "" && $ARGV[1] ne ""){
print "Starting...\n";
$filelist = buildFileList($ARGV[0]);
$config = getConfig($ARGV[1]);
$dbh = connectToDb($config);
$settings = getSettings($dbh,$ARGV[3]);
addFiles($dbh, $filelist, $settings, $ARGV[0], $ARGV[2], $config);
setPrivileges($config) unless ($^O =~ /Win/i);
print "Cleaning up...\n";
$dbh->disconnect;
print "Finished!\n";
} else {
print "Usage: $0 <pathToFiles> <webguiConfigFile> <wobjectId> [<thumbnailSize>]\n";
my $configFile;
my $groupToView = 2;
my $help;
my $pathToFiles;
my $override;
my $quiet;
my $webUser = 'apache';
my $wobjectId;
GetOptions(
'configFile=s'=>\$configFile,
'groupToView=i'=>\$groupToView,
'help'=>\$help,
'override'=>$override,
'pathToFiles=s'=>\$pathToFiles,
'quiet'=>\$quiet,
'webUser=s'=>\$webUser,
'wobjectId=i'=>\$wobjectId
);
if ($help || $configFile eq "" || $pathToFiles eq "" || $wobjectId eq ""){
print <<STOP;
Usage: perl $0 --pathToFiles=<pathToImportFiles> --configfile=<webguiConfig> --wobjectId=<fileManagerWobjectId>
--configFile WebGUI config file.
--pathToFiles Folder containing files to import.
--wobjectId The wobject ID of the file manager you
wish to import these files to.
Options:
--groupToView The group ID of the group that should
have the privileges to view these
files. Defaults to '2'.
--help Display this help message and exit.
--override This utility is designed to be run as
a privileged user on Linux style systems.
If you wish to run this utility without
being the super user, then use this flag,
but note that it may not work as
intended.
--quiet Disable output unless there's an error.
--webUser The user that your web server runs as.
Defaults to 'apache'.
STOP
exit;
}
if (!($^O =~ /^Win/i) && $> != 0 && !$override) {
print "You must be the super user to use this utility.\n";
exit;
}
print "Starting..." unless ($quiet);
WebGUI::Session::open($webguiRoot,$ARGV[1]);
WebGUI::Session::refreshUserInfo(3);
print "OK\n" unless ($quiet);
addFiles(buildFileList($pathToFiles));
setPrivileges();
print "Cleaning up..." unless ($quiet);
WebGUI::Session::end($session{var}{sessionId});
WebGUI::Session::close();
print "OK\n" unless ($quiet);
#-----------------------------------------
# addFiles(dbHandler, filelistHashRef, webguiSettingsHashRef, pathToCopyFrom)
#-----------------------------------------
sub addFiles {
my ($exists, @files, $filename, $ext, $id, $i, $file1, $file2, $file3, $seq);
print "Adding files...\n";
($exists) = WebGUI::SQL->quickArray("select count(*) from FileManager where wobjectId='$_[4]'",$_[0]);
print "Adding files...\n" unless ($quiet);
($exists) = WebGUI::SQL->quickArray("select count(*) from FileManager where wobjectId='$wobjectId'");
if ($exists) {
mkdir($_[5]->{uploadsPath}."/".$_[4]);
foreach $filename (keys %{$_[1]}) {
print "Processing $filename.\n";
$id = getId($_[0]);
mkdir($_[5]->{uploadsPath}."/".$_[4]."/".$id);
($seq) = WebGUI::SQL->quickArray("select max(sequenceNumber) from FileManager_file where wobjectId='$_[4]'",$_[0]);
my $w = WebGUI::Wobject::FileManager->new({wobjectId=>$wobjectId,namespace=>"FileManager"});
foreach $filename (keys %{$_[0]}) {
print "Processing $filename.\n" unless ($quiet);
$i = 0;
@files = [];
foreach $ext (keys %{${$_[1]}{$filename}}) {
print "Copying ".${$_[1]}{$filename}{$ext}.".\n";
$a = FileHandle->new($_[3]."/".${$_[1]}{$filename}{$ext},"r");
binmode($a);
$b = FileHandle->new(">".$_[5]->{uploadsPath}."/".$_[4]."/".$id."/".${$_[1]}{$filename}{$ext});
binmode($b);
cp($a,$b);
$a->close;
$b->close;
createThumbnail(${$_[1]}{$filename}{$ext},$_[5]->{uploadsPath}."/".$_[4]."/".$id,$_[2]->{thumbnailSize});
$files[$i] = ${$_[1]}{$filename}{$ext};
print "\tAdding $filename to the database.\n" unless ($quiet);
my $fileId = $w->setCollateral("FileManager_file","FileManager_fileId",{
FileManager_fileId=>"new",
groupToView=>$groupToView,
dateUploaded=>time(),
fileTitle=>$filename
});
my $attachment = WebGUI::Attachment->new("new",$w->get("wobjectId"),$fileId);
foreach $ext (keys %{${$_[0]}{$filename}}) {
print "\tCopying ".${$_[0]}{$filename}{$ext}.".\n" unless ($quiet);
$attachment->saveFromFilesystem($pathToFiles.$session{os}{slash}.${$_[0]}{$filename}{$ext});
$files[$i] = ${$_[0]}{$filename}{$ext};
$i++;
}
my @files = sort {isIn(getType($b),@nailable) cmp isIn(getType($a),@nailable)} @files;
print "Adding $filename to the database.\n";
WebGUI::SQL->write("insert into FileManager_file (FileManager_fileId,wobjectId,fileTitle,downloadFile,
groupToView,dateUploaded,alternateVersion1,alternateVersion2,sequenceNumber) values (
$id,$_[4],'$filename','$files[0]',2,".time().",'$files[1]','$files[2]',".($seq+1).")",$_[0]);
$w->setCollateral("FileManager_file","FileManager_fileId",{
FileManager_fileId=>$fileId,
downloadFile=>$files[0],
alternateVersion1=>$files[1],
alternateVersion2=>$files[2]
});
}
} else {
print "Warning: File Manager '$_[4]' does not exist. Cannot import files.\n";
print "Warning: File Manager '".$wobjectId."' does not exist. Cannot import files.\n";
}
print "Finished adding.\n";
print "Finished adding.\n" unless ($quiet);
}
#-----------------------------------------
# setPrivileges(webguiSettingsHashRef)
# setPrivileges()
#-----------------------------------------
sub setPrivileges {
print "Setting filesystem privileges.\n";
system("chown -R ".$webUser." ".$_[0]->{uploadsPath});
print "Privileges set.\n";
}
#-----------------------------------------
# getSettings(dbHandler)
#-----------------------------------------
sub getSettings {
my (%settings);
print "Retrieving settings from WebGUI.\n";
%settings = WebGUI::SQL->buildHash("select * from settings",$_[0]);
print "Settings retrieved.\n";
$settings{thumbnailSize} = $_[1] if ($_[1] ne "");
return \%settings;
}
#-----------------------------------------
# getConfig(configFilename)
#-----------------------------------------
sub getConfig {
my ($config, $error, %config);
print "Getting site config.\n";
$config = new Data::Config $webguiRoot.'/etc/'.$_[0] or $error=1;
if ($error) {
print "Couldn't open config file.\n";
exit;
print "Setting filesystem privileges.\n" unless ($quiet);
if ($session{os}{type} = "Linuxish") {
unless (system("chown -R ".$webUser." ".$session{config}{uploadsPath})) {
print "Privileges set.\n" unless ($quiet);
} else {
print "Could not set privileges.\n";
}
} else {
foreach ($config->param) {
$config{$_} = $config->param($_);
}
print "Config retrieved.\n";
return \%config;
print "Cannot set privileges on this platform.\n" unless ($quiet)
}
}
#-----------------------------------------
# connectToDb()
#-----------------------------------------
sub connectToDb {
my ($dbh, $error);
print "Connecting to database ".${$_[0]}{dsn}." as user ".${$_[0]}{dbuser}.".\n";
$dbh = DBI->connect(${$_[0]}{dsn}, ${$_[0]}{dbuser}, ${$_[0]}{dbpass}, { RaiseError => 0, AutoCommit => 1 }) or $error=1;
unless ($error) {
print "Connection established.\n";
return $dbh;
} else {
print "Error: Could not connect to the database.\n";
exit;
}
}
#-----------------------------------------
# buildFileList(pathToImportFiles)
#-----------------------------------------
sub buildFileList {
print "Building file list.\n";
print "Building file list.\n" unless ($quiet);
my (%filelist, @files, $file, $filename, $ext);
if (opendir(FILES,$_[0])) {
@files = readdir(FILES);
@ -164,11 +185,11 @@ sub buildFileList {
$filename = $1;
$ext = $2;
$filelist{$filename}{$ext} = $file;
print "Found file $file.\n";
print "Found file $file.\n" unless ($quiet);
}
}
closedir(FILES);
print "File list complete.\n";
print "File list complete.\n" unless ($quiet);
return \%filelist;
} else {
print "Error: Could not open folder.\n";
@ -176,28 +197,6 @@ sub buildFileList {
}
}
#-----------------------------------------
# isIn(keyvalue, arrayOfValues)
#-----------------------------------------
sub isIn {
my ($i, @a, @b, @isect, %union, %isect, $e);
foreach $e (@_) {
if ($a[0] eq "") {
$a[0] = $e;
} else {
$b[$i] = $e;
$i++;
}
}
foreach $e (@a, @b) { $union{$e}++ && $isect{$e}++ }
@isect = keys %isect;
if (defined @isect) {
undef @isect;
return 1;
} else {
return 0;
}
}
#-----------------------------------------
# getType(filename)
@ -209,37 +208,6 @@ sub getType {
return $extension;
}
#-----------------------------------------
# createThumbnail(filename,path,thumnailSize)
#-----------------------------------------
sub createThumbnail {
my ($image, $x, $y, $r, $n, $type);
$type = getType($_[0]);
if (isIn($type, @nailable) && !($_[0] =~ m/thumb-/)) {
print "Nailing: $_[1]/$_[0]\n";
$image = Image::Magick->new;
$image->Read($_[1].'/'.$_[0]);
($x, $y) = $image->Get('width','height');
$n = $_[2] || 50;
$r = $x>$y ? $x / $n : $y / $n;
$image->Scale(width=>($x/$r),height=>($y/$r)) if ($r > 0);
if (isIn($type, qw(tif tiff bmp))) {
$image->Write($_[1].'/thumb-'.$_[0].'.png');
} else {
$image->Write($_[1].'/thumb-'.$_[0]);
}
}
}
#-----------------------------------------
# getId(dbHandler)
#-----------------------------------------
sub getId {
my ($id);
($id) = WebGUI::SQL->quickArray("select nextValue from incrementer where incrementerId='downloadId'",$_[0]);
WebGUI::SQL->write("update incrementer set nextValue=nextValue+1 where incrementerId='downloadId'",$_[0]);
return $id;
}