mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 15:14:34 +00:00
Merge pull request #33 from Terranom674/fix/09711-fotorama-ondemand
0.9.7.11: Fotorama-Derivate on demand erzeugen
This commit is contained in:
@@ -40,6 +40,17 @@ function bratonien_tools_webdav_derivative_matches_preview($derivative_path, $pa
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function bratonien_tools_webdav_ondemand_derivative_url($image_id, $params)
|
||||||
|
{
|
||||||
|
$image_id = (int)$image_id;
|
||||||
|
if ($image_id < 1 || !is_object($params)) return null;
|
||||||
|
|
||||||
|
$type = trim((string)($params->type ?? ''));
|
||||||
|
if ($type === '') return null;
|
||||||
|
|
||||||
|
return get_root_url().'plugins/'.BRATONIEN_TOOLS_ID.'/webdav-derivative.php?id='.$image_id.'&type='.rawurlencode($type);
|
||||||
|
}
|
||||||
|
|
||||||
function bratonien_tools_filter_webdav_gallery_derivative_url($url, $params, $src_image, $rel_url)
|
function bratonien_tools_filter_webdav_gallery_derivative_url($url, $params, $src_image, $rel_url)
|
||||||
{
|
{
|
||||||
if (!is_object($src_image) || empty($src_image->id) || empty($src_image->rel_path)) return $url;
|
if (!is_object($src_image) || empty($src_image->id) || empty($src_image->rel_path)) return $url;
|
||||||
@@ -51,7 +62,6 @@ function bratonien_tools_filter_webdav_gallery_derivative_url($url, $params, $sr
|
|||||||
}
|
}
|
||||||
|
|
||||||
$image_id = (int)$src_image->id;
|
$image_id = (int)$src_image->id;
|
||||||
$derivative_path = '';
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$derivative = new DerivativeImage($params, $src_image);
|
$derivative = new DerivativeImage($params, $src_image);
|
||||||
@@ -69,45 +79,20 @@ function bratonien_tools_filter_webdav_gallery_derivative_url($url, $params, $sr
|
|||||||
error_log('Bratonien WebDAV derivative lookup #'.$image_id.': '.$e->getMessage());
|
error_log('Bratonien WebDAV derivative lookup #'.$image_id.': '.$e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auf der Picture-/Fotorama-Seite wird nicht mehr waehrend des HTML-Aufbaus
|
||||||
|
// synchron erzeugt. Fotorama arbeitet lazy: Erst wenn das konkrete Bild in
|
||||||
|
// den Fokus kommt und seine URL abruft, erzeugt dieser Endpoint genau dieses
|
||||||
|
// angeforderte Standard-Derivat aus dem bereits gecachten NC-Preview.
|
||||||
$script_name = basename((string)($_SERVER['SCRIPT_NAME'] ?? ''));
|
$script_name = basename((string)($_SERVER['SCRIPT_NAME'] ?? ''));
|
||||||
if ($script_name === 'picture.php' && function_exists('bratonien_tools_webdav_generate_derivative'))
|
if ($script_name === 'picture.php')
|
||||||
{
|
{
|
||||||
if ($derivative_path !== '' && is_file($derivative_path) && !bratonien_tools_webdav_derivative_matches_preview($derivative_path, $params, $image_id))
|
$ondemand_url = bratonien_tools_webdav_ondemand_derivative_url($image_id, $params);
|
||||||
{
|
if ($ondemand_url) return $ondemand_url;
|
||||||
@unlink($derivative_path);
|
|
||||||
clearstatcache(true, $derivative_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$detail = '';
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (bratonien_tools_webdav_generate_derivative($params, $src_image, $detail))
|
|
||||||
{
|
|
||||||
if ($derivative_path === '')
|
|
||||||
{
|
|
||||||
$probe = new DerivativeImage($params, $src_image);
|
|
||||||
if (!$probe->same_as_source()) $derivative_path = $probe->get_path();
|
|
||||||
}
|
|
||||||
if (bratonien_tools_webdav_derivative_matches_preview($derivative_path, $params, $image_id))
|
|
||||||
{
|
|
||||||
return $url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Throwable $e)
|
|
||||||
{
|
|
||||||
$detail = get_class($e).': '.$e->getMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($detail !== '')
|
|
||||||
{
|
|
||||||
error_log('Bratonien WebDAV picture derivative #'.$image_id.': '.$detail);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connector-Platzhalter oder daraus entstandene Derivate werden nie ausgeliefert.
|
// Galerie-Fallback: niemals Connector-Platzhalter ausliefern. Solange kein
|
||||||
// Wenn noch kein echtes lokales Derivat bereitsteht, geht der Request immer auf
|
// lokales korrektes Derivat vorliegt, wird das echte vorbereitete NC-Preview
|
||||||
// die echte WebDAV-Bildquelle bzw. deren vorbereitetes Preview.
|
// verwendet. Der eigentliche Galerie-Derivattyp wird beim Sync vorgebaut.
|
||||||
$preview_url = bratonien_tools_webdav_image_url($image_id, true);
|
$preview_url = bratonien_tools_webdav_image_url($image_id, true);
|
||||||
return $preview_url ?: $url;
|
return $preview_url ?: $url;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
Plugin Name: Bratonien Tools
|
Plugin Name: Bratonien Tools
|
||||||
Version: 0.9.7.10
|
Version: 0.9.7.11
|
||||||
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
||||||
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
||||||
Author: Bratonien
|
Author: Bratonien
|
||||||
|
|||||||
126
webdav-derivative.php
Normal file
126
webdav-derivative.php
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
define('PHPWG_ROOT_PATH', '../../');
|
||||||
|
include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
|
||||||
|
|
||||||
|
if (!defined('BRATONIEN_TOOLS_PATH'))
|
||||||
|
{
|
||||||
|
define('BRATONIEN_TOOLS_ID', basename(__DIR__));
|
||||||
|
define('BRATONIEN_TOOLS_PATH', PHPWG_ROOT_PATH.'plugins/'.BRATONIEN_TOOLS_ID.'/');
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once(BRATONIEN_TOOLS_PATH.'include/webdav_image_runtime.inc.php');
|
||||||
|
require_once(BRATONIEN_TOOLS_PATH.'include/webdav_gallery_runtime.inc.php');
|
||||||
|
|
||||||
|
function bratonien_tools_webdav_derivative_abort($status, $message)
|
||||||
|
{
|
||||||
|
http_response_code((int)$status);
|
||||||
|
header('Content-Type: text/plain; charset=utf-8');
|
||||||
|
header('Cache-Control: no-store');
|
||||||
|
echo $message;
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$image_id = (int)($_GET['id'] ?? 0);
|
||||||
|
$type = trim((string)($_GET['type'] ?? ''));
|
||||||
|
if ($image_id < 1 || $type === '')
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(400, 'Bild-ID oder Derivattyp fehlt.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$permission_condition = get_sql_condition_FandF(array('forbidden_categories'=>'category_id'), null, true);
|
||||||
|
$access_result = pwg_query('SELECT 1 FROM '.IMAGE_CATEGORY_TABLE.' WHERE image_id='.$image_id.' AND '.$permission_condition.' LIMIT 1');
|
||||||
|
if (!pwg_db_num_rows($access_result))
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(403, 'Kein Zugriff auf dieses Bild.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!class_exists('ImageStdParams') || !class_exists('DerivativeImage') || !class_exists('SrcImage'))
|
||||||
|
{
|
||||||
|
require_once(PHPWG_ROOT_PATH.'include/derivative.inc.php');
|
||||||
|
}
|
||||||
|
|
||||||
|
$params = @ImageStdParams::get_by_type($type);
|
||||||
|
if (!$params)
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(404, 'Unbekannter Piwigo-Derivattyp.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = pwg_query('SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$image_id.' LIMIT 1');
|
||||||
|
if (!pwg_db_num_rows($result))
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(404, 'Bild wurde nicht gefunden.');
|
||||||
|
}
|
||||||
|
$row = pwg_db_fetch_assoc($result);
|
||||||
|
$src = new SrcImage($row);
|
||||||
|
$info = bratonien_tools_webdav_image_source_info($image_id);
|
||||||
|
if (!$info)
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(404, 'Keine WebDAV-Quelle fuer dieses Bild gefunden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$preview = bratonien_tools_webdav_preview_path($info);
|
||||||
|
if (!$preview || !is_file($preview) || !is_readable($preview))
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(404, 'Nextcloud-Preview ist noch nicht vorbereitet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$derivative = new DerivativeImage($params, $src);
|
||||||
|
if ($derivative->same_as_source())
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(404, 'Derivat entspricht der Platzhalterquelle und wird nicht ausgeliefert.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$target = $derivative->get_path();
|
||||||
|
if ($target === '')
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(500, 'Derivat-Zielpfad fehlt.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bratonien_tools_webdav_derivative_matches_preview($target, $params, $image_id))
|
||||||
|
{
|
||||||
|
if (is_file($target))
|
||||||
|
{
|
||||||
|
@unlink($target);
|
||||||
|
clearstatcache(true, $target);
|
||||||
|
}
|
||||||
|
|
||||||
|
$detail = '';
|
||||||
|
if (!bratonien_tools_webdav_generate_derivative($params, $src, $detail))
|
||||||
|
{
|
||||||
|
error_log('Bratonien WebDAV on-demand derivative #'.$image_id.' type='.$type.': '.$detail);
|
||||||
|
bratonien_tools_webdav_derivative_abort(503, 'Derivat konnte noch nicht erzeugt werden.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bratonien_tools_webdav_derivative_matches_preview($target, $params, $image_id))
|
||||||
|
{
|
||||||
|
bratonien_tools_webdav_derivative_abort(503, 'Erzeugtes Derivat ist noch nicht gueltig.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$size = @filesize($target);
|
||||||
|
$mtime = @filemtime($target) ?: time();
|
||||||
|
$etag = sha1($target.'|'.$mtime.'|'.($size ?: 0));
|
||||||
|
$extension = strtolower(pathinfo($target, PATHINFO_EXTENSION));
|
||||||
|
$content_type = 'image/jpeg';
|
||||||
|
if ($extension === 'png') $content_type = 'image/png';
|
||||||
|
elseif ($extension === 'gif') $content_type = 'image/gif';
|
||||||
|
elseif ($extension === 'webp') $content_type = 'image/webp';
|
||||||
|
|
||||||
|
header('Content-Type: '.$content_type);
|
||||||
|
if ($size !== false) header('Content-Length: '.(string)$size);
|
||||||
|
header('ETag: "'.$etag.'"');
|
||||||
|
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mtime).' GMT');
|
||||||
|
header('Cache-Control: private, max-age=86400, must-revalidate');
|
||||||
|
header('X-Content-Type-Options: nosniff');
|
||||||
|
|
||||||
|
$client_etag = trim((string)($_SERVER['HTTP_IF_NONE_MATCH'] ?? ''), " \t\r\n\"");
|
||||||
|
if ($client_etag !== '' && hash_equals($etag, $client_etag))
|
||||||
|
{
|
||||||
|
http_response_code(304);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'HEAD')
|
||||||
|
{
|
||||||
|
readfile($target);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user