This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/utils/massupload/massupload.pl
2017-04-04 19:40:46 +10:00

88 lines
2.2 KiB
Perl
Executable File

#!/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 > /dev/null 2>&1");
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);
}
}
}