2013-03-28 23:58:00 -07:00
|
|
|
<?php
|
|
|
|
|
|
2026-01-12 03:13:32 -08:00
|
|
|
/* Count pages of PDF attachments by calling Go service */
|
2013-03-28 23:58:00 -07:00
|
|
|
|
2026-01-12 03:13:32 -08:00
|
|
|
class PageCounter {
|
2013-03-28 23:58:00 -07:00
|
|
|
|
2026-01-12 03:13:32 -08:00
|
|
|
/**
|
|
|
|
|
* Count the number of pages in a PDF file by calling the Go service.
|
|
|
|
|
* Returns the page count or null if unable to determine.
|
|
|
|
|
*/
|
2013-03-28 23:58:00 -07:00
|
|
|
function count($file) {
|
2026-01-18 22:52:57 -08:00
|
|
|
error_log("PageCounter: START - Attempting to count pages for file: $file");
|
2026-01-16 20:41:24 -08:00
|
|
|
|
2026-01-12 03:13:32 -08:00
|
|
|
if (!file_exists($file) || !is_readable($file)) {
|
2026-01-16 20:41:24 -08:00
|
|
|
error_log("PageCounter: File does not exist or is not readable: $file");
|
2026-01-12 03:13:32 -08:00
|
|
|
return null;
|
2013-03-28 23:58:00 -07:00
|
|
|
}
|
2026-01-12 03:13:32 -08:00
|
|
|
|
|
|
|
|
try {
|
2026-01-18 22:52:57 -08:00
|
|
|
error_log("PageCounter: File exists and is readable, calling getGoBaseUrlOrFail()");
|
2026-01-12 03:13:32 -08:00
|
|
|
// Get the Go base URL from config
|
|
|
|
|
App::import('Controller', 'App');
|
|
|
|
|
$appController = new AppController();
|
|
|
|
|
$goBaseUrl = $appController::getGoBaseUrlOrFail();
|
2026-01-21 05:14:27 -08:00
|
|
|
$goEndpoint = $goBaseUrl . '/go/document/page-count';
|
2026-01-16 20:41:24 -08:00
|
|
|
|
|
|
|
|
error_log("PageCounter: Calling Go endpoint: $goEndpoint with file: $file");
|
2026-01-12 03:13:32 -08:00
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
2026-01-16 20:41:24 -08:00
|
|
|
error_log("PageCounter: Got response with HTTP code $httpCode: $response");
|
|
|
|
|
|
2026-01-12 03:13:32 -08:00
|
|
|
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'];
|
2026-01-16 20:41:24 -08:00
|
|
|
error_log("PageCounter: Returning page count: $count");
|
2026-01-12 03:13:32 -08:00
|
|
|
return $count > 0 ? $count : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 20:41:24 -08:00
|
|
|
error_log("PageCounter: Failed to get valid page count from response");
|
2026-01-12 03:13:32 -08:00
|
|
|
return null;
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
// If Go service fails, return null gracefully
|
2026-01-16 20:41:24 -08:00
|
|
|
error_log("PageCounter: Exception: " . $e->getMessage());
|
2026-01-12 03:13:32 -08:00
|
|
|
return null;
|
2013-03-28 23:58:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|