FIX_POLICY_READ
This commit is contained in:
parent
06a977985a
commit
9d32597396
@ -828,7 +828,7 @@ class PolicyController extends ResourceController
|
|||||||
'pdf_local_path'=> $pdfFilePath,
|
'pdf_local_path'=> $pdfFilePath,
|
||||||
]), JSON_UNESCAPED_SLASHES));
|
]), JSON_UNESCAPED_SLASHES));
|
||||||
|
|
||||||
$markdownResult = convert_policy_pdf_to_markdown($pdfFilePath);
|
$markdownResult = convert_policy_pdf_to_markdown($pdfFilePath, false, $pdfFileName);
|
||||||
if ($markdownResult['status'] !== 'success') {
|
if ($markdownResult['status'] !== 'success') {
|
||||||
log_message('error', '[S3_FILE_GET][POLICY_READ][CONVERT][FAILED] PDF to markdown conversion failed | ' . json_encode(array_merge($s3LogCtx, [
|
log_message('error', '[S3_FILE_GET][POLICY_READ][CONVERT][FAILED] PDF to markdown conversion failed | ' . json_encode(array_merge($s3LogCtx, [
|
||||||
'pdf_file_name' => $pdfFileName,
|
'pdf_file_name' => $pdfFileName,
|
||||||
@ -849,6 +849,16 @@ class PolicyController extends ResourceController
|
|||||||
'content_bytes' => isset($markdownResult['content']) ? strlen($markdownResult['content']) : null,
|
'content_bytes' => isset($markdownResult['content']) ? strlen($markdownResult['content']) : null,
|
||||||
]), JSON_UNESCAPED_SLASHES));
|
]), JSON_UNESCAPED_SLASHES));
|
||||||
|
|
||||||
|
$mdFileName = $markdownResult['md_file_name'] ?? $mdFileName;
|
||||||
|
|
||||||
|
if (!empty($markdownResult['content'])) {
|
||||||
|
$fileContent = $markdownResult['content'];
|
||||||
|
log_message('error', '[S3_FILE_GET][POLICY_READ][CONTENT] content taken from convert result (no extra storage read) | ' . json_encode(array_merge($s3LogCtx, [
|
||||||
|
'md_file_name' => $mdFileName,
|
||||||
|
'content_bytes' => strlen($fileContent),
|
||||||
|
'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, [
|
log_message('error', '[S3_FILE_GET][POLICY_READ][EXISTS] checking markdown in storage | ' . json_encode(array_merge($s3LogCtx, [
|
||||||
'module' => 'policy',
|
'module' => 'policy',
|
||||||
'sub_folder' => 'policy_md',
|
'sub_folder' => 'policy_md',
|
||||||
@ -874,14 +884,6 @@ class PolicyController extends ResourceController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($markdownResult['content'])) {
|
|
||||||
$fileContent = $markdownResult['content'];
|
|
||||||
log_message('error', '[S3_FILE_GET][POLICY_READ][CONTENT] content taken from convert result (no extra storage read) | ' . json_encode(array_merge($s3LogCtx, [
|
|
||||||
'md_file_name' => $mdFileName,
|
|
||||||
'content_bytes' => strlen($fileContent),
|
|
||||||
'source' => 'convert_result',
|
|
||||||
]), JSON_UNESCAPED_SLASHES));
|
|
||||||
} else {
|
|
||||||
log_message('error', '[S3_FILE_GET][POLICY_READ][READ][START] reading markdown from storage | ' . json_encode(array_merge($s3LogCtx, [
|
log_message('error', '[S3_FILE_GET][POLICY_READ][READ][START] reading markdown from storage | ' . json_encode(array_merge($s3LogCtx, [
|
||||||
'module' => 'policy',
|
'module' => 'policy',
|
||||||
'sub_folder' => 'policy_md',
|
'sub_folder' => 'policy_md',
|
||||||
|
|||||||
@ -98,11 +98,14 @@ if (!function_exists('policy_pdf_extract_text')) {
|
|||||||
|
|
||||||
if (!function_exists('convert_policy_pdf_to_markdown')) {
|
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}
|
* @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)) {
|
if (!is_file($pdfPath)) {
|
||||||
return [
|
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);
|
$mdFileName = basename($mdPath);
|
||||||
$mdDir = dirname($mdPath);
|
$mdDir = dirname($mdPath);
|
||||||
|
|
||||||
if (!is_dir($mdDir) && !mkdir($mdDir, 0777, true) && !is_dir($mdDir)) {
|
if (!is_dir($mdDir) && !mkdir($mdDir, 0777, true) && !is_dir($mdDir)) {
|
||||||
return [
|
log_message('error', "Unable to create markdown directory at {$mdDir}; will rely on storage put only");
|
||||||
'status' => 'failed',
|
|
||||||
'message' => "Unable to create markdown directory at {$mdDir}",
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$forceRefresh && policy_md_exists($mdFileName)) {
|
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;
|
$markdown = "# Motor Policy Document\n\n" . $extractedText;
|
||||||
|
|
||||||
if (file_put_contents($mdPath, $markdown) === false) {
|
// Local write is best-effort (S3 driver may not have write access under writable/).
|
||||||
return [
|
$localWritten = false;
|
||||||
'status' => 'failed',
|
if (is_dir($mdDir) || (is_dir(dirname($mdDir)) && @mkdir($mdDir, 0777, true))) {
|
||||||
'message' => "Failed to write markdown file at {$mdPath}",
|
$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);
|
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)) {
|
if (!policy_md_is_valid($savedContent)) {
|
||||||
|
if ($localWritten) {
|
||||||
@unlink($mdPath);
|
@unlink($mdPath);
|
||||||
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'status' => 'failed',
|
'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 [
|
return [
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'PDF converted to markdown and saved to disk',
|
'message' => 'PDF converted to markdown and saved to storage',
|
||||||
'content' => $savedContent,
|
'content' => $savedContent,
|
||||||
'md_path' => $mdPath,
|
'md_path' => $mdPath,
|
||||||
'md_file_name' => $mdFileName,
|
'md_file_name' => $mdFileName,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user