mirror of
https://github.com/Terranom674/Piwigo_Bratonien_Tools.git
synced 2026-09-19 17:34:31 +00:00
Compare commits
14 Commits
fix/09720-
...
fix/09724-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
29990aee4f | ||
|
|
9c551c8dfc | ||
|
|
e5bc260622 | ||
|
|
5b883706c7 | ||
|
|
66f463422e | ||
|
|
df9e9e6424 | ||
|
|
26b602783f | ||
|
|
ae1b47c4c4 | ||
|
|
6f2f39cd37 | ||
|
|
2794719ff3 | ||
|
|
941d0cd7bb | ||
|
|
392038ce7e | ||
|
|
3aa82b18f4 | ||
|
|
0a76919a21 |
@@ -1,6 +1,134 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var statusEndpoint = 'plugins/bratonien_tools/nc-connector-status.php';
|
||||
|
||||
function ensureLiveStatus(detail, actions) {
|
||||
var node = detail.querySelector('[data-nc-run-live]');
|
||||
if (node) return node;
|
||||
|
||||
node = document.createElement('div');
|
||||
node.setAttribute('data-nc-run-live', '1');
|
||||
node.className = 'bratonien-base-note';
|
||||
node.style.marginTop = '.6rem';
|
||||
node.hidden = true;
|
||||
actions.parentNode.insertBefore(node, actions.nextSibling);
|
||||
return node;
|
||||
}
|
||||
|
||||
function renderLiveStatus(node, data) {
|
||||
var state = String(data && data.state || '');
|
||||
var message = String(data && data.message || '');
|
||||
var detail = String(data && data.error_detail || '');
|
||||
|
||||
node.hidden = false;
|
||||
node.innerHTML = '';
|
||||
|
||||
var strong = document.createElement('strong');
|
||||
if (state === 'queued') strong.textContent = 'Angefordert: ';
|
||||
else if (state === 'running') strong.textContent = 'Läuft: ';
|
||||
else if (state === 'ok' || state === 'success') strong.textContent = 'Erfolgreich: ';
|
||||
else if (state === 'error') strong.textContent = 'Fehler: ';
|
||||
else strong.textContent = 'Status: ';
|
||||
node.appendChild(strong);
|
||||
node.appendChild(document.createTextNode(message || state || 'Status wird ermittelt …'));
|
||||
|
||||
if (detail) {
|
||||
var details = document.createElement('details');
|
||||
details.style.marginTop = '.35rem';
|
||||
var summary = document.createElement('summary');
|
||||
summary.textContent = 'Technische Laufzeitdetails';
|
||||
var pre = document.createElement('pre');
|
||||
pre.style.whiteSpace = 'pre-wrap';
|
||||
pre.style.wordBreak = 'break-word';
|
||||
pre.textContent = detail;
|
||||
details.appendChild(summary);
|
||||
details.appendChild(pre);
|
||||
node.appendChild(details);
|
||||
}
|
||||
}
|
||||
|
||||
function pollConnection(connectionId, node, button) {
|
||||
var attempts = 0;
|
||||
var maxAttempts = 180;
|
||||
var timer = null;
|
||||
|
||||
function stop() {
|
||||
if (timer) window.clearInterval(timer);
|
||||
timer = null;
|
||||
if (button) {
|
||||
button.disabled = false;
|
||||
button.textContent = 'Jetzt abgleichen';
|
||||
}
|
||||
}
|
||||
|
||||
function poll() {
|
||||
attempts += 1;
|
||||
fetch(statusEndpoint + '?connection_id=' + encodeURIComponent(connectionId) + '&_=' + Date.now(), {
|
||||
credentials: 'same-origin',
|
||||
cache: 'no-store',
|
||||
headers: {'Accept': 'application/json'}
|
||||
})
|
||||
.then(function (response) {
|
||||
if (!response.ok) throw new Error('HTTP ' + response.status);
|
||||
return response.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
renderLiveStatus(node, data);
|
||||
var state = String(data && data.state || '');
|
||||
if (state === 'ok' || state === 'success' || state === 'error' || attempts >= maxAttempts) stop();
|
||||
})
|
||||
.catch(function (error) {
|
||||
if (attempts >= maxAttempts) {
|
||||
renderLiveStatus(node, {state: 'error', message: 'Status konnte nicht gelesen werden.', error_detail: error.message});
|
||||
stop();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
poll();
|
||||
timer = window.setInterval(poll, 1000);
|
||||
}
|
||||
|
||||
function bindRunNow(form, detail, liveNode) {
|
||||
if (form.dataset.ncRunBound === '1') return;
|
||||
form.dataset.ncRunBound = '1';
|
||||
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
detail.open = true;
|
||||
|
||||
var button = form.querySelector('button[value="nc_connector_run_now"]');
|
||||
var connectionInput = form.querySelector('input[name="connection_id"]');
|
||||
if (!connectionInput) return;
|
||||
|
||||
if (button) {
|
||||
button.disabled = true;
|
||||
button.textContent = 'Abgleich wird gestartet …';
|
||||
}
|
||||
renderLiveStatus(liveNode, {state: 'queued', message: 'Abgleich wird angefordert …'});
|
||||
|
||||
fetch(form.action || window.location.href, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
cache: 'no-store',
|
||||
body: new FormData(form)
|
||||
})
|
||||
.then(function (response) {
|
||||
if (!response.ok) throw new Error('HTTP ' + response.status);
|
||||
renderLiveStatus(liveNode, {state: 'queued', message: 'Abgleich wurde angefordert.'});
|
||||
pollConnection(connectionInput.value, liveNode, button);
|
||||
})
|
||||
.catch(function (error) {
|
||||
renderLiveStatus(liveNode, {state: 'error', message: 'Abgleich konnte nicht angefordert werden.', error_detail: error.message});
|
||||
if (button) {
|
||||
button.disabled = false;
|
||||
button.textContent = 'Jetzt abgleichen';
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function restoreRunNowButtons() {
|
||||
var section = document.getElementById('nc-connector');
|
||||
if (!section) return;
|
||||
@@ -18,36 +146,42 @@
|
||||
var details = connectionCard.querySelectorAll(':scope > details');
|
||||
details.forEach(function (detail) {
|
||||
var actions = detail.querySelector('.bratonien-actions');
|
||||
if (!actions || actions.querySelector('[value="nc_connector_run_now"]')) return;
|
||||
if (!actions) return;
|
||||
|
||||
var connectionInput = detail.querySelector('input[name="connection_id"]');
|
||||
var tokenInput = detail.querySelector('input[name="pwg_token"]');
|
||||
if (!connectionInput || !tokenInput) return;
|
||||
|
||||
var form = document.createElement('form');
|
||||
form.method = 'post';
|
||||
var form = actions.querySelector('form[data-nc-run-now]');
|
||||
if (!form) {
|
||||
form = document.createElement('form');
|
||||
form.method = 'post';
|
||||
form.setAttribute('data-nc-run-now', '1');
|
||||
|
||||
var token = document.createElement('input');
|
||||
token.type = 'hidden';
|
||||
token.name = 'pwg_token';
|
||||
token.value = tokenInput.value;
|
||||
form.appendChild(token);
|
||||
var token = document.createElement('input');
|
||||
token.type = 'hidden';
|
||||
token.name = 'pwg_token';
|
||||
token.value = tokenInput.value;
|
||||
form.appendChild(token);
|
||||
|
||||
var connection = document.createElement('input');
|
||||
connection.type = 'hidden';
|
||||
connection.name = 'connection_id';
|
||||
connection.value = connectionInput.value;
|
||||
form.appendChild(connection);
|
||||
var connection = document.createElement('input');
|
||||
connection.type = 'hidden';
|
||||
connection.name = 'connection_id';
|
||||
connection.value = connectionInput.value;
|
||||
form.appendChild(connection);
|
||||
|
||||
var button = document.createElement('button');
|
||||
button.className = 'buttonLike';
|
||||
button.type = 'submit';
|
||||
button.name = 'bratonien_tool';
|
||||
button.value = 'nc_connector_run_now';
|
||||
button.textContent = 'Jetzt abgleichen';
|
||||
form.appendChild(button);
|
||||
var button = document.createElement('button');
|
||||
button.className = 'buttonLike';
|
||||
button.type = 'submit';
|
||||
button.name = 'bratonien_tool';
|
||||
button.value = 'nc_connector_run_now';
|
||||
button.textContent = 'Jetzt abgleichen';
|
||||
form.appendChild(button);
|
||||
|
||||
actions.insertBefore(form, actions.firstChild);
|
||||
actions.insertBefore(form, actions.firstChild);
|
||||
}
|
||||
|
||||
bindRunNow(form, detail, ensureLiveStatus(detail, actions));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/*
|
||||
Plugin Name: Bratonien Tools
|
||||
Version: 0.9.7.20
|
||||
Version: 0.9.7.24
|
||||
Description: Erweiterbare Administrationswerkzeuge fuer die Bratonien-Piwigo-Installation.
|
||||
Plugin URI: https://github.com/Terranom674/Piwigo_Bratonien_Tools
|
||||
Author: Bratonien
|
||||
|
||||
@@ -24,6 +24,7 @@ if (!function_exists('is_admin') || !is_admin())
|
||||
exit;
|
||||
}
|
||||
|
||||
$requested_connection_id = isset($_GET['connection_id']) ? max(0, (int)$_GET['connection_id']) : 0;
|
||||
$dir = PHPWG_ROOT_PATH.'_data/bratonien-tools/nc-connector-status';
|
||||
$latest = array(
|
||||
'state'=>'idle',
|
||||
@@ -41,11 +42,16 @@ $latest = array(
|
||||
'route_timestamp'=>0,
|
||||
'route_time_label'=>'Nicht verfügbar',
|
||||
'route_detail'=>'',
|
||||
'connection_id'=>$requested_connection_id,
|
||||
);
|
||||
|
||||
if (is_dir($dir))
|
||||
{
|
||||
foreach (glob($dir.'/connection-*.json') ?: array() as $file)
|
||||
$files = $requested_connection_id > 0
|
||||
? array($dir.'/connection-'.$requested_connection_id.'.json')
|
||||
: (glob($dir.'/connection-*.json') ?: array());
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (!is_readable($file))
|
||||
{
|
||||
@@ -63,60 +69,58 @@ if (is_dir($dir))
|
||||
}
|
||||
}
|
||||
|
||||
$route_file = $dir.'/route-status.json';
|
||||
if (is_readable($route_file))
|
||||
if ($requested_connection_id === 0)
|
||||
{
|
||||
$route = json_decode((string)@file_get_contents($route_file), true);
|
||||
if (is_array($route))
|
||||
$route_file = $dir.'/route-status.json';
|
||||
if (is_readable($route_file))
|
||||
{
|
||||
$route_name = (string)($route['route'] ?? '');
|
||||
$route_timestamp = (int)($route['timestamp'] ?? 0);
|
||||
$route_label = (string)($route['label'] ?? '');
|
||||
$route = json_decode((string)@file_get_contents($route_file), true);
|
||||
if (is_array($route))
|
||||
{
|
||||
$route_name = (string)($route['route'] ?? '');
|
||||
$route_timestamp = (int)($route['timestamp'] ?? 0);
|
||||
$route_label = (string)($route['label'] ?? '');
|
||||
|
||||
if ($route_name === 'webdav')
|
||||
{
|
||||
$route_label = 'WEBDAV PRIMÄR';
|
||||
}
|
||||
elseif ($route_name === 'legacy_fallback')
|
||||
{
|
||||
$route_label = 'LEGACY-FALLBACK AKTIV';
|
||||
}
|
||||
elseif ($route_name === 'failed')
|
||||
{
|
||||
$route_label = 'FEHLER - KEIN ERFOLGREICHER DATENWEG';
|
||||
}
|
||||
elseif ($route_label === '')
|
||||
{
|
||||
$route_label = 'UNBEKANNTER DATENWEG';
|
||||
}
|
||||
if ($route_name === 'webdav')
|
||||
{
|
||||
$route_label = 'WEBDAV PRIMÄR';
|
||||
}
|
||||
elseif ($route_name === 'legacy_fallback')
|
||||
{
|
||||
$route_label = 'LEGACY-FALLBACK AKTIV';
|
||||
}
|
||||
elseif ($route_name === 'failed')
|
||||
{
|
||||
$route_label = 'FEHLER - KEIN ERFOLGREICHER DATENWEG';
|
||||
}
|
||||
elseif ($route_label === '')
|
||||
{
|
||||
$route_label = 'UNBEKANNTER DATENWEG';
|
||||
}
|
||||
|
||||
$route_detail = trim((string)($route['detail'] ?? ''));
|
||||
$latest['route'] = $route_name;
|
||||
$latest['route_label'] = $route_label;
|
||||
$latest['route_timestamp'] = $route_timestamp;
|
||||
$latest['route_time_label'] = $route_timestamp > 0 ? date('d.m.Y H:i:s', $route_timestamp) : 'Nicht verfügbar';
|
||||
$latest['route_detail'] = $route_detail;
|
||||
$route_detail = trim((string)($route['detail'] ?? ''));
|
||||
$latest['route'] = $route_name;
|
||||
$latest['route_label'] = $route_label;
|
||||
$latest['route_timestamp'] = $route_timestamp;
|
||||
$latest['route_time_label'] = $route_timestamp > 0 ? date('d.m.Y H:i:s', $route_timestamp) : 'Nicht verfügbar';
|
||||
$latest['route_detail'] = $route_detail;
|
||||
|
||||
$base_message = trim((string)($latest['message'] ?? ''));
|
||||
$message_parts = array($route_label);
|
||||
if ($route_name !== 'webdav' && $route_detail !== '')
|
||||
{
|
||||
$message_parts[] = $route_detail;
|
||||
$base_message = trim((string)($latest['message'] ?? ''));
|
||||
$message_parts = array($route_label);
|
||||
if ($route_name !== 'webdav' && $route_detail !== '')
|
||||
{
|
||||
$message_parts[] = $route_detail;
|
||||
}
|
||||
if ($base_message !== '')
|
||||
{
|
||||
$message_parts[] = $base_message;
|
||||
}
|
||||
$latest['message'] = implode(' · ', $message_parts);
|
||||
}
|
||||
if ($base_message !== '')
|
||||
{
|
||||
$message_parts[] = $base_message;
|
||||
}
|
||||
$latest['message'] = implode(' · ', $message_parts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((int)$latest['timestamp'] > 0)
|
||||
{
|
||||
$latest['last_run_label'] = date('d.m.Y H:i:s', (int)$latest['timestamp']);
|
||||
}
|
||||
|
||||
$system_file = BRATONIEN_TOOLS_PATH.'include/nc_connector_system.inc.php';
|
||||
if (is_readable($system_file))
|
||||
{
|
||||
@@ -129,4 +133,27 @@ if (is_readable($system_file))
|
||||
}
|
||||
}
|
||||
|
||||
$scheduler_file = PHPWG_ROOT_PATH.'_data/bratonien-tools/nc-connector-scheduler/state.json';
|
||||
if ($requested_connection_id > 0 && is_readable($scheduler_file))
|
||||
{
|
||||
$scheduler = json_decode((string)@file_get_contents($scheduler_file), true);
|
||||
if (
|
||||
is_array($scheduler)
|
||||
&& (int)($scheduler['connection_id'] ?? 0) === $requested_connection_id
|
||||
&& in_array((string)($scheduler['state'] ?? ''), array('queued','running'), true)
|
||||
&& (int)($scheduler['timestamp'] ?? 0) >= (int)($latest['timestamp'] ?? 0)
|
||||
)
|
||||
{
|
||||
$latest['state'] = (string)$scheduler['state'];
|
||||
$latest['message'] = (string)($scheduler['message'] ?? 'NC-Abgleich läuft.');
|
||||
$latest['timestamp'] = (int)($scheduler['timestamp'] ?? time());
|
||||
$latest['error_detail'] = '';
|
||||
}
|
||||
}
|
||||
|
||||
if ((int)$latest['timestamp'] > 0)
|
||||
{
|
||||
$latest['last_run_label'] = date('d.m.Y H:i:s', (int)$latest['timestamp']);
|
||||
}
|
||||
|
||||
echo json_encode($latest, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
"""Build a placeholder-backed local source tree from Nextcloud WebDAV.
|
||||
|
||||
This creates only tiny placeholder files plus a metadata mapping; no Nextcloud
|
||||
original media is downloaded. The authenticated Nextcloud user is never used as
|
||||
an album name.
|
||||
original media is stored locally. The authenticated Nextcloud user is never used
|
||||
as an album name.
|
||||
|
||||
Each placeholder carries the original image dimensions. Dimensions are read
|
||||
from Nextcloud metadata first. If those metadata are unavailable, only the
|
||||
beginning of the original file is read through WebDAV and parsed locally.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -27,8 +31,10 @@ from pathlib import Path, PurePosixPath
|
||||
|
||||
DAV = "DAV:"
|
||||
OC = "http://owncloud.org/ns"
|
||||
NC = "http://nextcloud.org/ns"
|
||||
SUPPORTED_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
|
||||
PLACEHOLDER = base64.b64decode("R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==")
|
||||
DIMENSION_PROBE_BYTES = 4 * 1024 * 1024
|
||||
|
||||
|
||||
def fail(message: str) -> None:
|
||||
@@ -54,6 +60,101 @@ def safe_local_name(name: str) -> str:
|
||||
return name
|
||||
|
||||
|
||||
def parse_dimensions(prop: ET.Element) -> tuple[int, int]:
|
||||
for property_name in ("file-metadata-size", "metadata-photos-size"):
|
||||
raw = prop.findtext(f"{{{NC}}}{property_name}", default="").strip()
|
||||
if not raw:
|
||||
continue
|
||||
try:
|
||||
decoded = json.loads(raw)
|
||||
except (TypeError, ValueError, json.JSONDecodeError):
|
||||
continue
|
||||
if isinstance(decoded, dict) and isinstance(decoded.get("value"), dict):
|
||||
decoded = decoded["value"]
|
||||
if not isinstance(decoded, dict):
|
||||
continue
|
||||
try:
|
||||
width = int(decoded.get("width", 0) or 0)
|
||||
height = int(decoded.get("height", 0) or 0)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
if width > 0 and height > 0:
|
||||
return width, height
|
||||
return 0, 0
|
||||
|
||||
|
||||
def parse_image_header_dimensions(data: bytes) -> tuple[int, int]:
|
||||
if len(data) >= 24 and data.startswith(b"\x89PNG\r\n\x1a\n") and data[12:16] == b"IHDR":
|
||||
return int.from_bytes(data[16:20], "big"), int.from_bytes(data[20:24], "big")
|
||||
|
||||
if len(data) >= 10 and data[:6] in (b"GIF87a", b"GIF89a"):
|
||||
return int.from_bytes(data[6:8], "little"), int.from_bytes(data[8:10], "little")
|
||||
|
||||
if len(data) >= 12 and data[:4] == b"RIFF" and data[8:12] == b"WEBP":
|
||||
chunk = data[12:16]
|
||||
if chunk == b"VP8X" and len(data) >= 30:
|
||||
width = 1 + int.from_bytes(data[24:27], "little")
|
||||
height = 1 + int.from_bytes(data[27:30], "little")
|
||||
return width, height
|
||||
if chunk == b"VP8 " and len(data) >= 30:
|
||||
payload = 20
|
||||
if data[payload + 3:payload + 6] == b"\x9d\x01\x2a":
|
||||
width = int.from_bytes(data[payload + 6:payload + 8], "little") & 0x3FFF
|
||||
height = int.from_bytes(data[payload + 8:payload + 10], "little") & 0x3FFF
|
||||
return width, height
|
||||
if chunk == b"VP8L" and len(data) >= 25 and data[20] == 0x2F:
|
||||
b1, b2, b3, b4 = data[21:25]
|
||||
width = 1 + b1 + ((b2 & 0x3F) << 8)
|
||||
height = 1 + ((b2 & 0xC0) >> 6) + (b3 << 2) + ((b4 & 0x0F) << 10)
|
||||
return width, height
|
||||
|
||||
if len(data) >= 4 and data[:2] == b"\xff\xd8":
|
||||
sof_markers = {
|
||||
0xC0, 0xC1, 0xC2, 0xC3,
|
||||
0xC5, 0xC6, 0xC7,
|
||||
0xC9, 0xCA, 0xCB,
|
||||
0xCD, 0xCE, 0xCF,
|
||||
}
|
||||
pos = 2
|
||||
while pos + 4 <= len(data):
|
||||
if data[pos] != 0xFF:
|
||||
pos += 1
|
||||
continue
|
||||
while pos < len(data) and data[pos] == 0xFF:
|
||||
pos += 1
|
||||
if pos >= len(data):
|
||||
break
|
||||
marker = data[pos]
|
||||
pos += 1
|
||||
if marker in (0xD8, 0xD9) or 0xD0 <= marker <= 0xD7:
|
||||
continue
|
||||
if marker == 0xDA:
|
||||
break
|
||||
if pos + 2 > len(data):
|
||||
break
|
||||
segment_length = int.from_bytes(data[pos:pos + 2], "big")
|
||||
if segment_length < 2:
|
||||
break
|
||||
if marker in sof_markers:
|
||||
if pos + 7 > len(data):
|
||||
break
|
||||
height = int.from_bytes(data[pos + 3:pos + 5], "big")
|
||||
width = int.from_bytes(data[pos + 5:pos + 7], "big")
|
||||
return width, height
|
||||
pos += segment_length
|
||||
|
||||
return 0, 0
|
||||
|
||||
|
||||
def placeholder_bytes(width: int, height: int) -> bytes:
|
||||
if not (1 <= width <= 65535 and 1 <= height <= 65535):
|
||||
fail(f"unsupported image dimensions for placeholder: {width}x{height}")
|
||||
data = bytearray(PLACEHOLDER)
|
||||
data[6:8] = width.to_bytes(2, "little")
|
||||
data[8:10] = height.to_bytes(2, "little")
|
||||
return bytes(data)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def pinned_resolution(host: str, ip: str):
|
||||
host = host.strip("[]").lower()
|
||||
@@ -92,22 +193,50 @@ class WebDavClient:
|
||||
self.auth_header = f"Basic {token}"
|
||||
self.context = ssl.create_default_context()
|
||||
|
||||
def collection_url(self, relative: str) -> str:
|
||||
def file_url(self, relative: str) -> str:
|
||||
user = urllib.parse.quote(self.user, safe="")
|
||||
suffix = quote_path(relative)
|
||||
url = f"{self.base_url}/remote.php/dav/files/{user}/"
|
||||
if suffix:
|
||||
url += suffix + "/"
|
||||
return f"{self.base_url}/remote.php/dav/files/{user}/{suffix}"
|
||||
|
||||
def collection_url(self, relative: str) -> str:
|
||||
url = self.file_url(relative)
|
||||
if not url.endswith("/"):
|
||||
url += "/"
|
||||
return url
|
||||
|
||||
def probe_dimensions(self, relative: str) -> tuple[int, int]:
|
||||
request = urllib.request.Request(self.file_url(relative), method="GET")
|
||||
request.add_header("Authorization", self.auth_header)
|
||||
request.add_header("Range", f"bytes=0-{DIMENSION_PROBE_BYTES - 1}")
|
||||
try:
|
||||
with pinned_resolution(self.host, self.connect_ip):
|
||||
with urllib.request.urlopen(request, timeout=self.timeout, context=self.context) as response:
|
||||
if response.status not in (200, 206):
|
||||
fail(f"Nextcloud image header returned HTTP {response.status}")
|
||||
data = response.read(DIMENSION_PROBE_BYTES)
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code in {401, 403}:
|
||||
fail("Nextcloud rejected the WebDAV credentials or file access")
|
||||
fail(f"Nextcloud image header request failed with HTTP {error.code}")
|
||||
except urllib.error.URLError as error:
|
||||
fail(f"Nextcloud WebDAV is unreachable while reading image dimensions: {error.reason}")
|
||||
|
||||
width, height = parse_image_header_dimensions(data)
|
||||
if width < 1 or height < 1:
|
||||
fail(f"original image dimensions could not be read from Nextcloud file header: {relative}")
|
||||
return width, height
|
||||
|
||||
def list_collection(self, relative: str) -> tuple[dict[str, object], list[dict[str, object]]]:
|
||||
relative = validate_relative(relative)
|
||||
url = self.collection_url(relative)
|
||||
body = (
|
||||
'<?xml version="1.0"?>'
|
||||
'<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">'
|
||||
'<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" '
|
||||
'xmlns:nc="http://nextcloud.org/ns">'
|
||||
'<d:prop><d:displayname/><d:resourcetype/><d:getcontenttype/>'
|
||||
'<d:getcontentlength/><d:getetag/><oc:fileid/></d:prop></d:propfind>'
|
||||
'<d:getcontentlength/><d:getetag/><oc:fileid/>'
|
||||
'<nc:file-metadata-size/><nc:metadata-photos-size/>'
|
||||
'</d:prop></d:propfind>'
|
||||
).encode("utf-8")
|
||||
request = urllib.request.Request(url, data=body, method="PROPFIND")
|
||||
request.add_header("Authorization", self.auth_header)
|
||||
@@ -151,6 +280,7 @@ class WebDavClient:
|
||||
fileid_text = prop.findtext(f"{{{OC}}}fileid", default="").strip()
|
||||
resource_type = prop.find(f"{{{DAV}}}resourcetype")
|
||||
is_dir = resource_type is not None and resource_type.find(f"{{{DAV}}}collection") is not None
|
||||
width, height = parse_dimensions(prop)
|
||||
item = {
|
||||
"display_name": display,
|
||||
"fileid": int(fileid_text) if fileid_text.isdigit() else 0,
|
||||
@@ -158,6 +288,8 @@ class WebDavClient:
|
||||
"content_type": prop.findtext(f"{{{DAV}}}getcontenttype", default=""),
|
||||
"size": int(prop.findtext(f"{{{DAV}}}getcontentlength", default="0") or 0),
|
||||
"etag": prop.findtext(f"{{{DAV}}}getetag", default="").strip('"'),
|
||||
"width": width,
|
||||
"height": height,
|
||||
}
|
||||
if href_path == base_path:
|
||||
current = item
|
||||
@@ -169,18 +301,17 @@ class WebDavClient:
|
||||
return current, children
|
||||
|
||||
|
||||
def link_placeholder(seed: Path, target: Path) -> None:
|
||||
def write_placeholder(target: Path, width: int, height: int) -> None:
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
os.link(seed, target)
|
||||
except OSError:
|
||||
target.write_bytes(PLACEHOLDER)
|
||||
target.write_bytes(placeholder_bytes(width, height))
|
||||
os.chmod(target, 0o644)
|
||||
|
||||
|
||||
def build_root(client: WebDavClient, remote_root: str, local_root: Path, seed: Path, mapping: dict[str, dict[str, object]]) -> tuple[int, int, int]:
|
||||
def build_root(client: WebDavClient, remote_root: str, local_root: Path, mapping: dict[str, dict[str, object]]) -> tuple[int, int, int, int]:
|
||||
files = 0
|
||||
folders = 0
|
||||
skipped = 0
|
||||
probed = 0
|
||||
stack: list[tuple[str, Path]] = [(validate_relative(remote_root), local_root)]
|
||||
visited: set[str] = set()
|
||||
|
||||
@@ -210,7 +341,16 @@ def build_root(client: WebDavClient, remote_root: str, local_root: Path, seed: P
|
||||
if extension not in SUPPORTED_IMAGE_EXTENSIONS:
|
||||
skipped += 1
|
||||
continue
|
||||
link_placeholder(seed, child_local)
|
||||
|
||||
width = int(child.get("width", 0) or 0)
|
||||
height = int(child.get("height", 0) or 0)
|
||||
dimension_source = "metadata"
|
||||
if width < 1 or height < 1:
|
||||
width, height = client.probe_dimensions(child_remote)
|
||||
dimension_source = "header"
|
||||
probed += 1
|
||||
|
||||
write_placeholder(child_local, width, height)
|
||||
files += 1
|
||||
mapping[str(child_local)] = {
|
||||
"kind": "file",
|
||||
@@ -220,8 +360,11 @@ def build_root(client: WebDavClient, remote_root: str, local_root: Path, seed: P
|
||||
"content_type": str(child.get("content_type", "")),
|
||||
"size": int(child.get("size", 0)),
|
||||
"etag": str(child.get("etag", "")),
|
||||
"width": width,
|
||||
"height": height,
|
||||
"dimension_source": dimension_source,
|
||||
}
|
||||
return files, folders, skipped
|
||||
return files, folders, skipped, probed
|
||||
|
||||
|
||||
def atomic_json(path: Path, payload: object) -> None:
|
||||
@@ -265,10 +408,7 @@ def main() -> int:
|
||||
source_dir = args.source_dir.resolve()
|
||||
staging = source_dir.with_name(f".{source_dir.name}.next")
|
||||
previous = source_dir.with_name(f".{source_dir.name}.previous")
|
||||
seed = source_dir.parent / ".bratonien-webdav-placeholder.gif"
|
||||
source_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
seed.write_bytes(PLACEHOLDER)
|
||||
os.chmod(seed, 0o644)
|
||||
if staging.exists():
|
||||
shutil.rmtree(staging)
|
||||
staging.mkdir(parents=True)
|
||||
@@ -276,7 +416,7 @@ def main() -> int:
|
||||
client = WebDavClient(args.base_url, args.user, password, max(1, args.timeout), args.connect_ip)
|
||||
mapping: dict[str, dict[str, object]] = {}
|
||||
manifest: list[str] = []
|
||||
total_files = total_folders = total_skipped = 0
|
||||
total_files = total_folders = total_skipped = total_probed = 0
|
||||
used_fileids: set[int] = set()
|
||||
|
||||
for remote_root_raw in args.root:
|
||||
@@ -288,10 +428,11 @@ def main() -> int:
|
||||
used_fileids.add(fileid)
|
||||
local_name = f"root-{fileid}"
|
||||
local_root = staging / local_name
|
||||
files, folders, skipped = build_root(client, remote_root, local_root, seed, mapping)
|
||||
files, folders, skipped, probed = build_root(client, remote_root, local_root, mapping)
|
||||
total_files += files
|
||||
total_folders += folders
|
||||
total_skipped += skipped
|
||||
total_probed += probed
|
||||
|
||||
if remote_root == "":
|
||||
for child in sorted(root_children, key=lambda item: str(item.get("display_name", "")).casefold()):
|
||||
@@ -333,7 +474,7 @@ def main() -> int:
|
||||
|
||||
atomic_text(args.manifest, "\n".join(manifest) + "\n")
|
||||
atomic_json(args.mapping, {
|
||||
"version": 1,
|
||||
"version": 3,
|
||||
"base_url": args.base_url.rstrip("/"),
|
||||
"connect_ip": client.connect_ip,
|
||||
"user": args.user,
|
||||
@@ -344,6 +485,7 @@ def main() -> int:
|
||||
"files": total_files,
|
||||
"folders": total_folders,
|
||||
"skipped": total_skipped,
|
||||
"dimension_header_probes": total_probed,
|
||||
"connect_ip": client.connect_ip,
|
||||
"source_dir": str(source_dir),
|
||||
"manifest": str(args.manifest),
|
||||
|
||||
@@ -78,7 +78,9 @@ done < "$WEBDAV_ROOTS_FILE"
|
||||
WEBDAV_CONNECT_IP="$(php "$SCRIPT_DIR/lib/resolve-nextcloud-target.php" "$WEBDAV_BASE_URL")"
|
||||
[[ -n "$WEBDAV_CONNECT_IP" ]] || { write_status error "Nextcloud-Zieladresse konnte nicht ermittelt werden"; exit 1; }
|
||||
|
||||
python3 "$SCRIPT_DIR/lib/build_webdav_placeholder_source.py" \
|
||||
PLACEHOLDER_OUTPUT=""
|
||||
PLACEHOLDER_EXIT=0
|
||||
if PLACEHOLDER_OUTPUT="$(python3 "$SCRIPT_DIR/lib/build_webdav_placeholder_source.py" \
|
||||
--base-url "$WEBDAV_BASE_URL" \
|
||||
--connect-ip "$WEBDAV_CONNECT_IP" \
|
||||
--user "$WEBDAV_USER" \
|
||||
@@ -86,12 +88,42 @@ python3 "$SCRIPT_DIR/lib/build_webdav_placeholder_source.py" \
|
||||
"${ROOT_ARGS[@]}" \
|
||||
--source-dir "$WEBDAV_SOURCE_DIR" \
|
||||
--manifest "$MANIFEST" \
|
||||
--mapping "$WEBDAV_MAPPING_FILE"
|
||||
--mapping "$WEBDAV_MAPPING_FILE" 2>&1)"; then
|
||||
PLACEHOLDER_EXIT=0
|
||||
else
|
||||
PLACEHOLDER_EXIT=$?
|
||||
fi
|
||||
[[ -z "$PLACEHOLDER_OUTPUT" ]] || printf '%s\n' "$PLACEHOLDER_OUTPUT"
|
||||
if [[ "$PLACEHOLDER_EXIT" -ne 0 ]]; then
|
||||
DETAIL="Exit-Code: $PLACEHOLDER_EXIT"
|
||||
if [[ -n "$PLACEHOLDER_OUTPUT" ]]; then
|
||||
DETAIL+="; Ausgabe: $(printf '%s\n' "$PLACEHOLDER_OUTPUT" | tail -n 20 | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]//; s/[[:space:]]$//')"
|
||||
fi
|
||||
trap - ERR
|
||||
write_status error "WebDAV-Shadow-Tree fehlgeschlagen" "$DETAIL"
|
||||
exit "$PLACEHOLDER_EXIT"
|
||||
fi
|
||||
|
||||
python3 "$SCRIPT_DIR/lib/shadow_tree.py" \
|
||||
SHADOW_OUTPUT=""
|
||||
SHADOW_EXIT=0
|
||||
if SHADOW_OUTPUT="$(python3 "$SCRIPT_DIR/lib/shadow_tree.py" \
|
||||
--manifest "$MANIFEST" \
|
||||
--destination "$GALLERY_ROOT" \
|
||||
--state "$SHADOW_MAP_FILE"
|
||||
--state "$SHADOW_MAP_FILE" 2>&1)"; then
|
||||
SHADOW_EXIT=0
|
||||
else
|
||||
SHADOW_EXIT=$?
|
||||
fi
|
||||
[[ -z "$SHADOW_OUTPUT" ]] || printf '%s\n' "$SHADOW_OUTPUT"
|
||||
if [[ "$SHADOW_EXIT" -ne 0 ]]; then
|
||||
DETAIL="Exit-Code: $SHADOW_EXIT"
|
||||
if [[ -n "$SHADOW_OUTPUT" ]]; then
|
||||
DETAIL+="; Ausgabe: $(printf '%s\n' "$SHADOW_OUTPUT" | tail -n 20 | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]//; s/[[:space:]]$//')"
|
||||
fi
|
||||
trap - ERR
|
||||
write_status error "WebDAV-Shadow-Tree fehlgeschlagen" "$DETAIL"
|
||||
exit "$SHADOW_EXIT"
|
||||
fi
|
||||
|
||||
trap - ERR
|
||||
|
||||
|
||||
@@ -52,6 +52,18 @@ if (!pwg_db_num_rows($access_result)) bratonien_tools_webdav_image_abort(403, 'K
|
||||
$source = bratonien_tools_webdav_image_source_info($image_id);
|
||||
if (!$source) bratonien_tools_webdav_image_abort(404, 'Keine WebDAV-Quelle für dieses Bild gefunden.');
|
||||
|
||||
if (!empty($_GET['ajaxload']))
|
||||
{
|
||||
$preview = !empty($_GET['preview']);
|
||||
$final_url = bratonien_tools_webdav_image_url($image_id, $preview);
|
||||
if (!$final_url) bratonien_tools_webdav_image_abort(404, 'Keine WebDAV-Bild-URL verfügbar.');
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('Cache-Control: no-store, max-age=0');
|
||||
echo json_encode(array('url'=>$final_url), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!empty($_GET['preview']))
|
||||
{
|
||||
$preview = bratonien_tools_webdav_preview_path($source);
|
||||
@@ -121,7 +133,7 @@ $options = array(
|
||||
CURLOPT_USERPWD => $user.':'.$password,
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_FAILONERROR => false,
|
||||
CURLOPT_USERAGENT => 'Bratonien-Tools-WebDAV-Image/0.9.7.16',
|
||||
CURLOPT_USERAGENT => 'Bratonien-Tools-WebDAV-Image/0.9.7.24',
|
||||
CURLOPT_HEADERFUNCTION => function($ch, $line)
|
||||
{
|
||||
$length = strlen($line);
|
||||
|
||||
Reference in New Issue
Block a user