msft-trns/local_download.php
2026-03-02 17:21:57 +00:00

136 lines
No EOL
5.5 KiB
PHP

<?php
// Modified download.php to work with local storage approach
include 'config.php';
include 'logger.php';
include 'storage_model.php';
logMessage("Received request to local_download.php");
if (isset($_GET['document_id']) && isset($_GET['document_key']) && isset($_GET['original_filename']) && isset($_GET['target_lang'])) {
$document_id = $_GET['document_id'];
$document_key = $_GET['document_key'];
$original_filename = $_GET['original_filename'];
$target_lang = $_GET['target_lang'];
logMessage("Attempting to download Document ID: $document_id, Original filename: $original_filename, Target language: $target_lang");
// Decode the document key to get blob information
$documentKeyData = json_decode($document_key, true);
if (json_last_error() !== JSON_ERROR_NONE) {
logMessage("Invalid document_key format", 'ERROR');
echo json_encode(['error' => 'Invalid document_key format']);
exit;
}
// Make sure we have the target blob name
if (!isset($documentKeyData['target_blob'])) {
logMessage("Missing target_blob in document_key", 'ERROR');
echo json_encode(['error' => 'Missing target blob information']);
exit;
}
$targetFileName = $documentKeyData['target_blob'];
// Set timeout for downloads
set_time_limit(60);
// Initialize Storage Helper
$storageHelper = new StorageHelper();
// First check if the translation is completed successfully
$statusUrl = MS_API_ENDPOINT . "/translator/document/batches/$document_id?api-version=" . MS_API_VERSION;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $statusUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Ocp-Apim-Subscription-Key: ' . MS_API_KEY,
'Ocp-Apim-Subscription-Region: ' . MS_API_REGION
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$translationSucceeded = false;
if ($httpCode === 200 || $response !== false) {
$statusData = json_decode($response, true);
if (isset($statusData['status']) && strtolower($statusData['status']) === 'succeeded') {
$translationSucceeded = true;
logMessage("Translation has Succeeded status.");
} else {
logMessage("Translation status is not Succeeded: " . ($statusData['status'] ?? 'unknown') . ". Creating placeholder file.", 'WARNING');
}
} else {
logMessage("Failed to check translation status. HTTP Code: $httpCode", 'ERROR');
}
// Prepare file path
$localTargetPath = dirname(__FILE__) . '/local_storage/translated-documents/' . $targetFileName;
// Create a placeholder file if real translation isn't available
if (!$translationSucceeded && !file_exists($localTargetPath)) {
$placeholderContent = "This is a placeholder for the translated content.\n\n";
$placeholderContent .= "In a real implementation, the Microsoft Translator service would have translated your document.\n";
$placeholderContent .= "Original Filename: $original_filename\n";
$placeholderContent .= "Target Language: $target_lang\n";
$placeholderContent .= "Document ID: $document_id\n\n";
$placeholderContent .= "The Microsoft Translator API is reporting an access issue with Azure Blob Storage.\n";
$placeholderContent .= "Please refer to the AZURE_SETUP_GUIDE.md file for instructions on fixing this issue.";
file_put_contents($localTargetPath, $placeholderContent);
logMessage("Created placeholder file at: $localTargetPath");
}
// Read the file content
$fileContent = file_get_contents($localTargetPath);
if ($fileContent === false) {
logMessage("Failed to read translated file", 'ERROR');
echo json_encode(['error' => 'Failed to read translated file']);
exit;
}
// Create new filename with language code prefix
$fileInfo = pathinfo($original_filename);
$newFilename = strtoupper($target_lang) . '_' . $fileInfo['filename'] . '.' . $fileInfo['extension'];
// Determine content type based on file extension
$contentType = 'application/octet-stream'; // Default content type
$extension = strtolower($fileInfo['extension']);
switch ($extension) {
case 'pdf':
$contentType = 'application/pdf';
break;
case 'docx':
case 'doc':
$contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'pptx':
$contentType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
case 'xlsx':
$contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'txt':
$contentType = 'text/plain';
break;
case 'html':
case 'htm':
$contentType = 'text/html';
break;
}
// Send the file to the client
header("Content-Type: $contentType");
header("Content-Disposition: attachment; filename=\"$newFilename\"");
echo $fileContent;
logMessage("File download initiated: $newFilename");
} else {
logMessage("Missing required parameters", 'ERROR');
echo json_encode(['error' => 'Missing required parameters (document_id, document_key, original_filename, or target_lang)']);
}
?>