import 'dart:io'; import 'package:dio/dio.dart'; import 'package:path/path.dart' as p; import 'package:path_provider/path_provider.dart'; import '../../cekirdek/log.dart'; /// PowerShell scriptinin yolu — uygulamanın çalıştığı dizine göre. String _ltxTokenPsYolu() { // Önce çalışan exe'nin yanındaki tools/ — release build'de final exeDir = File(Platform.resolvedExecutable).parent.path; final yakinin = File(p.join(exeDir, 'tools', 'ltx_token_oku.ps1')); if (yakinin.existsSync()) return yakinin.path; // Geliştirme modunda proje kökü final gelistirme = File(p.join( Directory.current.path, 'tools', 'ltx_token_oku.ps1', )); if (gelistirme.existsSync()) return gelistirme.path; // Mutlak fallback return r'D:\__sistem__\Masaustu\FikoYT.Yeni.Cag\video-uretim\flutter-video-uret\tools\ltx_token_oku.ps1'; } class LtxTokenYonetici { static Future _tokenDosyasi() async { final docs = await getApplicationDocumentsDirectory(); final dir = Directory(p.join(docs.path, 'FikoYT-VideoStudyo')); if (!await dir.exists()) await dir.create(recursive: true); return File(p.join(dir.path, 'ltx_token.txt')); } static Future kaydedilenOku() async { final f = await _tokenDosyasi(); if (await f.exists()) { final s = (await f.readAsString()).trim(); if (s.isNotEmpty) return s; } // Fallback: mevcut Python pipeline'ın yazdığı token dosyası final mevcutPipelineToken = File( r'D:\__sistem__\Masaustu\FikoYT.Yeni.Cag\video-uretim\ltx_token.txt', ); if (await mevcutPipelineToken.exists()) { final s = (await mevcutPipelineToken.readAsString()).trim(); if (s.isNotEmpty) return s; } return null; } /// PowerShell + Win32 API ile çalışan ltx2_server.py sürecinin /// LTX_AUTH_TOKEN env değişkenini canlı okur (Python pipeline ile aynı yöntem). static Future processBellektenOku() async { try { final scriptYolu = _ltxTokenPsYolu(); Log.bilgi('LTX', 'Process bellekten token deneniyor: $scriptYolu'); final r = await Process.run( 'powershell', [ '-ExecutionPolicy', 'Bypass', '-NoProfile', '-File', scriptYolu, ], ); if (r.exitCode == 0) { final token = (r.stdout as String).trim(); if (token.isNotEmpty) { Log.bilgi('LTX', 'Token process belleğinden bulundu'); return token; } } else { Log.uyari('LTX', 'PowerShell token okuma başarısız: ${r.stderr}'); } } catch (e) { Log.uyari('LTX', 'Token okuma hatası: $e'); } return null; } static Future kaydet(String token) async { final f = await _tokenDosyasi(); await f.writeAsString(token.trim()); } /// /api/generation/progress endpoint'ine istek atar; 200 → geçerli, 401 → geçersiz. static Future dogrula(String url, String? token) async { final dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 5))); try { final r = await dio.get( '$url/api/generation/progress', options: Options( headers: token != null ? {'Authorization': 'Bearer $token'} : null, validateStatus: (_) => true, ), ); if (r.statusCode == 200) return true; if (r.statusCode == 401) return false; } catch (e) { Log.uyari('LTX', 'Token doğrulama hatası: $e'); } return false; } }