feat: YouTube/Instagram/MP3 download with modal + yt-dlp support

Sync from gaming-hub soundboard plugin:
- Add yt-dlp URL detection (YouTube, Instagram) + direct MP3 support
- downloadWithYtDlp() with verbose logging, error detection, fallback scan
- handleUrlDownload() shared logic with custom filename + rename
- Download modal: filename input, progress spinner, success/error phases
- URL type badges (YT/IG/MP3) in toolbar input
- Auto-prepend https:// for URLs without protocol
- Fix Dockerfile: yt-dlp_linux standalone binary (no Python needed)
- download-url route (admin-only, save without playing)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Daniel 2026-03-07 00:13:48 +01:00
parent 4875747dc5
commit 3c8ad63f99
5 changed files with 579 additions and 61 deletions

View file

@ -1,6 +1,6 @@
import React, { useEffect, useMemo, useRef, useState, useCallback } from 'react';
import {
fetchChannels, fetchSounds, fetchAnalytics, playSound, playUrl, setVolumeLive, getVolume,
fetchChannels, fetchSounds, fetchAnalytics, playSound, playUrl, downloadUrl, setVolumeLive, getVolume,
adminStatus, adminLogin, adminLogout, adminDelete, adminRename,
fetchCategories, partyStart, partyStop, subscribeEvents,
getSelectedChannels, setSelectedChannel, uploadFile,
@ -52,6 +52,13 @@ export default function App() {
const [importUrl, setImportUrl] = useState('');
const [importBusy, setImportBusy] = useState(false);
// Download modal state
const [dlModal, setDlModal] = useState<{
url: string; type: 'youtube' | 'instagram' | 'mp3' | null;
filename: string; phase: 'input' | 'downloading' | 'done' | 'error';
savedName?: string; error?: string;
} | null>(null);
/* ── Channels ── */
const [channels, setChannels] = useState<VoiceChannelInfo[]>([]);
const [selected, setSelected] = useState('');
@ -153,14 +160,35 @@ export default function App() {
setTimeout(() => setNotification(null), 3000);
}, []);
const soundKey = useCallback((s: Sound) => s.relativePath ?? s.fileName, []);
const isMp3Url = useCallback((value: string) => {
const YTDLP_HOSTS = ['youtube.com', 'www.youtube.com', 'm.youtube.com', 'youtu.be', 'music.youtube.com', 'instagram.com', 'www.instagram.com'];
/** Auto-prepend https:// if missing */
const normalizeUrl = useCallback((value: string): string => {
const v = value.trim();
if (!v) return v;
if (/^https?:\/\//i.test(v)) return v;
return 'https://' + v;
}, []);
const isSupportedUrl = useCallback((value: string) => {
try {
const parsed = new URL(value.trim());
return parsed.pathname.toLowerCase().endsWith('.mp3');
const parsed = new URL(normalizeUrl(value));
const host = parsed.hostname.toLowerCase();
if (parsed.pathname.toLowerCase().endsWith('.mp3')) return true;
if (YTDLP_HOSTS.some(h => host === h || host.endsWith('.' + h))) return true;
return false;
} catch {
return false;
}
}, []);
}, [normalizeUrl]);
const getUrlType = useCallback((value: string): 'youtube' | 'instagram' | 'mp3' | null => {
try {
const parsed = new URL(normalizeUrl(value));
const host = parsed.hostname.toLowerCase();
if (host.includes('youtube') || host === 'youtu.be') return 'youtube';
if (host.includes('instagram')) return 'instagram';
if (parsed.pathname.toLowerCase().endsWith('.mp3')) return 'mp3';
return null;
} catch { return null; }
}, [normalizeUrl]);
const guildId = selected ? selected.split(':')[0] : '';
const channelId = selected ? selected.split(':')[1] : '';
@ -346,22 +374,42 @@ export default function App() {
} catch (e: any) { notify(e?.message || 'Play fehlgeschlagen', 'error'); }
}
async function handleUrlImport() {
const trimmed = importUrl.trim();
if (!trimmed) return notify('Bitte einen MP3-Link eingeben', 'error');
if (!selected) return notify('Bitte einen Voice-Channel auswählen', 'error');
if (!isMp3Url(trimmed)) return notify('Nur direkte MP3-Links sind erlaubt', 'error');
setImportBusy(true);
// Open download modal instead of downloading directly
function handleUrlImport() {
const trimmed = normalizeUrl(importUrl);
if (!trimmed) return notify('Bitte einen Link eingeben', 'error');
if (!isSupportedUrl(trimmed)) return notify('Nur YouTube, Instagram oder direkte MP3-Links', 'error');
const urlType = getUrlType(trimmed);
// Pre-fill filename for MP3 links (basename without .mp3), empty for YT/IG
let defaultName = '';
if (urlType === 'mp3') {
try { defaultName = new URL(trimmed).pathname.split('/').pop()?.replace(/\.mp3$/i, '') ?? ''; } catch {}
}
setDlModal({ url: trimmed, type: urlType, filename: defaultName, phase: 'input' });
}
// Actual download triggered from modal
async function handleModalDownload() {
if (!dlModal) return;
setDlModal(prev => prev ? { ...prev, phase: 'downloading' } : null);
try {
await playUrl(trimmed, guildId, channelId, volume);
let savedName: string | undefined;
const fn = dlModal.filename.trim() || undefined;
if (selected && guildId && channelId) {
const result = await playUrl(dlModal.url, guildId, channelId, volume, fn);
savedName = result.saved;
} else {
const result = await downloadUrl(dlModal.url, fn);
savedName = result.saved;
}
setDlModal(prev => prev ? { ...prev, phase: 'done', savedName } : null);
setImportUrl('');
notify('MP3 importiert und abgespielt');
setRefreshKey(k => k + 1);
await loadAnalytics();
void loadAnalytics();
// Auto-close after 2.5s
setTimeout(() => setDlModal(null), 2500);
} catch (e: any) {
notify(e?.message || 'URL-Import fehlgeschlagen', 'error');
} finally {
setImportBusy(false);
setDlModal(prev => prev ? { ...prev, phase: 'error', error: e?.message || 'Fehler' } : null);
}
}
@ -715,20 +763,32 @@ export default function App() {
</div>
<div className="url-import-wrap">
<span className="material-icons url-import-icon">link</span>
<span className="material-icons url-import-icon">
{getUrlType(importUrl) === 'youtube' ? 'smart_display'
: getUrlType(importUrl) === 'instagram' ? 'photo_camera'
: 'link'}
</span>
<input
className="url-import-input"
type="url"
placeholder="MP3-URL einfügen..."
type="text"
placeholder="YouTube / Instagram / MP3-Link..."
value={importUrl}
onChange={e => setImportUrl(e.target.value)}
onKeyDown={e => { if (e.key === 'Enter') void handleUrlImport(); }}
/>
{importUrl && (
<span className={`url-import-tag ${isSupportedUrl(importUrl) ? 'valid' : 'invalid'}`}>
{getUrlType(importUrl) === 'youtube' ? 'YT'
: getUrlType(importUrl) === 'instagram' ? 'IG'
: getUrlType(importUrl) === 'mp3' ? 'MP3'
: '?'}
</span>
)}
<button
className="url-import-btn"
onClick={() => { void handleUrlImport(); }}
disabled={importBusy}
title="MP3 importieren"
disabled={importBusy || (!!importUrl && !isSupportedUrl(importUrl))}
title="Sound herunterladen"
>
{importBusy ? 'Lädt...' : 'Download'}
</button>
@ -1252,6 +1312,110 @@ export default function App() {
</div>
</div>
)}
{/* ── Download Modal ── */}
{dlModal && (
<div className="dl-modal-overlay" onClick={() => dlModal.phase !== 'downloading' && setDlModal(null)}>
<div className="dl-modal" onClick={e => e.stopPropagation()}>
<div className="dl-modal-header">
<span className="material-icons" style={{ fontSize: 20 }}>
{dlModal.type === 'youtube' ? 'smart_display' : dlModal.type === 'instagram' ? 'photo_camera' : 'audio_file'}
</span>
<span>
{dlModal.phase === 'input' ? 'Sound herunterladen'
: dlModal.phase === 'downloading' ? 'Wird heruntergeladen...'
: dlModal.phase === 'done' ? 'Fertig!'
: 'Fehler'}
</span>
{dlModal.phase !== 'downloading' && (
<button className="dl-modal-close" onClick={() => setDlModal(null)}>
<span className="material-icons" style={{ fontSize: 16 }}>close</span>
</button>
)}
</div>
<div className="dl-modal-body">
{/* URL badge */}
<div className="dl-modal-url">
<span className={`dl-modal-tag ${dlModal.type ?? ''}`}>
{dlModal.type === 'youtube' ? 'YouTube' : dlModal.type === 'instagram' ? 'Instagram' : 'MP3'}
</span>
<span className="dl-modal-url-text" title={dlModal.url}>
{dlModal.url.length > 60 ? dlModal.url.slice(0, 57) + '...' : dlModal.url}
</span>
</div>
{/* Filename input (input phase only) */}
{dlModal.phase === 'input' && (
<div className="dl-modal-field">
<label className="dl-modal-label">Dateiname</label>
<div className="dl-modal-input-wrap">
<input
className="dl-modal-input"
type="text"
placeholder={dlModal.type === 'mp3' ? 'Dateiname...' : 'Wird automatisch erkannt...'}
value={dlModal.filename}
onChange={e => setDlModal(prev => prev ? { ...prev, filename: e.target.value } : null)}
onKeyDown={e => { if (e.key === 'Enter') void handleModalDownload(); }}
autoFocus
/>
<span className="dl-modal-ext">.mp3</span>
</div>
<span className="dl-modal-hint">Leer lassen = automatischer Name</span>
</div>
)}
{/* Progress (downloading phase) */}
{dlModal.phase === 'downloading' && (
<div className="dl-modal-progress">
<div className="dl-modal-spinner" />
<span>
{dlModal.type === 'youtube' || dlModal.type === 'instagram'
? 'Audio wird extrahiert...'
: 'MP3 wird heruntergeladen...'}
</span>
</div>
)}
{/* Success */}
{dlModal.phase === 'done' && (
<div className="dl-modal-success">
<span className="material-icons dl-modal-check">check_circle</span>
<span>Gespeichert als <b>{dlModal.savedName}</b></span>
</div>
)}
{/* Error */}
{dlModal.phase === 'error' && (
<div className="dl-modal-error">
<span className="material-icons" style={{ color: '#e74c3c' }}>error</span>
<span>{dlModal.error}</span>
</div>
)}
</div>
{/* Actions */}
{dlModal.phase === 'input' && (
<div className="dl-modal-actions">
<button className="dl-modal-cancel" onClick={() => setDlModal(null)}>Abbrechen</button>
<button className="dl-modal-submit" onClick={() => void handleModalDownload()}>
<span className="material-icons" style={{ fontSize: 16 }}>download</span>
Herunterladen
</button>
</div>
)}
{dlModal.phase === 'error' && (
<div className="dl-modal-actions">
<button className="dl-modal-cancel" onClick={() => setDlModal(null)}>Schliessen</button>
<button className="dl-modal-submit" onClick={() => setDlModal(prev => prev ? { ...prev, phase: 'input', error: undefined } : null)}>
<span className="material-icons" style={{ fontSize: 16 }}>refresh</span>
Nochmal
</button>
</div>
)}
</div>
</div>
)}
</div>
);
}