Files
ferramentas/DocumentProcessor.php
T

52 lines
1.6 KiB
PHP

<?php
class DocumentProcessor {
private $pythonScript;
private $pythonPath;
public function __construct() {
$this->pythonScript = __DIR__ . '/document_detector.py';
$this->pythonPath = 'python'; // ou 'python3' dependendo do sistema
if (!file_exists($this->pythonScript)) {
throw new Exception('Script Python não encontrado');
}
}
public function processDocument(string $inputPath, string $outputPath): array {
if (!file_exists($inputPath)) {
throw new Exception('Arquivo de entrada não encontrado');
}
// Escapa argumentos para segurança
$inputPath = escapeshellarg($inputPath);
$outputPath = escapeshellarg($outputPath);
// Executa script Python
$command = sprintf('%s %s %s %s 2>&1',
$this->pythonPath,
escapeshellarg($this->pythonScript),
$inputPath,
$outputPath
);
$result = shell_exec($command);
if ($result === null) {
throw new Exception('Falha ao executar o script Python');
}
$response = json_decode($result, true);
if (!$response || !isset($response['success'])) {
throw new Exception('Resposta inválida do script Python: ' . $result);
}
if (!$response['success']) {
throw new Exception('Falha no processamento: ' . ($response['message'] ?? 'Erro desconhecido'));
}
return $response;
}
}