add files uuid

This commit is contained in:
Jürgen Mummert
2026-01-12 09:41:54 +01:00
parent 579f58b614
commit f402e6546a
+50 -10
View File
@@ -4,6 +4,9 @@ namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\Config; use Contao\Config;
use Contao\System; use Contao\System;
use Contao\FilesModel;
use Contao\StringUtil;
use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface;
class IndexPageListener class IndexPageListener
{ {
@@ -186,10 +189,40 @@ class IndexPageListener
foreach ($fileLinks as $file) { foreach ($fileLinks as $file) {
try { try {
$url = strtok($file['url'], '#'); $url = strtok($file['url'], '#');
$path = parse_url($url, PHP_URL_PATH); $path = parse_url($url, PHP_URL_PATH);
$abs = $path ? $projectDir . '/public/' . ltrim($path, '/') : null; $path = $path ? ltrim($path, '/') : null;
// ---------------------------------------------
// UUID aus Pfad ermitteln (Contao 4.13 + 5.x)
// ---------------------------------------------
$uuid = null;
$uuidBin = null;
if ($path && str_starts_with($path, 'files/')) {
if (interface_exists(VirtualFilesystemInterface::class)) {
try {
$vfs = System::getContainer()->get(VirtualFilesystemInterface::class);
$uuid = $vfs->pathToUuid($path);
} catch (\Throwable) {
$uuid = null;
}
}
if (!$uuid) {
$fileModel = FilesModel::findByPath($path);
if ($fileModel) {
$uuid = $fileModel->uuid;
}
}
if ($uuid) {
$uuidBin = StringUtil::uuidToBin($uuid);
}
}
$abs = $path ? $projectDir . '/public/' . $path : null;
$mtime = ($abs && is_file($abs)) ? filemtime($abs) : 0; $mtime = ($abs && is_file($abs)) ? filemtime($abs) : 0;
$checksum = md5($url . '|' . $mtime); $checksum = md5($url . '|' . $mtime);
@@ -208,11 +241,15 @@ class IndexPageListener
'page_id' => (int) ($data['pid'] ?? 0), 'page_id' => (int) ($data['pid'] ?? 0),
'file_mtime' => $mtime, 'file_mtime' => $mtime,
'checksum' => $checksum, 'checksum' => $checksum,
'uuid' => $uuidBin, // ⬅️ NEU
], ],
['id' => $existing['id']] ['id' => $existing['id']]
); );
$this->debug('File updated', ['url' => $url, 'checksum' => $checksum]); $this->debug('File updated', [
'url' => $url,
'uuid' => $uuid,
]);
} else { } else {
$db->insert( $db->insert(
'tl_search_files', 'tl_search_files',
@@ -225,19 +262,22 @@ class IndexPageListener
'page_id' => (int) ($data['pid'] ?? 0), 'page_id' => (int) ($data['pid'] ?? 0),
'file_mtime' => $mtime, 'file_mtime' => $mtime,
'checksum' => $checksum, 'checksum' => $checksum,
'uuid' => $uuidBin, // ⬅️ NEU
] ]
); );
$this->debug('File inserted', ['url' => $url, 'checksum' => $checksum]); $this->debug('File inserted', [
'url' => $url,
'uuid' => $uuid,
]);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->debug('File upsert FAILED', [ $this->debug('File upsert FAILED', [
'url' => $file['url'] ?? null, 'url' => $file['url'] ?? null,
'type' => $file['type'] ?? null, 'type' => $file['type'] ?? null,
'error' => $e->getMessage(), 'error' => $e->getMessage(),
'class' => $e::class, 'class' => $e::class,
// falls DBAL-Exception: SQLSTATE/Code helfen brutal beim Finden 'code' => $e->getCode(),
'code' => $e->getCode(),
]); ]);
} }
} }