voice2text/process.php
DJP d78207b64c Fix VTT/SRT display and download functionality
- Display VTT/SRT content in formatted pre blocks on page
- Improved download button styling with better visibility
- Added content truncation for large files (10k char limit)
- Downloads now open in new tab for better UX
- Fixed session_start() duplicate warnings
- Added console logging for debugging
- Created check_api.php for API status monitoring
- Enhanced error messages with specific HTTP status codes
- API now returns full content for all formats
- Both original and translated files display inline

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 13:05:04 -04:00

90 lines
No EOL
3.8 KiB
PHP

<?php
session_start();
require_once 'config.php';
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
];
// Send request to Python API
$ch = curl_init(PYTHON_API_URL . '/transcribe');
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);
if (curl_errno($ch)) {
echo json_encode(['success' => false, 'error' => "Error processing file: " . curl_error($ch)]);
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
$data = json_decode($response, true);
if (isset($data['success']) && $data['success']) {
// 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'])) {
$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."]);
}