From 0eb2ca264129963d007adf62c34b22ea90ddf62b Mon Sep 17 00:00:00 2001 From: Terranom674 Date: Mon, 17 Aug 2026 10:29:08 +0200 Subject: [PATCH] Add NC Connector legacy migration helper --- nc-connector-migrate.php | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 nc-connector-migrate.php diff --git a/nc-connector-migrate.php b/nc-connector-migrate.php new file mode 100644 index 0000000..83aaf52 --- /dev/null +++ b/nc-connector-migrate.php @@ -0,0 +1,133 @@ +#!/usr/bin/env php += 2) + { + $first = $value[0]; + $last = $value[strlen($value)-1]; + if (($first === '"' && $last === '"') || ($first === "'" && $last === "'")) + { + $value = substr($value, 1, -1); + } + } + $config[$matches[1]] = $value; + } + + return $config; +} + +function readStorages($path) +{ + $storages = array(); + if (!is_readable($path)) + { + return $storages; + } + + foreach (file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) + { + if ($line === '' || $line[0] === '#') + { + continue; + } + $parts = explode("\t", $line); + if (count($parts) !== 3) + { + continue; + } + $storages[] = array( + 'storage_id' => $parts[0], + 'source_prefix' => $parts[1], + 'local_mount' => $parts[2], + ); + } + + return $storages; +} + +try +{ + $config = readLegacyConfig($configPath); + $passwordPath = isset($config['NC_DB_PASSWORD_FILE']) ? $config['NC_DB_PASSWORD_FILE'] : '/etc/piwigo-sync/nextcloud-db-password'; + if (!is_readable($passwordPath)) + { + throw new RuntimeException('Nextcloud-Datenbankpasswort nicht lesbar: '.$passwordPath); + } + + $password = trim((string)file_get_contents($passwordPath)); + if ($password === '') + { + throw new RuntimeException('Nextcloud-Datenbankpasswort ist leer.'); + } + + $bundle = array( + 'format' => 1, + 'created_at' => gmdate('c'), + 'config' => $config, + 'db_password' => $password, + 'storages' => readStorages($storagePath), + ); + + $json = json_encode($bundle, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if (!is_string($json)) + { + throw new RuntimeException('Migrationspaket konnte nicht erzeugt werden.'); + } + + if (file_put_contents($bundlePath, $json."\n", LOCK_EX) === false) + { + throw new RuntimeException('Migrationspaket konnte nicht geschrieben werden: '.$bundlePath); + } + + @chmod($bundlePath, 0600); + if (!@chown($bundlePath, 'www-data')) + { + @unlink($bundlePath); + throw new RuntimeException('Migrationspaket konnte nicht sicher an www-data uebergeben werden.'); + } + @chgrp($bundlePath, 'www-data'); + + echo "Migrationspaket wurde bereitgestellt.\n"; + echo "Jetzt in Piwigo -> Bratonien Tools -> NC Connector wechseln und 'Bestehende Verbindung importieren' ausfuehren.\n"; + echo "Der laufende piwigo-sync wurde nicht veraendert oder gestoppt.\n"; +} +catch (Throwable $e) +{ + fwrite(STDERR, 'Fehler: '.$e->getMessage()."\n"); + exit(1); +}