Add PDF file detection to item metadata

Add a new function to find PDF files in a directory and include the PDF
file path in the item metadata output
This commit is contained in:
Ruben 2025-10-14 21:08:57 +02:00
parent d198b3f5fe
commit dc4292f298

View file

@ -200,6 +200,17 @@ function findCoverImage(string $dirPath): ?string {
return null; return null;
} }
function findPdfFile(string $dirPath): ?string {
$files = scandir($dirPath) ?: [];
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'pdf') {
return $file;
}
}
return null;
}
function loadTranslations(string $lang): array { function loadTranslations(string $lang): array {
$translationFile = dirname(__DIR__) . "/custom/languages/$lang.ini"; $translationFile = dirname(__DIR__) . "/custom/languages/$lang.ini";
if (file_exists($translationFile)) { if (file_exists($translationFile)) {
@ -440,6 +451,7 @@ switch ($parsedPath['type']) {
$metadata = loadMetadata($itemPath, $currentLang, $defaultLang); $metadata = loadMetadata($itemPath, $currentLang, $defaultLang);
$coverImage = findCoverImage($itemPath); $coverImage = findCoverImage($itemPath);
$pdfFile = findPdfFile($itemPath);
$title = $metadata['title'] ?? extractTitle($itemPath, $patterns) ?? $item; $title = $metadata['title'] ?? extractTitle($itemPath, $patterns) ?? $item;
$date = null; $date = null;
@ -461,7 +473,8 @@ switch ($parsedPath['type']) {
'date' => $date, 'date' => $date,
'url' => $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug), 'url' => $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug),
'cover' => $coverImage ? $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug) . '/' . $coverImage : null, 'cover' => $coverImage ? $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug) . '/' . $coverImage : null,
'summary' => $metadata['summary'] ?? null 'summary' => $metadata['summary'] ?? null,
'pdf' => $pdfFile ? $langPrefix . '/' . trim($requestPath, '/') . '/' . urlencode($urlSlug) . '/' . $pdfFile : null
]; ];
}, $subdirs)); }, $subdirs));