cmc-sales/php/app/vendors/pagecounter.php

54 lines
1.4 KiB
PHP
Executable file

<?php
/* Count pages of PDF attachments by calling Go service */
class PageCounter {
/**
* Count the number of pages in a PDF file by calling the Go service.
* Returns the page count or null if unable to determine.
*/
function count($file) {
if (!file_exists($file) || !is_readable($file)) {
return null;
}
try {
// Get the Go base URL from config
App::import('Controller', 'App');
$appController = new AppController();
$goBaseUrl = $appController::getGoBaseUrlOrFail();
$goEndpoint = $goBaseUrl . '/go/pdf/count-pages';
// Call the Go page counter endpoint
$payload = array('file_path' => $file);
$ch = curl_init($goEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 200 && $httpCode < 300) {
$result = json_decode($response, true);
if (isset($result['page_count']) && is_numeric($result['page_count'])) {
$count = (int)$result['page_count'];
return $count > 0 ? $count : null;
}
}
return null;
} catch (Exception $e) {
// If Go service fails, return null gracefully
return null;
}
}
}
?>