File size: 1,658 Bytes
1897d91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
// 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'])
]);