📤 Fotos hochladen
Fotos hier reinziehen
oder hier klicken zum Auswählen
JPG, PNG, HEIC • bis zu 20 MB
Wird hochgeladen…
🗂️ Meine Fotos
Noch keine Fotos
Lad dein erstes Foto hoch!
const express = require('express'); const multer = require('multer'); const sharp = require('sharp'); const path = require('path'); const fs = require('fs'); const { exec } = require('child_process'); const app = express(); const PORT = 3001; const IMAGES_DIR = path.join(__dirname, 'static/gallery/images'); const HERO_DIR = path.join(__dirname, 'static/hero'); const UEBERMICH_IMG_DIR = path.join(__dirname, 'static/uebermich'); const DATA_FILE = path.join(__dirname, 'data/gallery.json'); const HOMEPAGE_FILE = path.join(__dirname, 'data/homepage.json'); const CATEGORIES_FILE = path.join(__dirname, 'data/categories.json'); const UEBERMICH_FILE = path.join(__dirname, 'data/uebermich.json'); const ERFOLGE_FILE = path.join(__dirname, 'data/erfolge.json'); const ERFOLGE_IMG_DIR = path.join(__dirname, 'static/erfolge-img'); const GALERIE_PAGE_FILE = path.join(__dirname, 'data/galerie.json'); const GAESTEBUCH_FILE = path.join(__dirname, 'data/gaestebuch.json'); const GLOBAL_FILE = path.join(__dirname, 'data/global.json'); const GAESTEBUCH_IMG_DIR = path.join(__dirname, 'static/gaestebuch-img'); if (!fs.existsSync(UEBERMICH_IMG_DIR)) fs.mkdirSync(UEBERMICH_IMG_DIR, { recursive: true }); if (!fs.existsSync(HERO_DIR)) fs.mkdirSync(HERO_DIR, { recursive: true }); if (!fs.existsSync(ERFOLGE_IMG_DIR)) fs.mkdirSync(ERFOLGE_IMG_DIR, { recursive: true }); if (!fs.existsSync(GAESTEBUCH_IMG_DIR)) fs.mkdirSync(GAESTEBUCH_IMG_DIR, { recursive: true }); app.use(express.json()); app.use('/images', express.static(IMAGES_DIR)); // Multer: temporärer Speicher, dann sharp übernimmt const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 20 * 1024 * 1024 }, fileFilter: (req, file, cb) => { if (file.mimetype.startsWith('image/')) cb(null, true); else cb(new Error('Nur Bilder erlaubt!')); } }); function readGallery() { return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')); } function writeGallery(data) { fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2)); } // Upload app.post('/api/upload', upload.array('photos', 20), async (req, res) => { try { const gallery = readGallery(); const added = []; for (const file of req.files) { const id = Date.now() + '-' + Math.random().toString(36).slice(2, 7); const filename = id + '.webp'; const thumbname = id + '-thumb.webp'; // Vollbild (max 1200px) await sharp(file.buffer) .resize(1200, 1200, { fit: 'inside', withoutEnlargement: true }) .webp({ quality: 85 }) .toFile(path.join(IMAGES_DIR, filename)); // Thumbnail (400px) await sharp(file.buffer) .resize(400, 400, { fit: 'cover' }) .webp({ quality: 80 }) .toFile(path.join(IMAGES_DIR, thumbname)); const photo = { id, filename, thumb: thumbname, title: req.body.title || file.originalname.replace(/\.[^.]+$/, ''), kategorie: req.body.kategorie || 'Training', datum: new Date().toISOString().slice(0, 10) }; gallery.photos.unshift(photo); added.push(photo); } writeGallery(gallery); res.json({ ok: true, added }); } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); // Foto-Metadaten updaten app.put('/api/photo/:id', (req, res) => { const gallery = readGallery(); const photo = gallery.photos.find(p => p.id === req.params.id); if (!photo) return res.status(404).json({ ok: false }); if (req.body.title !== undefined) photo.title = req.body.title; if (req.body.kategorie !== undefined) photo.kategorie = req.body.kategorie; writeGallery(gallery); res.json({ ok: true, photo }); }); // Foto löschen app.delete('/api/photo/:id', (req, res) => { const gallery = readGallery(); const idx = gallery.photos.findIndex(p => p.id === req.params.id); if (idx === -1) return res.status(404).json({ ok: false }); const [photo] = gallery.photos.splice(idx, 1); [photo.filename, photo.thumb].forEach(f => { const fp = path.join(IMAGES_DIR, f); if (fs.existsSync(fp)) fs.unlinkSync(fp); }); writeGallery(gallery); res.json({ ok: true }); }); // Alle Fotos app.get('/api/photos', (req, res) => { res.json(readGallery()); }); // ── Kategorien API ──────────────────────────────────────────── function readCategories() { if (!fs.existsSync(CATEGORIES_FILE)) return ['Training', 'Wettkämpfe', 'Gürtelprüfungen']; return JSON.parse(fs.readFileSync(CATEGORIES_FILE, 'utf8')); } function writeCategories(cats) { fs.writeFileSync(CATEGORIES_FILE, JSON.stringify(cats, null, 2)); } app.get('/api/categories', (req, res) => { res.json(readCategories()); }); app.post('/api/categories', (req, res) => { const name = (req.body.name || '').trim(); if (!name) return res.status(400).json({ ok: false, error: 'Name fehlt' }); const cats = readCategories(); if (cats.includes(name)) return res.status(409).json({ ok: false, error: 'Existiert bereits' }); cats.push(name); writeCategories(cats); res.json({ ok: true, categories: cats }); }); app.delete('/api/categories/:name', (req, res) => { const cats = readCategories().filter(c => c !== req.params.name); writeCategories(cats); res.json({ ok: true, categories: cats }); }); // ── Homepage API ────────────────────────────────────────────── function readHomepage() { return JSON.parse(fs.readFileSync(HOMEPAGE_FILE, 'utf8')); } function writeHomepage(data) { fs.writeFileSync(HOMEPAGE_FILE, JSON.stringify(data, null, 2)); } app.get('/api/homepage', (req, res) => { res.json(readHomepage()); }); function rebuildAndDeploy() { const SSH_KEY = '/Users/jessi/.ssh/vpsserver/vpsserver'; const cmd = `cd /Users/jessi/karatehp && hugo --minify && SSH_ASKPASS_REQUIRE=never ssh-add ${SSH_KEY} <<< "bonzeikiller" 2>/dev/null; rsync -az --delete -e "ssh -o StrictHostKeyChecking=no -i ${SSH_KEY}" /Users/jessi/karatehp/public/ root@217.160.212.198:/var/www/emy.bonzeipunk.de/`; exec(cmd, { shell: '/bin/bash' }, (err) => { if (err) console.error('Deploy-Fehler:', err.message); else console.log('✓ Deploy erfolgreich'); }); } app.put('/api/homepage', (req, res) => { const hp = readHomepage(); if (req.body.siteTitle !== undefined) hp.siteTitle = req.body.siteTitle; if (req.body.badge !== undefined) hp.hero.badge = req.body.badge; if (req.body.description !== undefined) hp.hero.description = req.body.description; if (req.body.stats !== undefined) hp.stats = req.body.stats; if (req.body.hero_karte !== undefined) hp.hero_karte = req.body.hero_karte; if (req.body.pruefung_karte !== undefined) hp.pruefung_karte = req.body.pruefung_karte; if (req.body.dojo_karte !== undefined) hp.dojo_karte = req.body.dojo_karte; if (req.body.cta !== undefined) hp.cta = req.body.cta; writeHomepage(hp); res.json({ ok: true }); rebuildAndDeploy(); }); app.post('/api/homepage/image', upload.single('image'), async (req, res) => { try { const filename = 'hero.webp'; await sharp(req.file.buffer) .resize(1200, 1200, { fit: 'inside', withoutEnlargement: true }) .webp({ quality: 88 }) .toFile(path.join(HERO_DIR, filename)); const hp = readHomepage(); hp.hero.image = filename; writeHomepage(hp); res.json({ ok: true, image: filename }); rebuildAndDeploy(); } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); app.use('/hero', express.static(HERO_DIR)); app.use('/uebermich', express.static(UEBERMICH_IMG_DIR)); app.use('/erfolge-img', express.static(ERFOLGE_IMG_DIR)); app.use('/gaestebuch-img', express.static(GAESTEBUCH_IMG_DIR)); // Über mich API function readUebermich() { return JSON.parse(fs.readFileSync(UEBERMICH_FILE, 'utf8')); } function writeUebermich(data) { fs.writeFileSync(UEBERMICH_FILE, JSON.stringify(data, null, 2)); } app.get('/api/uebermich', (req, res) => { res.json(readUebermich()); }); app.put('/api/uebermich', (req, res) => { const data = req.body; writeUebermich(data); res.json({ ok: true }); rebuildAndDeploy(); }); app.post('/api/uebermich/image', upload.single('image'), async (req, res) => { try { const filename = 'portrait.webp'; await sharp(req.file.buffer) .resize(800, 1000, { fit: 'cover', position: 'top' }) .webp({ quality: 88 }) .toFile(path.join(UEBERMICH_IMG_DIR, filename)); const data = readUebermich(); data.hero.image = filename; writeUebermich(data); res.json({ ok: true, image: filename }); rebuildAndDeploy(); } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); // ── Erfolge API ─────────────────────────────────────────────── function readErfolge() { return JSON.parse(fs.readFileSync(ERFOLGE_FILE, 'utf8')); } function writeErfolge(data) { fs.writeFileSync(ERFOLGE_FILE, JSON.stringify(data, null, 2)); } app.get('/api/erfolge', (req, res) => { res.json(readErfolge()); }); app.put('/api/erfolge', (req, res) => { writeErfolge(req.body); res.json({ ok: true }); rebuildAndDeploy(); }); app.post('/api/erfolge/image', upload.single('image'), async (req, res) => { try { const filename = 'hero.webp'; await sharp(req.file.buffer) .resize(1200, 1500, { fit: 'inside', withoutEnlargement: true }) .webp({ quality: 88 }) .toFile(path.join(ERFOLGE_IMG_DIR, filename)); const data = readErfolge(); data.hero.image = filename; writeErfolge(data); res.json({ ok: true, image: filename }); rebuildAndDeploy(); } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); // ── Galerie Seiten-Texte API ────────────────────────────────── app.get('/api/galerie', (req, res) => { res.json(JSON.parse(fs.readFileSync(GALERIE_PAGE_FILE, 'utf8'))); }); app.put('/api/galerie', (req, res) => { fs.writeFileSync(GALERIE_PAGE_FILE, JSON.stringify(req.body, null, 2)); res.json({ ok: true }); rebuildAndDeploy(); }); // ── Gästebuch API ───────────────────────────────────────────── app.get('/api/gaestebuch', (req, res) => { res.json(JSON.parse(fs.readFileSync(GAESTEBUCH_FILE, 'utf8'))); }); app.put('/api/gaestebuch', (req, res) => { fs.writeFileSync(GAESTEBUCH_FILE, JSON.stringify(req.body, null, 2)); res.json({ ok: true }); rebuildAndDeploy(); }); app.post('/api/gaestebuch/image', upload.single('image'), async (req, res) => { try { const filename = 'hero.webp'; await sharp(req.file.buffer) .resize(1400, 600, { fit: 'cover', position: 'center' }) .webp({ quality: 88 }) .toFile(path.join(GAESTEBUCH_IMG_DIR, filename)); const data = JSON.parse(fs.readFileSync(GAESTEBUCH_FILE, 'utf8')); data.hero.image = filename; fs.writeFileSync(GAESTEBUCH_FILE, JSON.stringify(data, null, 2)); res.json({ ok: true, image: filename }); rebuildAndDeploy(); } catch (e) { res.status(500).json({ ok: false, error: e.message }); } }); // ── Global (Social Links) API ───────────────────────────────── app.get('/api/global', (req, res) => { res.json(JSON.parse(fs.readFileSync(GLOBAL_FILE, 'utf8'))); }); app.put('/api/global', (req, res) => { fs.writeFileSync(GLOBAL_FILE, JSON.stringify(req.body, null, 2)); res.json({ ok: true }); rebuildAndDeploy(); }); // Admin-UI app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'admin.html')); }); app.get('/__old', (req, res) => { res.send(`
Galerie verwalten
Fotos hier reinziehen
oder hier klicken zum Auswählen
JPG, PNG, HEIC • bis zu 20 MB
Wird hochgeladen…
Noch keine Fotos
Lad dein erstes Foto hoch!