feat: WebDAV Sync-Button - alle lokalen Dateien auf Synology kopieren

This commit is contained in:
2026-06-14 01:30:12 +02:00
parent d564323c00
commit 5ded22aef7
2 changed files with 86 additions and 1 deletions

View File

@@ -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;