import type { APIRoute } from 'astro'; import posixpath from 'node:path/posix'; import { loadConfig, cleanupTmp } from '../../../lib/picdrop'; export const prerender = false; const json = (b: unknown, s = 200) => new Response(JSON.stringify(b), { status: s, headers: { 'Content-Type': 'application/json' } }); /** Diagnose: listet einen Picdrop-Ordner (Standard: Basisordner). Body { path? } * Aufräumen: Body { action:'cleanup_tmp', gallery } entfernt liegengebliebene .tmp-Dateien. */ export const POST: APIRoute = async ({ request }) => { const b = await request.json().catch(() => ({} as any)); const cfg = await loadConfig(); if (!cfg) return json({ error: 'Picdrop nicht konfiguriert.' }, 400); if (b.action === 'cleanup_tmp') { try { const removed = await cleanupTmp(cfg, b.gallery || ''); return json({ cleaned: removed }); } catch (e: any) { return json({ error: e?.message || 'Aufräumen fehlgeschlagen.' }, 500); } } const path = b.path || cfg.basePath || '/'; try { if (cfg.protocol === 'sftp') { const SftpClient = (await import('ssh2-sftp-client')).default; const c = new SftpClient(); await c.connect({ host: cfg.host, port: cfg.port, username: cfg.user, password: cfg.password, readyTimeout: 15000 }); try { const list = await c.list(path); return json({ path, entries: list.map((e: any) => ({ name: e.name, type: e.type, size: e.size })) }); } finally { await c.end(); } } else { const { Client } = await import('basic-ftp'); const c = new Client(15000); await c.access({ host: cfg.host, port: cfg.port, user: cfg.user, password: cfg.password, secure: true }); try { const list = await c.list(path); return json({ path, entries: list.map((e: any) => ({ name: e.name, type: e.type, size: e.size })) }); } finally { c.close(); } } } catch (e: any) { return json({ path, error: e?.message || 'Listing fehlgeschlagen.' }); } };