From 639c29c2c4753f86e2a6916a83a182b4bb4148a8 Mon Sep 17 00:00:00 2001 From: Ryan Shpeherd Date: Mon, 14 Sep 2026 16:02:21 -0400 Subject: [PATCH] Fix scanner --- scanner/scanner.pl | 88 +++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/scanner/scanner.pl b/scanner/scanner.pl index a32ed9a..fd7a673 100755 --- a/scanner/scanner.pl +++ b/scanner/scanner.pl @@ -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,29 +36,36 @@ my $row = $sth->fetchrow_hashref(); my $now = $row->{now}; $sth->finish(); -while(my $dir = shift @dir_queue) { - opendir(my $dh, $dir) or die "Cannot open directory $dir: $!"; - print "$dir\n"; - while (my $file = readdir($dh)) { - next if ($file eq '.' || $file eq '..'); - my $full_path = "$dir/$file"; - if (-d $full_path) { - unshift @dir_queue, $full_path; - } elsif (-f $full_path) { - process_file($full_path); +if($do_scan) { + while(my $dir = shift @dir_queue) { + opendir(my $dh, $dir) or die "Cannot open directory $dir: $!"; + print "$dir\n"; + while (my $file = readdir($dh)) { + next if ($file eq '.' || $file eq '..'); + my $full_path = "$dir/$file"; + if (-d $full_path) { + unshift @dir_queue, $full_path; + } elsif (-f $full_path) { + process_file($full_path); + } } + closedir($dh); + } + for my $ext (sort { $ignored_extensions{$b} <=> $ignored_extensions{$a} } keys %ignored_extensions) { + print "Ignored extension $ext: $ignored_extensions{$ext} times\n"; } - 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); } -CreateReviewTasks(); +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}); + + # 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(); - $sth_update->finish(); } \ No newline at end of file