File size: 1,454 Bytes
37ac66f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
require_once __DIR__ . '/config.php';
ensure_dirs();

$id = $_POST['id'] ?? $_GET['id'] ?? null;
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);
}

$video = $job['upload_path'];
if (!file_exists($video)) {
    fail_json('Uploaded video missing');
}

$outDir = RECEIPT_DIR . "/$id";
if (!is_dir($outDir)) {
    mkdir($outDir, 0775, true);
}

$cmd = BIN_EXTRACT . ' extract ' . shell_arg($video) . ' ' . shell_arg($outDir) . ' 2>&1';
$output = [];
$code = 0;
exec($cmd, $output, $code);

$receiptPath = "$outDir/receipt.json";
$receipt = read_json_file($receiptPath);

// Inject videoId and videoName into receipt
if ($receipt) {
    $receipt['videoId'] = $id;
    $receipt['videoName'] = $job['original_name'];
    write_json_file($receiptPath, $receipt);
}

$job['status'] = $code === 0 ? 'extracted' : 'extract_failed';
$job['extract_exit_code'] = $code;
$job['extract_log'] = implode("\n", array_slice($output, -80));
$job['receipt_path'] = $receiptPath;
$job['updated_at'] = time();
write_json_file($jobPath, $job);

if ($code !== 0 || !$receipt) {
    fail_json('Extraction failed', 500, [
        'exit_code' => $code,
        'log' => $job['extract_log']
    ]);
}

json_response([
    'ok' => true,
    'id' => $id,
    'receipt' => $receipt,
    'log' => $job['extract_log']
]);