65 lines
2 KiB
PHP
Executable file
65 lines
2 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) {
|
|
error_log("PageCounter: START - Attempting to count pages for file: $file");
|
|
|
|
if (!file_exists($file) || !is_readable($file)) {
|
|
error_log("PageCounter: File does not exist or is not readable: $file");
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
error_log("PageCounter: File exists and is readable, calling getGoBaseUrlOrFail()");
|
|
// Get the Go base URL from config
|
|
App::import('Controller', 'App');
|
|
$appController = new AppController();
|
|
$goBaseUrl = $appController::getGoBaseUrlOrFail();
|
|
$goEndpoint = $goBaseUrl . '/go/api/pdf/count-pages';
|
|
|
|
error_log("PageCounter: Calling Go endpoint: $goEndpoint with file: $file");
|
|
|
|
// 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);
|
|
|
|
error_log("PageCounter: Got response with HTTP code $httpCode: $response");
|
|
|
|
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'];
|
|
error_log("PageCounter: Returning page count: $count");
|
|
return $count > 0 ? $count : null;
|
|
}
|
|
}
|
|
|
|
error_log("PageCounter: Failed to get valid page count from response");
|
|
return null;
|
|
} catch (Exception $e) {
|
|
// If Go service fails, return null gracefully
|
|
error_log("PageCounter: Exception: " . $e->getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|