add files uuid

This commit is contained in:
Jürgen Mummert
2026-01-12 10:59:36 +01:00
parent bc35527f3f
commit b7a5e95c7d
+69 -55
View File
@@ -26,7 +26,7 @@ class MeilisearchFileHelper
]); ]);
// ------------------------------------------------- // -------------------------------------------------
// 1. URL zerlegen // 1. URL normalisieren
// ------------------------------------------------- // -------------------------------------------------
$cleanUrl = strtok($url, '#'); $cleanUrl = strtok($url, '#');
$parts = parse_url($cleanUrl); $parts = parse_url($cleanUrl);
@@ -37,12 +37,16 @@ class MeilisearchFileHelper
} }
// ------------------------------------------------- // -------------------------------------------------
// 2. Externe Datei? // 2. Externe Datei? → skip
// ------------------------------------------------- // -------------------------------------------------
if (!empty($parts['host'])) { if (!empty($parts['host'])) {
$pageHost = parse_url(System::getContainer() $currentRequest = System::getContainer()
->get('request_stack') ->get('request_stack')
->getCurrentRequest()?->getUri() ?? '', PHP_URL_HOST); ->getCurrentRequest();
$pageHost = $currentRequest
? parse_url($currentRequest->getSchemeAndHttpHost(), PHP_URL_HOST)
: null;
if ($pageHost && $parts['host'] !== $pageHost) { if ($pageHost && $parts['host'] !== $pageHost) {
$this->log('External file detected, skip', [ $this->log('External file detected, skip', [
@@ -52,9 +56,9 @@ class MeilisearchFileHelper
} }
} }
// ------------------------------------------------- // -------------------------------------------------
// 3. Normalisierten lokalen Pfad ermitteln (UUID-first) // 3. Pfad-Kandidaten sammeln (ohne Annahmen!)
// ------------------------------------------------- // -------------------------------------------------
$query = []; $query = [];
if (!empty($parts['query'])) { if (!empty($parts['query'])) {
parse_str($parts['query'], $query); parse_str($parts['query'], $query);
@@ -62,49 +66,54 @@ class MeilisearchFileHelper
$pathCandidates = []; $pathCandidates = [];
// direkter Pfad // direkter Pfad
if (!empty($parts['path'])) { if (!empty($parts['path'])) {
$pathCandidates[] = $parts['path']; $pathCandidates[] = $parts['path'];
} }
// Download-Parameter // Download-Parameter
foreach (['file', 'f', 'p'] as $param) { foreach (['file', 'f', 'p'] as $param) {
if (!empty($query[$param])) { if (!empty($query[$param])) {
$pathCandidates[] = $query[$param]; $pathCandidates[] = $query[$param];
} }
} }
// normalisieren
$pathCandidates = array_values(array_unique(array_filter(array_map( $pathCandidates = array_values(array_unique(array_filter(array_map(
static function ($c) { static function ($candidate) {
$c = rawurldecode(html_entity_decode((string) $c, ENT_QUOTES)); $candidate = rawurldecode(html_entity_decode((string) $candidate, ENT_QUOTES));
$c = ltrim($c, '/'); return ltrim($candidate, '/') ?: null;
return $c !== '' ? $c : null;
}, },
$pathCandidates $pathCandidates
)))); ))));
$this->log('Path candidates (normalized)', ['candidates' => $pathCandidates]); $this->log('Path candidates (normalized)', [
'candidates' => $pathCandidates,
]);
$resolvedModel = null; // -------------------------------------------------
// 4. FilesModel (DBAFS) auflösen → UUID
// -------------------------------------------------
$fileModel = null;
// Wir testen Kandidaten in dieser Reihenfolge:
// - kandidat selbst
// - falls nicht mit "files/" beginnt: zusätzlich "files/".$kandidat
foreach ($pathCandidates as $candidate) { foreach ($pathCandidates as $candidate) {
// 1) direkt versuchen // 1) direkt
$model = FilesModel::findByPath($candidate); $model = FilesModel::findByPath($candidate);
if ($model && $model->uuid) { if ($model && $model->uuid) {
$resolvedModel = $model; $fileModel = $model;
$this->log('Resolved via FilesModel (direct)', ['candidate' => $candidate, 'path' => $model->path]); $this->log('Resolved via FilesModel (direct)', [
'candidate' => $candidate,
'path' => $model->path,
]);
break; break;
} }
// 2) fallback: "files/" davor (ohne Annahme über Ordnerstruktur) // 2) fallback: files/ davor
if (!str_starts_with($candidate, 'files/')) { if (!str_starts_with($candidate, 'files/')) {
$model = FilesModel::findByPath('files/' . $candidate); $model = FilesModel::findByPath('files/' . $candidate);
if ($model && $model->uuid) { if ($model && $model->uuid) {
$resolvedModel = $model; $fileModel = $model;
$this->log('Resolved via FilesModel (files/ prefix)', [ $this->log('Resolved via FilesModel (files/ prefix)', [
'candidate' => $candidate, 'candidate' => $candidate,
'path' => $model->path, 'path' => $model->path,
@@ -114,24 +123,30 @@ class MeilisearchFileHelper
} }
} }
if (!$resolvedModel) { if (!$fileModel) {
$this->log('No Contao file model found for candidates, skip', ['candidates' => $pathCandidates]); $this->log('No Contao file model found, skip', [
'candidates' => $pathCandidates,
]);
return; return;
} }
$normalizedPath = (string) $resolvedModel->path; $normalizedPath = (string) $fileModel->path;
$uuidBin = $resolvedModel->uuid; $uuidBin = $fileModel->uuid;
$uuid = StringUtil::binToUuid($uuidBin);
$this->log('UUID resolved', [ $this->log('UUID resolved', [
'path' => $normalizedPath, 'path' => $normalizedPath,
'uuid' => StringUtil::binToUuid($uuidBin), 'uuid' => $uuid,
]); ]);
// -------------------------------------------------
// 5. Datei im Filesystem prüfen
// -------------------------------------------------
$projectDir = System::getContainer()->getParameter('kernel.project_dir'); $projectDir = System::getContainer()->getParameter('kernel.project_dir');
$abs = $projectDir . '/public/' . $normalizedPath; $abs = $projectDir . '/public/' . $normalizedPath;
if (!is_file($abs)) { if (!is_file($abs)) {
$this->log('Resolved model but file missing on FS, skip', [ $this->log('Resolved model but file missing on filesystem, skip', [
'path' => $normalizedPath, 'path' => $normalizedPath,
'abs' => $abs, 'abs' => $abs,
]); ]);
@@ -139,36 +154,31 @@ class MeilisearchFileHelper
} }
// ------------------------------------------------- // -------------------------------------------------
// 4. UUID aus Contao ermitteln // 6. Redaktionellen Titel aus tl_files.meta
// ------------------------------------------------- // -------------------------------------------------
$fileModel = FilesModel::findByPath($normalizedPath); $title = null;
$meta = StringUtil::deserialize($fileModel->meta, true);
$lang = $GLOBALS['TL_LANGUAGE'] ?? 'de';
if (!$fileModel || !$fileModel->uuid) { if (!empty($meta[$lang]['title'])) {
$this->log('File has no Contao UUID, skip', [ $title = trim((string) $meta[$lang]['title']);
'path' => $normalizedPath,
]);
return;
} }
$uuidBin = $fileModel->uuid; if ($title) {
$this->log('Title resolved from tl_files', [
$this->log('UUID resolved', [ 'title' => $title,
'path' => $normalizedPath,
'uuid' => StringUtil::binToUuid($uuidBin),
]); ]);
}
// ------------------------------------------------- // -------------------------------------------------
// 5. Dateiinformationen // 7. Datei-Infos
// ------------------------------------------------- // -------------------------------------------------
$abs = System::getContainer()->getParameter('kernel.project_dir') . '/public/' . $normalizedPath;
$mtime = filemtime($abs) ?: 0; $mtime = filemtime($abs) ?: 0;
$checksum = md5($normalizedPath . '|' . $mtime); $checksum = md5($normalizedPath . '|' . $mtime);
$now = time(); $now = time();
// ------------------------------------------------- // -------------------------------------------------
// 6. Existiert Eintrag bereits über UUID? // 8. Upsert über UUID
// ------------------------------------------------- // -------------------------------------------------
$existing = $this->connection->fetchAssociative( $existing = $this->connection->fetchAssociative(
'SELECT id FROM tl_search_files WHERE uuid = ?', 'SELECT id FROM tl_search_files WHERE uuid = ?',
@@ -176,10 +186,7 @@ class MeilisearchFileHelper
); );
if ($existing) { if ($existing) {
// UPDATE $data = [
$this->connection->update(
'tl_search_files',
[
'tstamp' => $now, 'tstamp' => $now,
'last_seen' => $now, 'last_seen' => $now,
'type' => $type, 'type' => $type,
@@ -187,15 +194,22 @@ class MeilisearchFileHelper
'page_id' => $pageId, 'page_id' => $pageId,
'file_mtime' => $mtime, 'file_mtime' => $mtime,
'checksum' => $checksum, 'checksum' => $checksum,
], ];
if ($title !== null) {
$data['title'] = $title;
}
$this->connection->update(
'tl_search_files',
$data,
['id' => $existing['id']] ['id' => $existing['id']]
); );
$this->log('File updated by UUID', [ $this->log('File updated by UUID', [
'uuid' => StringUtil::binToUuid($uuidBin), 'uuid' => $uuid,
]); ]);
} else { } else {
// INSERT
$this->connection->insert( $this->connection->insert(
'tl_search_files', 'tl_search_files',
[ [
@@ -203,7 +217,7 @@ class MeilisearchFileHelper
'last_seen' => $now, 'last_seen' => $now,
'type' => $type, 'type' => $type,
'url' => $cleanUrl, 'url' => $cleanUrl,
'title' => basename($normalizedPath), 'title' => $title ?? basename($normalizedPath),
'page_id' => $pageId, 'page_id' => $pageId,
'file_mtime' => $mtime, 'file_mtime' => $mtime,
'checksum' => $checksum, 'checksum' => $checksum,
@@ -212,7 +226,7 @@ class MeilisearchFileHelper
); );
$this->log('File inserted by UUID', [ $this->log('File inserted by UUID', [
'uuid' => StringUtil::binToUuid($uuidBin), 'uuid' => $uuid,
]); ]);
} }