FIX_POLICY_READ

This commit is contained in:
VENKATESHWARAN 2026-07-30 10:50:07 +05:30
parent 06a977985a
commit 9d32597396
2 changed files with 53 additions and 42 deletions

View File

@ -828,7 +828,7 @@ class PolicyController extends ResourceController
'pdf_local_path'=> $pdfFilePath,
]), JSON_UNESCAPED_SLASHES));
$markdownResult = convert_policy_pdf_to_markdown($pdfFilePath);
$markdownResult = convert_policy_pdf_to_markdown($pdfFilePath, false, $pdfFileName);
if ($markdownResult['status'] !== 'success') {
log_message('error', '[S3_FILE_GET][POLICY_READ][CONVERT][FAILED] PDF to markdown conversion failed | ' . json_encode(array_merge($s3LogCtx, [
'pdf_file_name' => $pdfFileName,
@ -849,30 +849,7 @@ class PolicyController extends ResourceController
'content_bytes' => isset($markdownResult['content']) ? strlen($markdownResult['content']) : null,
]), JSON_UNESCAPED_SLASHES));
log_message('error', '[S3_FILE_GET][POLICY_READ][EXISTS] checking markdown in storage | ' . json_encode(array_merge($s3LogCtx, [
'module' => 'policy',
'sub_folder' => 'policy_md',
'md_file_name' => $mdFileName,
]), JSON_UNESCAPED_SLASHES));
$mdExists = policy_md_exists($mdFileName);
log_message('error', '[S3_FILE_GET][POLICY_READ][EXISTS] markdown exists check complete | ' . json_encode(array_merge($s3LogCtx, [
'md_file_name' => $mdFileName,
'exists' => $mdExists,
]), JSON_UNESCAPED_SLASHES));
if (!$mdExists) {
log_message('error', '[S3_FILE_GET][POLICY_READ][FAILED] markdown file not found in storage | ' . json_encode(array_merge($s3LogCtx, [
'md_file_name' => $mdFileName,
]), JSON_UNESCAPED_SLASHES));
if($return == true)
{
return $this->respond(['status'=>"failed", 'message'=> "Markdown file not found for {$mdFileName}"], 200);
}else{
return ['status'=>"failed", 'message'=> "Markdown file not found for {$mdFileName}"];
}
}
$mdFileName = $markdownResult['md_file_name'] ?? $mdFileName;
if (!empty($markdownResult['content'])) {
$fileContent = $markdownResult['content'];
@ -882,6 +859,31 @@ class PolicyController extends ResourceController
'source' => 'convert_result',
]), JSON_UNESCAPED_SLASHES));
} else {
log_message('error', '[S3_FILE_GET][POLICY_READ][EXISTS] checking markdown in storage | ' . json_encode(array_merge($s3LogCtx, [
'module' => 'policy',
'sub_folder' => 'policy_md',
'md_file_name' => $mdFileName,
]), JSON_UNESCAPED_SLASHES));
$mdExists = policy_md_exists($mdFileName);
log_message('error', '[S3_FILE_GET][POLICY_READ][EXISTS] markdown exists check complete | ' . json_encode(array_merge($s3LogCtx, [
'md_file_name' => $mdFileName,
'exists' => $mdExists,
]), JSON_UNESCAPED_SLASHES));
if (!$mdExists) {
log_message('error', '[S3_FILE_GET][POLICY_READ][FAILED] markdown file not found in storage | ' . json_encode(array_merge($s3LogCtx, [
'md_file_name' => $mdFileName,
]), JSON_UNESCAPED_SLASHES));
if($return == true)
{
return $this->respond(['status'=>"failed", 'message'=> "Markdown file not found for {$mdFileName}"], 200);
}else{
return ['status'=>"failed", 'message'=> "Markdown file not found for {$mdFileName}"];
}
}
log_message('error', '[S3_FILE_GET][POLICY_READ][READ][START] reading markdown from storage | ' . json_encode(array_merge($s3LogCtx, [
'module' => 'policy',
'sub_folder' => 'policy_md',

View File

@ -98,11 +98,14 @@ if (!function_exists('policy_pdf_extract_text')) {
if (!function_exists('convert_policy_pdf_to_markdown')) {
/**
* Convert a policy PDF to markdown, save to policy_md/, then read back from disk.
* Convert a policy PDF to markdown, save to policy_md/, then read back from storage.
*
* Always pass $originalPdfFileName (DB/storage name) when $pdfPath is an S3 temp download
* like /tmp/s3_<uniqid>_original.pdf so the MD key matches the original PDF basename.
*
* @return array{status: string, message: string, content?: string, md_path?: string, md_file_name?: string}
*/
function convert_policy_pdf_to_markdown(string $pdfPath, bool $forceRefresh = false): array
function convert_policy_pdf_to_markdown(string $pdfPath, bool $forceRefresh = false, ?string $originalPdfFileName = null): array
{
if (!is_file($pdfPath)) {
return [
@ -122,15 +125,17 @@ if (!function_exists('convert_policy_pdf_to_markdown')) {
];
}
$mdPath = policy_pdf_md_path($pdfPath);
// Prefer the original storage/DB PDF name so S3 temp paths do not pollute the MD key.
$sourcePdfName = ($originalPdfFileName !== null && $originalPdfFileName !== '')
? basename($originalPdfFileName)
: basename($pdfPath);
$mdPath = policy_md_upload_path($sourcePdfName);
$mdFileName = basename($mdPath);
$mdDir = dirname($mdPath);
if (!is_dir($mdDir) && !mkdir($mdDir, 0777, true) && !is_dir($mdDir)) {
return [
'status' => 'failed',
'message' => "Unable to create markdown directory at {$mdDir}",
];
log_message('error', "Unable to create markdown directory at {$mdDir}; will rely on storage put only");
}
if (!$forceRefresh && policy_md_exists($mdFileName)) {
@ -162,19 +167,23 @@ if (!function_exists('convert_policy_pdf_to_markdown')) {
$markdown = "# Motor Policy Document\n\n" . $extractedText;
if (file_put_contents($mdPath, $markdown) === false) {
return [
'status' => 'failed',
'message' => "Failed to write markdown file at {$mdPath}",
];
// Local write is best-effort (S3 driver may not have write access under writable/).
$localWritten = false;
if (is_dir($mdDir) || (is_dir(dirname($mdDir)) && @mkdir($mdDir, 0777, true))) {
$localWritten = @file_put_contents($mdPath, $markdown) !== false;
if (!$localWritten) {
log_message('error', "Local markdown write failed (continuing with storage put): {$mdPath}");
}
}
policy_put_md($mdFileName, $markdown);
$savedContent = file_get_contents($mdPath);
$savedContent = $localWritten ? (file_get_contents($mdPath) ?: $markdown) : $markdown;
if ($savedContent === false || !policy_md_is_valid($savedContent)) {
@unlink($mdPath);
if (!policy_md_is_valid($savedContent)) {
if ($localWritten) {
@unlink($mdPath);
}
return [
'status' => 'failed',
@ -182,11 +191,11 @@ if (!function_exists('convert_policy_pdf_to_markdown')) {
];
}
log_message('info', "Policy PDF converted to markdown: {$mdPath}");
log_message('info', "Policy PDF converted to markdown: {$mdFileName} (local_written=" . ($localWritten ? '1' : '0') . ')');
return [
'status' => 'success',
'message' => 'PDF converted to markdown and saved to disk',
'message' => 'PDF converted to markdown and saved to storage',
'content' => $savedContent,
'md_path' => $mdPath,
'md_file_name' => $mdFileName,