feat: WebDAV Sync mit vollständiger Ordnerstruktur (user/ordner/unterordner/datei)

This commit is contained in:
2026-06-14 01:46:51 +02:00
parent 5ded22aef7
commit e2d28e285c
2 changed files with 45 additions and 5 deletions

View File

@@ -236,11 +236,28 @@ router.post('/webdav/sync', authenticate, requireAdmin, async (req, res) => {
const cfg = webdav.getConfig();
if (!cfg.enabled) return res.status(400).json({ error: 'WebDAV nicht aktiviert' });
// Alle Dateien aus DB holen mit Username
// Ordnerpfad rekursiv aus DB auflösen
function getFolderPath(folderId) {
if (!folderId) return '';
const parts = [];
let current = folderId;
const visited = new Set();
while (current) {
if (visited.has(current)) break;
visited.add(current);
const folder = db.prepare('SELECT id, name, parent_id FROM folders WHERE id=?').get(current);
if (!folder) break;
parts.unshift(folder.name.replace(/[/\\]/g, '_'));
current = folder.parent_id;
}
return parts.join('/');
}
// Alle Dateien aus DB holen mit Username und folder_id
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
ORDER BY f.user_id, f.folder_id, f.id
`).all();
const results = { ok: 0, failed: 0, errors: [] };
@@ -254,7 +271,11 @@ router.post('/webdav/sync', authenticate, requireAdmin, async (req, res) => {
}
try {
const buf = fs.readFileSync(localPath);
const davPath = webdav.userPath(cfg, file.username) + '/' + file.originalname;
const folderPath = getFolderPath(file.folder_id);
const userBase = webdav.userPath(cfg, file.username);
const davPath = folderPath
? `${userBase}/${folderPath}/${file.originalname}`
: `${userBase}/${file.originalname}`;
await webdav.uploadFile(buf, davPath, file.mimetype || 'application/octet-stream');
results.ok++;
} catch(e) {