add files uuid
This commit is contained in:
@@ -184,78 +184,34 @@ class IndexPageListener
|
|||||||
$db = System::getContainer()->get('database_connection');
|
$db = System::getContainer()->get('database_connection');
|
||||||
$time = time();
|
$time = time();
|
||||||
|
|
||||||
|
// ✅ Contao 5.x robust: Projektverzeichnis statt TL_ROOT
|
||||||
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
$projectDir = System::getContainer()->getParameter('kernel.project_dir');
|
||||||
|
|
||||||
foreach ($fileLinks as $file) {
|
foreach ($fileLinks as $file) {
|
||||||
try {
|
try {
|
||||||
// -------------------------------------------------
|
|
||||||
// URL normalisieren (Fragment weg)
|
|
||||||
// -------------------------------------------------
|
|
||||||
$url = strtok($file['url'], '#');
|
$url = strtok($file['url'], '#');
|
||||||
$parts = parse_url($url);
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
$path = parse_url($url, PHP_URL_PATH);
|
||||||
// ⬅️ NEU: externe Links überspringen
|
$path = $path ? ltrim($path, '/') : null;
|
||||||
// -------------------------------------------------
|
|
||||||
if (!empty($parts['host'])) {
|
|
||||||
// absolute URL → nur erlauben, wenn eigener Host
|
|
||||||
$currentHost = parse_url(System::getContainer()->get('request_stack')->getCurrentRequest()?->getSchemeAndHttpHost() ?? '', PHP_URL_HOST);
|
|
||||||
if ($currentHost && $parts['host'] !== $currentHost) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
// ---------------------------------------------
|
||||||
// ⬅️ NEU: lokalen Dateipfad ermitteln
|
// UUID aus Pfad ermitteln (Contao 4.13 + 5.x)
|
||||||
// -------------------------------------------------
|
// ---------------------------------------------
|
||||||
$normalizedPath = null;
|
|
||||||
|
|
||||||
// 1) direkter Pfad /files/…
|
|
||||||
if (!empty($parts['path'])) {
|
|
||||||
$candidate = ltrim(urldecode($parts['path']), '/');
|
|
||||||
if (str_starts_with($candidate, 'files/')) {
|
|
||||||
$normalizedPath = $candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2) Download-Parameter ?file= / ?p= / ?f=
|
|
||||||
if (!$normalizedPath && !empty($parts['query'])) {
|
|
||||||
parse_str($parts['query'], $query);
|
|
||||||
foreach (['file', 'p', 'f'] as $param) {
|
|
||||||
if (!empty($query[$param])) {
|
|
||||||
$candidate = ltrim(urldecode(html_entity_decode((string) $query[$param], ENT_QUOTES)), '/');
|
|
||||||
if (str_starts_with($candidate, 'files/')) {
|
|
||||||
$normalizedPath = $candidate;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// ⬅️ NEU: wenn keine lokale Datei → skip
|
|
||||||
// -------------------------------------------------
|
|
||||||
if (!$normalizedPath || !is_file($projectDir . '/public/' . $normalizedPath)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------
|
|
||||||
// UUID aus normalisiertem Pfad
|
|
||||||
// -------------------------------------------------
|
|
||||||
$uuid = null;
|
$uuid = null;
|
||||||
$uuidBin = null;
|
$uuidBin = null;
|
||||||
|
|
||||||
|
if ($path && str_starts_with($path, 'files/')) {
|
||||||
if (interface_exists(VirtualFilesystemInterface::class)) {
|
if (interface_exists(VirtualFilesystemInterface::class)) {
|
||||||
try {
|
try {
|
||||||
$vfs = System::getContainer()->get(VirtualFilesystemInterface::class);
|
$vfs = System::getContainer()->get(VirtualFilesystemInterface::class);
|
||||||
$uuid = $vfs->pathToUuid($normalizedPath);
|
$uuid = $vfs->pathToUuid($path);
|
||||||
} catch (\Throwable) {
|
} catch (\Throwable) {
|
||||||
$uuid = null;
|
$uuid = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$uuid) {
|
if (!$uuid) {
|
||||||
$fileModel = FilesModel::findByPath($normalizedPath);
|
$fileModel = FilesModel::findByPath($path);
|
||||||
if ($fileModel) {
|
if ($fileModel) {
|
||||||
$uuid = $fileModel->uuid;
|
$uuid = $fileModel->uuid;
|
||||||
}
|
}
|
||||||
@@ -264,17 +220,15 @@ class IndexPageListener
|
|||||||
if ($uuid) {
|
if ($uuid) {
|
||||||
$uuidBin = StringUtil::uuidToBin($uuid);
|
$uuidBin = StringUtil::uuidToBin($uuid);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------
|
$abs = $path ? $projectDir . '/public/' . $path : null;
|
||||||
// bestehende Logik (unverändert)
|
|
||||||
// -------------------------------------------------
|
|
||||||
$abs = $projectDir . '/public/' . $normalizedPath;
|
|
||||||
|
|
||||||
$mtime = is_file($abs) ? filemtime($abs) : 0;
|
$mtime = ($abs && is_file($abs)) ? filemtime($abs) : 0;
|
||||||
$checksum = md5($url . '|' . $mtime);
|
$checksum = md5($url . '|' . $mtime);
|
||||||
|
|
||||||
$existing = $db->fetchAssociative(
|
$existing = $db->fetchAssociative(
|
||||||
'SELECT id FROM tl_search_files WHERE url = ?',
|
'SELECT id, checksum FROM tl_search_files WHERE url = ?',
|
||||||
[$url]
|
[$url]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -287,10 +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,
|
'uuid' => $uuidBin, // ⬅️ NEU
|
||||||
],
|
],
|
||||||
['id' => $existing['id']]
|
['id' => $existing['id']]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->debug('File updated', [
|
||||||
|
'url' => $url,
|
||||||
|
'uuid' => $uuid,
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
$db->insert(
|
$db->insert(
|
||||||
'tl_search_files',
|
'tl_search_files',
|
||||||
@@ -303,13 +262,19 @@ 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,
|
'uuid' => $uuidBin, // ⬅️ NEU
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$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,
|
||||||
'error' => $e->getMessage(),
|
'error' => $e->getMessage(),
|
||||||
'class' => $e::class,
|
'class' => $e::class,
|
||||||
'code' => $e->getCode(),
|
'code' => $e->getCode(),
|
||||||
@@ -318,7 +283,6 @@ class IndexPageListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$this->debug('Hook end', [
|
$this->debug('Hook end', [
|
||||||
'final_set_keys' => array_keys($set),
|
'final_set_keys' => array_keys($set),
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user