0.9.7.6: WebDAV-Hotpath pro Request cachen

This commit is contained in:
Terranom674
2026-08-19 21:35:06 +02:00
parent a23fc81190
commit 588f1fc100

View File

@@ -7,6 +7,9 @@ if (!defined('PHPWG_ROOT_PATH'))
function bratonien_tools_webdav_image_source_info($image_id) function bratonien_tools_webdav_image_source_info($image_id)
{ {
static $cache = array(); static $cache = array();
static $connection_cache = array();
static $mapping_cache = array();
$image_id = (int)$image_id; $image_id = (int)$image_id;
if ($image_id < 1) return null; if ($image_id < 1) return null;
if (array_key_exists($image_id, $cache)) return $cache[$image_id]; if (array_key_exists($image_id, $cache)) return $cache[$image_id];
@@ -20,12 +23,12 @@ function bratonien_tools_webdav_image_source_info($image_id)
$absolute = $path; $absolute = $path;
if (strpos($absolute, '/') !== 0) if (strpos($absolute, '/') !== 0)
{ {
$absolute = PHPWG_ROOT_PATH.ltrim(preg_replace('#^\./#', '', $absolute), '/'); $absolute = PHPWG_ROOT_PATH.ltrim(preg_replace('#^\\./#', '', $absolute), '/');
} }
$resolved = realpath($absolute); $resolved = realpath($absolute);
if ($resolved === false) return $cache[$image_id] = null; if ($resolved === false) return $cache[$image_id] = null;
$normalized = str_replace('\\', '/', $resolved); $normalized = str_replace('\\\\', '/', $resolved);
if (!preg_match('#/nc-webdav-source/connection-([0-9]+)/root-([0-9]+)/(.*)$#', $normalized, $match)) if (!preg_match('#/nc-webdav-source/connection-([0-9]+)/root-([0-9]+)/(.*)$#', $normalized, $match))
{ {
return $cache[$image_id] = null; return $cache[$image_id] = null;
@@ -36,13 +39,25 @@ function bratonien_tools_webdav_image_source_info($image_id)
$relative_path = trim((string)$match[3], '/'); $relative_path = trim((string)$match[3], '/');
if ($relative_path === '') return $cache[$image_id] = null; if ($relative_path === '') return $cache[$image_id] = null;
$table = defined('BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE') if (!array_key_exists($connection_id, $connection_cache))
? BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE {
: $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections'; $table = defined('BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE')
$connection_result = pwg_query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1'); ? BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE
if (!pwg_db_num_rows($connection_result)) return $cache[$image_id] = null; : $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
$connection_row = pwg_db_fetch_assoc($connection_result); $connection_result = pwg_query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1');
$config = json_decode((string)$connection_row['config_json'], true); if (!pwg_db_num_rows($connection_result))
{
$connection_cache[$connection_id] = null;
}
else
{
$connection_row = pwg_db_fetch_assoc($connection_result);
$decoded = json_decode((string)$connection_row['config_json'], true);
$connection_cache[$connection_id] = is_array($decoded) ? $decoded : null;
}
}
$config = $connection_cache[$connection_id];
if (!is_array($config) || (string)($config['source_mode'] ?? '') !== 'webdav-placeholder') if (!is_array($config) || (string)($config['source_mode'] ?? '') !== 'webdav-placeholder')
{ {
return $cache[$image_id] = null; return $cache[$image_id] = null;
@@ -68,27 +83,33 @@ function bratonien_tools_webdav_image_source_info($image_id)
$size = 0; $size = 0;
$etag = ''; $etag = '';
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/'); if (!array_key_exists($connection_id, $mapping_cache))
if ($state_dir !== '')
{ {
$mapping_file = $state_dir.'/webdav-map.json'; $mapping_cache[$connection_id] = array();
if (is_readable($mapping_file)) $state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
if ($state_dir !== '')
{ {
$mapping = json_decode((string)file_get_contents($mapping_file), true); $mapping_file = $state_dir.'/webdav-map.json';
if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files'])) if (is_readable($mapping_file))
{ {
$entry = $mapping['files'][$resolved] ?? $mapping['files'][$normalized] ?? null; $mapping = json_decode((string)file_get_contents($mapping_file), true);
if (is_array($entry) && (string)($entry['kind'] ?? '') === 'file') if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files']))
{ {
$webdav_path = trim((string)($entry['webdav_path'] ?? $webdav_path), '/'); $mapping_cache[$connection_id] = $mapping['files'];
$content_type = (string)($entry['content_type'] ?? '');
$size = (int)($entry['size'] ?? 0);
$etag = (string)($entry['etag'] ?? '');
} }
} }
} }
} }
$entry = $mapping_cache[$connection_id][$resolved] ?? $mapping_cache[$connection_id][$normalized] ?? null;
if (is_array($entry) && (string)($entry['kind'] ?? '') === 'file')
{
$webdav_path = trim((string)($entry['webdav_path'] ?? $webdav_path), '/');
$content_type = (string)($entry['content_type'] ?? '');
$size = (int)($entry['size'] ?? 0);
$etag = (string)($entry['etag'] ?? '');
}
return $cache[$image_id] = array( return $cache[$image_id] = array(
'image_id'=>$image_id, 'image_id'=>$image_id,
'connection_id'=>$connection_id, 'connection_id'=>$connection_id,
@@ -416,25 +437,30 @@ function bratonien_tools_filter_webdav_src_url($url, $src_image)
function bratonien_tools_filter_webdav_derivative_url($url, $params, $src_image, $rel_url) function bratonien_tools_filter_webdav_derivative_url($url, $params, $src_image, $rel_url)
{ {
if (!is_object($src_image) || empty($src_image->id)) return $url; if (!is_object($src_image) || empty($src_image->id)) return $url;
$info = bratonien_tools_webdav_image_source_info((int)$src_image->id);
if (!$info) return $url;
if (empty($info['root_is_base'])) // Hotpath: Bei einem bereits vorbereiteten WebDAV-Derivat keinerlei
// Connection-DB oder Mapping-Datei mehr anfassen. Der reale Quellpfad
// reicht aus, um Connector-Bilder sicher zu erkennen.
$source_path = $src_image->get_path();
$resolved_source = $source_path !== '' ? realpath($source_path) : false;
if ($resolved_source === false || !preg_match('#/nc-webdav-source/connection-[0-9]+/root-[0-9]+/#', str_replace('\\\\', '/', $resolved_source)))
{ {
try return $url;
}
try
{
$derivative = new DerivativeImage($params, $src_image);
if (!$derivative->same_as_source())
{ {
$derivative = new DerivativeImage($params, $src_image); $path = $derivative->get_path();
if (!$derivative->same_as_source()) if ($path !== '' && is_file($path) && is_readable($path)) return $url;
{
$path = $derivative->get_path();
if ($path !== '' && is_file($path) && is_readable($path)) return $url;
}
}
catch (Throwable $e)
{
error_log('Bratonien WebDAV derivative lookup #'.(int)$src_image->id.': '.$e->getMessage());
} }
} }
catch (Throwable $e)
{
error_log('Bratonien WebDAV derivative lookup #'.(int)$src_image->id.': '.$e->getMessage());
}
$preview_url = bratonien_tools_webdav_image_url((int)$src_image->id, true); $preview_url = bratonien_tools_webdav_image_url((int)$src_image->id, true);
return $preview_url ?: $url; return $preview_url ?: $url;