voice2text/process.php
2025-11-03 11:08:48 -06:00

134 lines
No EOL
5.5 KiB
PHP
Executable file

<?php
// Load config first (which sets session ini settings before starting session)
require_once 'config.php';
// Require authentication (this will start the session if needed)
if (!isAuthenticated()) {
echo json_encode(['success' => false, 'error' => 'Authentication required. Please log in.']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['voiceFile'])) {
$file = $_FILES['voiceFile'];
$outputFormat = isset($_POST['outputFormat']) ? $_POST['outputFormat'] : 'txt';
$enableTranslation = isset($_POST['enableTranslation']) ? $_POST['enableTranslation'] : '0';
$targetLanguage = isset($_POST['targetLanguage']) ? $_POST['targetLanguage'] : 'EN-US';
// Check file size
if ($file['size'] > 350 * 1024 * 1024) { // 350 MB limit
echo json_encode(['success' => false, 'error' => "File is too large. Maximum size is 350 MB."]);
exit;
}
// Prepare the file for sending to Python API
$formData = [
'audio' => new CURLFile($file['tmp_name'], $file['type'], $file['name']),
'format' => $outputFormat,
'translate' => $enableTranslation,
'target_lang' => $targetLanguage
];
// Build API URL
$apiUrl = PYTHON_API_URL . '/transcribe';
// DEBUG: Log the exact URL and configuration
error_log("=== UPLOAD DEBUG START ===");
error_log("PYTHON_API_URL constant: " . PYTHON_API_URL);
error_log("Full API URL: " . $apiUrl);
error_log("File name: " . $file['name']);
error_log("File size: " . $file['size']);
error_log("Output format: " . $outputFormat);
// Send request to Python API
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 5 minutes timeout for large files
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
$effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
// DEBUG: Log response details
error_log("HTTP Code: " . $httpCode);
error_log("cURL Error: " . ($curlError ?: 'none'));
error_log("Effective URL: " . $effectiveUrl);
if ($httpCode === 301 || $httpCode === 302) {
$redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
error_log("REDIRECT DETECTED!");
error_log("Redirect Location: " . ($redirectUrl ?: 'not provided'));
}
error_log("Response preview: " . substr($response, 0, 500));
error_log("=== UPLOAD DEBUG END ===");
if (curl_errno($ch)) {
echo json_encode(['success' => false, 'error' => "Error processing file: " . curl_error($ch)]);
} else {
if ($httpCode === 200) {
$data = json_decode($response, true);
if (isset($data['success']) && $data['success']) {
// Track file ownership in session
if (!isset($_SESSION['user_files'])) {
$_SESSION['user_files'] = [];
}
// Add original file to user's file list
$_SESSION['user_files'][] = $data['filename'];
// Return content for display and download links
$downloadUrl = 'download.php?file=' . urlencode($data['filename']);
$response = [
'success' => true,
'fileUrl' => $downloadUrl,
'filename' => $data['filename'],
'format' => $outputFormat
];
// Add content for display (all formats)
if (isset($data['content'])) {
if ($outputFormat === 'txt') {
$response['response'] = nl2br(htmlspecialchars($data['content']));
} else {
// For VTT/SRT, format as preformatted text
$response['response'] = '<pre style="text-align: left; white-space: pre-wrap; word-wrap: break-word;">' . htmlspecialchars($data['content']) . '</pre>';
}
}
// Add translated content if available
if (isset($data['translated_filename'])) {
// Add translated file to user's file list
$_SESSION['user_files'][] = $data['translated_filename'];
$response['translatedFileUrl'] = 'download.php?file=' . urlencode($data['translated_filename']);
$response['translatedFilename'] = $data['translated_filename'];
if (isset($data['translated_content'])) {
if ($outputFormat === 'txt') {
$response['translatedResponse'] = nl2br(htmlspecialchars($data['translated_content']));
} else {
// For VTT/SRT, format as preformatted text
$response['translatedResponse'] = '<pre style="text-align: left; white-space: pre-wrap; word-wrap: break-word;">' . htmlspecialchars($data['translated_content']) . '</pre>';
}
}
}
echo json_encode($response);
} else {
echo json_encode(['success' => false, 'error' => $data['error'] ?? "Unknown error occurred"]);
}
} else {
echo json_encode(['success' => false, 'error' => "Server error: HTTP $httpCode"]);
}
}
curl_close($ch);
} else {
echo json_encode(['success' => false, 'error' => "Invalid request."]);
}