Mass uploader tool

This commit is contained in:
Andrew Pamment 2017-04-04 19:38:05 +10:00
parent 3dacaece8a
commit 89a9d5e2bf

88
utils/massupload/massupload.pl Executable file
View File

@ -0,0 +1,88 @@
#!/usr/bin/env perl
use DBI;
use strict;
if ($#ARGV < 1) {
print "Usage ./massupload.pl filedir database.sq3\n";
exit(0);
}
my $dir = $ARGV[0];
if (substr($dir, 1) != '/') {
print "Please use the full path of the directory you wish to scan.\n";
exit(0);
}
my $driver = "SQLite";
my $database = $ARGV[1];
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
my $stmt = qq(CREATE TABLE IF NOT EXISTS files (
Id INTEGER PRIMARY KEY,
filename TEXT,
description TEXT,
size INTEGER,
dlcount INTEGER,
approved INTEGER););
my $rv = $dbh->do($stmt);
my $string;
if($rv < 0){
print $DBI::errstr;
exit(0);
}
print "Scanning " . $dir . " ...\n";
foreach my $fp (glob("$dir/*")) {
if ( -f $fp) {
print ".. Found: " . $fp . " ... ";
$stmt = 'SELECT count(*) FROM files WHERE filename=$1;';
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute($fp) or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
if ($sth->fetch()->[0]) {
print " duplicate.\n";
} else {
if (uc(substr($fp, -3)) == "ZIP") {
mkdir("/tmp/massupload");
system("unzip -jCLL $fp file_id.diz -d /tmp/massupload");
if ( -f "/tmp/massupload/file_id.diz") {
print(" found description.\n");
local $/=undef;
open FILE, "/tmp/massupload/file_id.diz" or die "Couldn't open file: $!";
binmode FILE;
$string = <FILE>;
close FILE;
$string =~ s/\r//g;
unlink("/tmp/massupload/file_id.diz");
} else {
$string = "No Description.";
print(" no description.\n");
}
rmdir("/tmp/massupload");
} else {
$string = "No Description.";
print(" not zip file.\n");
}
my $size = -s $fp;
$sth = $dbh->prepare('INSERT INTO files (filename, description, size, dlcount, approved) VALUES($1, $2, $3, 0, 1)');
$sth->execute($fp, $string, $size);
}
}
}