1000) { // Download complete — update job $job['status'] = 'downloaded'; // Try to get title from log $log = file_exists($logFile) ? file_get_contents($logFile) : ''; if (preg_match('/DONE\|(.+)/', $log, $m)) { $job['original_name'] = trim($m[1]) . '.mp4'; } write_json_file($jobPath, $job); } else { // Check log for errors $log = file_exists($logFile) ? file_get_contents($logFile) : ''; if (strpos($log, 'ERROR') !== false || strpos($log, 'error') !== false) { // Still might be downloading, check age $age = time() - ($job['created_at'] ?? time()); if ($age > 300) { $job['status'] = 'download_failed'; $job['error'] = 'Download timeout'; write_json_file($jobPath, $job); } } } } json_response([ 'ok' => true, 'id' => $id, 'status' => $job['status'], 'job' => $job, 'ready' => in_array($job['status'], ['downloaded', 'uploaded', 'extracted']) ]);