File size: 2,524 Bytes
e699ed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
$json_file = 'io.json';
$headers = getallheaders();
$auth_header = $headers['Authorization'] ?? '';
$input = file_get_contents('php://input');
$data = json_decode($input, true);
$method = $_SERVER['REQUEST_METHOD'];
$task_id = $_GET['task_id'] ?? null;

// Read storage
$storage = [];
if (file_exists($json_file)) {
    $storage = json_decode(file_get_contents($json_file), true) ?? [];
}

if ($method === 'POST' && !$task_id) {
    // CREATE NEW TASK
    $ch = curl_init('https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: ' . $auth_header
    ]);
    
    $response = curl_exec($ch);
    $result = json_decode($response, true);
    $new_task_id = $result['id'] ?? null;
    
    if ($new_task_id) {
        $storage[$new_task_id] = [
            'created' => time(),
            'status' => 'processing',
            'request' => $data,
            'response' => $result
        ];
        file_put_contents($json_file, json_encode($storage, JSON_PRETTY_PRINT));
    }
    
    echo $response;
    
} elseif ($method === 'GET' && $task_id) {
    // POLL EXISTING TASK - THIS UPDATES IO.JSON!
    $url = "https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks/{$task_id}";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: ' . $auth_header
    ]);
    
    $response = curl_exec($ch);
    $result = json_decode($response, true);
    
    // UPDATE STORAGE WITH LATEST STATUS
    if (isset($storage[$task_id])) {
        $storage[$task_id]['response'] = $result;
        $storage[$task_id]['status'] = $result['status'] ?? 'unknown';
        $storage[$task_id]['last_poll'] = time();
        
        if ($result['status'] === 'succeeded') {
            $storage[$task_id]['video_url'] = $result['content']['video_url'] ?? null;
        }
        
        file_put_contents($json_file, json_encode($storage, JSON_PRETTY_PRINT));
    }
    
    echo $response;
    
} elseif ($_SERVER['REQUEST_URI'] === '/proxy/io.json') {
    // DIRECT ACCESS TO JSON
    header('Content-Type: application/json');
    echo file_get_contents($json_file);
}

?>