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

231 lines
No EOL
9.1 KiB
PHP

<?php
// This is a drop-in replacement for azure_storage.php that uses local storage only
include_once 'config.php';
include_once 'logger.php';
/**
* Storage Helper Class for Document Translation
*
* This class provides helper functions to work with local file storage
* as a replacement for Azure Blob Storage.
*/
class StorageHelper {
private $sourceContainer;
private $targetContainer;
private $basePath;
/**
* Constructor
*/
public function __construct() {
$this->sourceContainer = AZURE_STORAGE_CONTAINER_SOURCE;
$this->targetContainer = AZURE_STORAGE_CONTAINER_TARGET;
$this->basePath = dirname(__FILE__) . '/local_storage';
// Ensure containers exist
$this->ensureContainersExist();
}
/**
* Upload a file to the source container in local storage
*
* @param string $filePath Path to the temporary file
* @param string $fileName Name of the file
* @return array|bool Returns array with file path on success, false on failure
*/
public function uploadSourceFile($filePath, $fileName) {
logMessage("Uploading file to local storage: $fileName from $filePath");
try {
// Create a unique file name to avoid collisions
$safeFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $fileName);
$uniqueId = uniqid();
$storageFileName = $uniqueId . '-' . $safeFileName;
logMessage("Generated storage filename: $storageFileName");
// Verify file exists and is readable
if (!file_exists($filePath)) {
logMessage("Error: File does not exist at path: $filePath", 'ERROR');
return false;
}
if (!is_readable($filePath)) {
logMessage("Error: File is not readable at path: $filePath", 'ERROR');
return false;
}
$fileSize = filesize($filePath);
logMessage("File size: $fileSize bytes");
if ($fileSize <= 0) {
logMessage("Error: File is empty or could not determine file size", 'ERROR');
return false;
}
// Save the file to local storage
$localStorageDir = $this->basePath . '/' . $this->sourceContainer;
if (!file_exists($localStorageDir)) {
if (!mkdir($localStorageDir, 0777, true)) {
logMessage("Failed to create local storage directory: $localStorageDir", 'ERROR');
return false;
}
}
$localFilePath = $localStorageDir . '/' . $storageFileName;
if (!copy($filePath, $localFilePath)) {
logMessage("Failed to copy file to local storage: $localFilePath", 'ERROR');
return false;
}
logMessage("File saved to local storage: $localFilePath");
// Generate URLs for Microsoft Translator API access
// Note: Microsoft Translator must be able to reach these URLs
$baseUrl = $this->getBaseUrl();
$sourceUrl = $baseUrl . "/local_storage/{$this->sourceContainer}/$storageFileName";
logMessage("Generated source URL: $sourceUrl");
return [
'blob_url' => $baseUrl . "/local_storage/{$this->sourceContainer}",
'blob_name' => $storageFileName,
'file_url' => $sourceUrl
];
} catch (Exception $e) {
logMessage("Error processing file: " . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 'ERROR');
return false;
}
}
/**
* Get the target info for translation result
*
* @param string $fileName Original file name
* @param string $targetLang Target language
* @return array|bool Returns array with file info on success, false on failure
*/
public function getTargetInfo($fileName, $targetLang) {
logMessage("Setting up target file for: $fileName, language: $targetLang");
try {
// Create a unique file name for the translated file
$safeFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $fileName);
$uniqueId = uniqid();
$storageFileName = $uniqueId . '-' . strtoupper($targetLang) . '_' . $safeFileName;
logMessage("Generated target filename: $storageFileName");
// Ensure local storage directory exists
$localStorageDir = $this->basePath . '/' . $this->targetContainer;
if (!file_exists($localStorageDir)) {
if (!mkdir($localStorageDir, 0777, true)) {
logMessage("Failed to create local target directory: $localStorageDir", 'ERROR');
return false;
}
logMessage("Created local target directory: $localStorageDir");
}
// Generate URLs for Microsoft Translator API access
$baseUrl = $this->getBaseUrl();
$targetUrl = $baseUrl . "/local_storage/{$this->targetContainer}";
logMessage("Target directory URL: $targetUrl");
logMessage("Using target filename: $storageFileName");
return [
'blob_url' => $targetUrl,
'blob_name' => $storageFileName
];
} catch (Exception $e) {
logMessage("Error setting up target file: " . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 'ERROR');
return false;
}
}
/**
* Download a translated file from the target container
*
* @param string $fileName Name of the file
* @return string|bool Returns file content on success, false on failure
*/
public function downloadTranslatedFile($fileName) {
logMessage("Retrieving translated file: $fileName");
try {
// Check if the translated file exists in local storage
$localTargetPath = $this->basePath . '/' . $this->targetContainer . '/' . $fileName;
if (file_exists($localTargetPath)) {
logMessage("Found local translated file at: $localTargetPath");
$fileContent = file_get_contents($localTargetPath);
if ($fileContent === false) {
logMessage("Error reading local translated file: $localTargetPath", 'ERROR');
return false;
} else {
logMessage("Local translated file read successfully");
return $fileContent;
}
} else {
logMessage("Translated file not found: $localTargetPath", 'ERROR');
return false;
}
} catch (Exception $e) {
logMessage("Error in download process: " . $e->getMessage() . "\nStack trace: " . $e->getTraceAsString(), 'ERROR');
return false;
}
}
/**
* Ensure the container directories exist
*
* @return bool Returns true if containers exist or were created, false on failure
*/
public function ensureContainersExist() {
logMessage("Ensuring storage directories exist: {$this->sourceContainer} and {$this->targetContainer}");
try {
// Create source container directory if it doesn't exist
$sourceDir = $this->basePath . '/' . $this->sourceContainer;
if (!file_exists($sourceDir)) {
if (!mkdir($sourceDir, 0777, true)) {
logMessage("Failed to create source directory: $sourceDir", 'ERROR');
return false;
}
logMessage("Created source directory: $sourceDir");
}
// Create target container directory if it doesn't exist
$targetDir = $this->basePath . '/' . $this->targetContainer;
if (!file_exists($targetDir)) {
if (!mkdir($targetDir, 0777, true)) {
logMessage("Failed to create target directory: $targetDir", 'ERROR');
return false;
}
logMessage("Created target directory: $targetDir");
}
logMessage("Storage directories verified successfully");
return true;
} catch (Exception $e) {
logMessage("Error creating storage directories: " . $e->getMessage(), 'ERROR');
return false;
}
}
/**
* Get the base URL for the application
*
* @return string The base URL
*/
private function getBaseUrl() {
// Determine the base URL from the server variables
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$uri = rtrim(dirname($_SERVER['REQUEST_URI'] ?? ''), '/');
return "$protocol://$host$uri";
}
}
?>