Fix scanner

This commit is contained in:
Ryan Shpeherd
2026-09-14 16:02:21 -04:00
parent 3eb5b5b08b
commit 639c29c2c4
+44 -20
View File
@@ -9,12 +9,20 @@ use String::ShellQuote qw/shell_quote/;
my $db_host = $ENV{'DB_HOST'} || 'mariadb';
my $db_name = $ENV{'DB_NAME'} || 'videodetect';
my $db_user = $ENV{'DB_USER'} || 'videodetect';
my $db_pass = $ENV{'DB_PASSWORD'} || 'videodetect123';
my $db_pass = $ENV{'DB_PASSWORD'} || 'videodetect';
my $do_scan = (defined $ENV{'DO_SCAN'}) ? $ENV{'DO_SCAN'} : 1;
my $do_review = (defined $ENV{'DO_REVIEW'}) ? $ENV{'DO_REVIEW'} : 1;
my $do_delete = (defined $ENV{'DO_DELETE'}) ? $ENV{'DO_DELETE'} : 1;
$SIG{INT} = sub { die "Interrupted\n"; };
my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass);
my $dbh = DBI->connect(
"DBI:mysql:database=$db_name;host=$db_host;mysql_enable_utf8=1",
$db_user,
$db_pass,
{ mysql_auto_reconnect => 1 }
);
my @video_extensions = qw(mp4 mkv avi mov flv wmv mpg mpeg webm m4v);
my %ignored_extensions;
@@ -28,6 +36,7 @@ my $row = $sth->fetchrow_hashref();
my $now = $row->{now};
$sth->finish();
if($do_scan) {
while(my $dir = shift @dir_queue) {
opendir(my $dh, $dir) or die "Cannot open directory $dir: $!";
print "$dir\n";
@@ -42,15 +51,21 @@ while(my $dir = shift @dir_queue) {
}
closedir($dh);
}
# Any entry where last_scan_time < $now is considered deleted or moved, so we can mark them as such
my $sth_delete = $dbh->prepare("DELETE FROM videos WHERE last_scan_time < ? OR last_scan_time IS NULL");
$sth_delete->execute($now);
for my $ext (sort { $ignored_extensions{$b} <=> $ignored_extensions{$a} } keys %ignored_extensions) {
print "Ignored extension $ext: $ignored_extensions{$ext} times\n";
}
}
if($do_scan && $do_delete) {
# Any entry where last_scan_time < $now is considered deleted or moved, so we can mark them as such
my $sth_delete = $dbh->prepare("DELETE FROM videos WHERE last_scan_time < ? OR last_scan_time IS NULL");
$sth_delete->execute($now);
}
if($do_review) {
print "Scanning complete. Creating review tasks for completed AISCAN tasks.\n";
CreateReviewTasks();
}
sub process_file {
my ($file_path) = @_;
@@ -177,25 +192,34 @@ sub get_video_info {
}
sub CreateReviewTasks {
# Fetch all completed AISCAN tasks and filter in Perl.
# This avoids MariaDB's inconsistent JSON functions in SQL WHERE clauses,
# which behave differently between CLI client and DBI connections.
my $sth = $dbh->prepare("
SELECT *
SELECT id, video_id, results
FROM tasks
WHERE task_type='AISCAN'
AND status='COMPLETED'
AND JSON_VALUE(results, '$.verdict')=1
AND JSON_VALUE(results, '$.reviewed') IS NULL
");
my $sth_update = $dbh->prepare("
UPDATE tasks
SET results = JSON_SET(results, '$.reviewed', 1)
WHERE id = ?
WHERE task_type='AISCAN' AND status='COMPLETED'
");
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
while (my $row = $sth->fetchrow_hashref()) {
my $results = decode_json($row->{results});
next unless $results && ref($results) eq 'HASH';
next unless $results->{verdict} == 1;
next if exists $results->{reviewed};
print "Creating REVIEW task for video_id: $row->{video_id}\n";
create_task($row->{video_id}, 'REVIEW');
$sth_update->execute($row->{id});
}
$sth->finish();
# Re-encode the whole JSON blob in Perl and update it.
# This avoids MariaDB JSON_SET path parsing issues through DBI connections.
$results->{reviewed} = 1;
my $updated = encode_json($results);
my $sth_update = $dbh->prepare(
"UPDATE tasks SET results = ? WHERE id = ?"
);
$sth_update->execute($updated, $row->{id});
$sth_update->finish();
}
$sth->finish();
}