voice2text/process.php

107 lines
No EOL
4.5 KiB
PHP

<?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
];
// 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']) {
// 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."]);
}