diff --git a/backend/src/routes/admin.js b/backend/src/routes/admin.js index b14d496..ee900fc 100644 --- a/backend/src/routes/admin.js +++ b/backend/src/routes/admin.js @@ -226,4 +226,44 @@ router.post('/webdav/test', authenticate, requireAdmin, async (req, res) => { } }); +// ── WebDAV Sync: alle lokalen Dateien auf NAS kopieren ──────────────────── +router.post('/webdav/sync', authenticate, requireAdmin, async (req, res) => { + const webdav = require('../tools/dateien/webdav'); + const path = require('path'); + const fs = require('fs'); + const UPLOAD_DIR = process.env.UPLOAD_DIR || '/data/uploads'; + + const cfg = webdav.getConfig(); + if (!cfg.enabled) return res.status(400).json({ error: 'WebDAV nicht aktiviert' }); + + // Alle Dateien aus DB holen mit Username + const files = db.prepare(` + SELECT f.*, u.username FROM files f + JOIN users u ON u.id = f.user_id + ORDER BY f.user_id, f.id + `).all(); + + const results = { ok: 0, failed: 0, errors: [] }; + + for (const file of files) { + const localPath = path.join(UPLOAD_DIR, file.filename); + if (!fs.existsSync(localPath)) { + results.failed++; + results.errors.push(`${file.originalname}: lokale Datei fehlt`); + continue; + } + try { + const buf = fs.readFileSync(localPath); + const davPath = webdav.userPath(cfg, file.username) + '/' + file.originalname; + await webdav.uploadFile(buf, davPath, file.mimetype || 'application/octet-stream'); + results.ok++; + } catch(e) { + results.failed++; + results.errors.push(`${file.originalname}: ${e.message}`); + } + } + + res.json({ total: files.length, ...results }); +}); + module.exports = router; diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 724f43d..3107514 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -2702,6 +2702,8 @@ function WebDavSettings({ toast }) { const [cfg, setCfg] = useState({ url:'', user:'', password:'', basePath:'/dickendock', enabled:false }); const [testing, setTesting] = useState(false); const [saving, setSaving] = useState(false); + const [syncing, setSyncing] = useState(false); + const [syncResult, setSyncResult] = useState(null); const [showPw, setShowPw] = useState(false); useEffect(() => { @@ -2780,7 +2782,7 @@ function WebDavSettings({ toast }) { Synology WebDAV aktivieren: Systemsteuerung → Dateidienste → WebDAV -
+
+ + {/* Sync Button */} + {cfg.enabled && ( +
+
+ BESTEHENDE DATEIEN SYNCHRONISIEREN +
+
+ Kopiert alle lokalen Dateien aus dem Docker-Container auf die Synology. + Bereits vorhandene Dateien werden überschrieben. +
+ + + {syncResult && !syncResult.error && ( +
+
+ ✓ Sync abgeschlossen: {syncResult.ok}/{syncResult.total} erfolgreich +
+ {syncResult.failed > 0 && ( +
+ ✗ {syncResult.failed} fehlgeschlagen + {syncResult.errors?.slice(0,5).map((e,i) => ( +
· {e}
+ ))} +
+ )} +
+ )} + {syncResult?.error && ( +
✗ {syncResult.error}
+ )} +
+ )}
); }