From 89a9d5e2bfcfa85698571e6bc4cdf0f222a3cb47 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Tue, 4 Apr 2017 19:38:05 +1000 Subject: [PATCH] Mass uploader tool --- utils/massupload/massupload.pl | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 utils/massupload/massupload.pl diff --git a/utils/massupload/massupload.pl b/utils/massupload/massupload.pl new file mode 100755 index 0000000..c49da0d --- /dev/null +++ b/utils/massupload/massupload.pl @@ -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 = ; + 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); + } + } +} \ No newline at end of file