mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 15:14:34 +00:00
0.9.7.6: WebDAV-Hotpath pro Request cachen
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
if (!array_key_exists($connection_id, $connection_cache))
|
||||||
|
{
|
||||||
$table = defined('BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE')
|
$table = defined('BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE')
|
||||||
? BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE
|
? BRATONIEN_TOOLS_NC_CONNECTIONS_TABLE
|
||||||
: $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
: $GLOBALS['prefixeTable'].'bratonien_tools_nc_connections';
|
||||||
$connection_result = pwg_query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1');
|
$connection_result = pwg_query('SELECT config_json FROM `'.$table.'` WHERE id='.$connection_id.' LIMIT 1');
|
||||||
if (!pwg_db_num_rows($connection_result)) return $cache[$image_id] = null;
|
if (!pwg_db_num_rows($connection_result))
|
||||||
|
{
|
||||||
|
$connection_cache[$connection_id] = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$connection_row = pwg_db_fetch_assoc($connection_result);
|
$connection_row = pwg_db_fetch_assoc($connection_result);
|
||||||
$config = json_decode((string)$connection_row['config_json'], true);
|
$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,6 +83,9 @@ function bratonien_tools_webdav_image_source_info($image_id)
|
|||||||
$size = 0;
|
$size = 0;
|
||||||
$etag = '';
|
$etag = '';
|
||||||
|
|
||||||
|
if (!array_key_exists($connection_id, $mapping_cache))
|
||||||
|
{
|
||||||
|
$mapping_cache[$connection_id] = array();
|
||||||
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
$state_dir = rtrim((string)($config['state_dir'] ?? ''), '/');
|
||||||
if ($state_dir !== '')
|
if ($state_dir !== '')
|
||||||
{
|
{
|
||||||
@@ -77,7 +95,13 @@ function bratonien_tools_webdav_image_source_info($image_id)
|
|||||||
$mapping = json_decode((string)file_get_contents($mapping_file), true);
|
$mapping = json_decode((string)file_get_contents($mapping_file), true);
|
||||||
if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files']))
|
if (is_array($mapping) && isset($mapping['files']) && is_array($mapping['files']))
|
||||||
{
|
{
|
||||||
$entry = $mapping['files'][$resolved] ?? $mapping['files'][$normalized] ?? null;
|
$mapping_cache[$connection_id] = $mapping['files'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$entry = $mapping_cache[$connection_id][$resolved] ?? $mapping_cache[$connection_id][$normalized] ?? null;
|
||||||
if (is_array($entry) && (string)($entry['kind'] ?? '') === 'file')
|
if (is_array($entry) && (string)($entry['kind'] ?? '') === 'file')
|
||||||
{
|
{
|
||||||
$webdav_path = trim((string)($entry['webdav_path'] ?? $webdav_path), '/');
|
$webdav_path = trim((string)($entry['webdav_path'] ?? $webdav_path), '/');
|
||||||
@@ -85,9 +109,6 @@ function bratonien_tools_webdav_image_source_info($image_id)
|
|||||||
$size = (int)($entry['size'] ?? 0);
|
$size = (int)($entry['size'] ?? 0);
|
||||||
$etag = (string)($entry['etag'] ?? '');
|
$etag = (string)($entry['etag'] ?? '');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $cache[$image_id] = array(
|
return $cache[$image_id] = array(
|
||||||
'image_id'=>$image_id,
|
'image_id'=>$image_id,
|
||||||
@@ -416,11 +437,17 @@ 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)))
|
||||||
{
|
{
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$derivative = new DerivativeImage($params, $src_image);
|
$derivative = new DerivativeImage($params, $src_image);
|
||||||
@@ -434,7 +461,6 @@ function bratonien_tools_filter_webdav_derivative_url($url, $params, $src_image,
|
|||||||
{
|
{
|
||||||
error_log('Bratonien WebDAV derivative lookup #'.(int)$src_image->id.': '.$e->getMessage());
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user