btg-sandbox-video-upscaler/process.php
DJP dd61900772 Initial commit: Video Enhancement Portal
- Complete PHP web application for video enhancement using Topaz Labs API
- Features include multiple AI models, resolution scaling, and real-time progress tracking
- Microsoft authentication integration with secure session management
- Comprehensive README with installation and configuration guide
- Security-focused .gitignore to protect sensitive configuration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-17 14:40:40 -04:00

123 lines
No EOL
4.3 KiB
PHP

<?php
require_once 'config.php';
require_once 'logger.php';
require_once 'api_client.php';
header('Content-Type: application/json');
session_start();
try {
Logger::log('Starting video processing');
if (!isset($_SESSION['video_request'])) {
Logger::log('Session data: ' . print_r($_SESSION, true));
throw new Exception('No video request found in session');
}
$videoRequest = $_SESSION['video_request'];
Logger::log('Video request data: ' . print_r($videoRequest, true));
if (!isset($videoRequest['requestId'])) {
throw new Exception('No request ID found in session data');
}
$requestId = $videoRequest['requestId'];
Logger::log("Using request ID: $requestId");
// Verify the file still exists
if (!file_exists($videoRequest['file']['tmp_name'])) {
throw new Exception('Upload file not found: ' . $videoRequest['file']['tmp_name']);
}
$apiClient = new TopazApiClient(Config::API_KEY);
// Accept the request with the same video info from estimation
Logger::log("Accepting request ID: $requestId");
$acceptResponse = $apiClient->acceptVideoRequest($requestId);
Logger::log('Accept response: ' . json_encode($acceptResponse));
if (!isset($acceptResponse['urls']) || !is_array($acceptResponse['urls'])) {
throw new Exception('Invalid upload URLs received from API');
}
$uploadUrls = $acceptResponse['urls'];
Logger::log("Received " . count($uploadUrls) . " upload URLs");
// Upload the file parts
$uploadResults = [];
$fileSize = filesize($videoRequest['file']['tmp_name']);
$partSize = ceil($fileSize / count($uploadUrls));
foreach ($uploadUrls as $index => $url) {
$partNum = $index + 1;
Logger::log("Uploading part $partNum");
$handle = fopen($videoRequest['file']['tmp_name'], 'rb');
if (!$handle) {
throw new Exception('Could not open file for reading');
}
// Seek to the correct position for this part
fseek($handle, $index * $partSize);
// Create curl handle
$ch = curl_init($url);
// Setup curl options
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_UPLOAD => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_INFILE => $handle,
CURLOPT_INFILESIZE => min($partSize, $fileSize - ($index * $partSize)),
CURLOPT_HEADER => true
]);
// Execute the request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
fclose($handle);
if ($httpCode !== 200) {
throw new Exception("Failed to upload part $partNum. HTTP Code: $httpCode");
}
// Extract ETag from response headers
if (preg_match('/ETag: "([^"]+)"/', $response, $matches)) {
$eTag = $matches[1];
$uploadResults[] = [
'partNum' => $partNum,
'eTag' => $eTag
];
Logger::log("Successfully uploaded part $partNum with ETag: $eTag");
} else {
throw new Exception("No ETag found in response for part $partNum");
}
curl_close($ch);
}
// Complete the upload
Logger::log("Completing upload with results: " . json_encode($uploadResults));
$completeResponse = $apiClient->completeUpload($requestId, $uploadResults);
Logger::log("Complete upload response: " . json_encode($completeResponse));
// Store request ID in session
$_SESSION['current_request_id'] = $requestId;
Logger::log("Processing started with request ID: $requestId");
$response = ['requestId' => $requestId, 'status' => 'success'];
Logger::log("Sending response: " . json_encode($response));
echo json_encode($response);
} catch (Exception $e) {
Logger::log('Error during processing: ' . $e->getMessage(), 'ERROR');
Logger::log('Error trace: ' . $e->getTraceAsString(), 'ERROR');
http_response_code(400);
$errorResponse = ['error' => $e->getMessage(), 'status' => 'error'];
Logger::log("Sending error response: " . json_encode($errorResponse));
echo json_encode($errorResponse);
}
?>