YtDownloadService: Gast-Zugang neben Baka-Auth unterstützen

Optionaler gast-Parameter (GastZugang) — sendet bei fehlender
Baka-Anmeldung X-Guest-Token statt Authorization, übernimmt
guest_remaining aus der Erfolgsantwort, holt bei 401 automatisch
einen neuen Gast-Token nach.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Hermes (Server)
2026-08-26 20:45:52 +02:00
co-authored by Claude Sonnet 5
parent 2e14256ded
commit 1ff03c96ed
2 changed files with 112 additions and 5 deletions
+16 -5
View File
@@ -6,6 +6,7 @@ import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'baka_auth.dart';
import 'gast_zugang.dart';
/// Entfernt alles, was in einem Dateinamen Ärger macht.
String sichererDateiname(String titel) =>
@@ -35,12 +36,13 @@ class YtErgebnis {
/// Lädt Musik über den Baka-YouTube-Proxy herunter. Der Proxy erledigt die
/// eigentliche Arbeit und liefert eine fertige MP3; die App speichert sie nur.
class YtDownloadService extends ChangeNotifier {
YtDownloadService({required this.auth, http.Client? client})
YtDownloadService({required this.auth, this.gast, http.Client? client})
: _client = client ?? http.Client();
static const proxyUrl = 'https://yt.baka-net.de';
final BakaAuth auth;
final GastZugang? gast;
final http.Client _client;
bool _laeuft = false;
@@ -95,7 +97,8 @@ class YtDownloadService extends ChangeNotifier {
final abbruch = _abbruch = Completer<void>();
notifyListeners();
if (!auth.istAngemeldet) {
final gastAktiv = !auth.istAngemeldet && (gast?.hatToken ?? false);
if (!auth.istAngemeldet && !gastAktiv) {
_scheitere('Bitte zuerst beim Baka-Konto anmelden');
return null;
}
@@ -103,6 +106,7 @@ class YtDownloadService extends ChangeNotifier {
_scheitere('Das ist keine YouTube-Adresse');
return null;
}
final zugangsHeader = gastAktiv ? gast!.gastHeader : auth.authHeader;
// ─── 1/2: Proxy fragen — er lädt das Video und wandelt es um ───
_melde('Proxy wird gefragt …');
@@ -113,7 +117,7 @@ class YtDownloadService extends ChangeNotifier {
.post(
Uri.parse('$proxyUrl/api/yt-dl'),
headers: {
...auth.authHeader,
...zugangsHeader,
'Content-Type': 'application/json'
},
body: jsonEncode({'url': url, 'cookies': cookies}),
@@ -137,7 +141,10 @@ class YtDownloadService extends ChangeNotifier {
}
if (antwort.statusCode == 401) {
_scheitere('Anmeldung abgelaufen — bitte neu anmelden');
if (gastAktiv) unawaited(gast!.holeToken());
_scheitere(gastAktiv
? 'Gast-Zugang abgelaufen — bitte erneut versuchen'
: 'Anmeldung abgelaufen — bitte neu anmelden');
return null;
}
if (antwort.statusCode != 200) {
@@ -149,6 +156,10 @@ class YtDownloadService extends ChangeNotifier {
final titel = daten['titel'] as String? ?? 'Unbekannt';
final dauer = (daten['dauer'] as int?) ?? 0;
final mp3Url = daten['mp3_url'] as String? ?? '/api/dl/audio.mp3';
final gastRest = daten['guest_remaining'] as int?;
if (gastAktiv && gastRest != null) {
gast!.merkeVerbleibend(gastRest);
}
// ─── 2/2: fertige MP3 abholen und speichern ───
_melde('Lade „$titel“ …');
@@ -161,7 +172,7 @@ class YtDownloadService extends ChangeNotifier {
try {
final mp3 = await _abbrechbar(
_client
.get(Uri.parse('$proxyUrl$mp3Url'), headers: auth.authHeader)
.get(Uri.parse('$proxyUrl$mp3Url'), headers: zugangsHeader)
.timeout(const Duration(seconds: 180)),
abbruch,
);