Spaces:
Paused
Paused
| // status.php — Poll job status for async YouTube downloads | |
| require_once __DIR__ . '/config.php'; | |
| ensure_dirs(); | |
| $id = $_GET['id'] ?? ''; | |
| if (!$id || !preg_match('/^[a-f0-9]{16}$/', $id)) { | |
| fail_json('Invalid id'); | |
| } | |
| $jobPath = RECEIPT_DIR . "/$id.job.json"; | |
| $job = read_json_file($jobPath); | |
| if (!$job) { | |
| fail_json('Job not found', 404); | |
| } | |
| $uploadPath = $job['upload_path'] ?? ''; | |
| $logFile = RECEIPT_DIR . "/$id.download.log"; | |
| // Check if download completed | |
| if ($job['status'] === 'downloading') { | |
| if (file_exists($uploadPath) && filesize($uploadPath) > 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']) | |
| ]); | |