T-012: Fix — Bibliothek async (spawn_blocking, kein UI-Freeze), Lade-Übergang, decrypt-Button für verschlüsselte Auth-Dateien

This commit is contained in:
2026-11-06 19:44:58 +01:00
parent 5ef2eb0145
commit eace05f180
4 changed files with 67 additions and 9 deletions

View File

@ -41,13 +41,17 @@ pub struct Book {
/// `audible-cli` später austauschbar bleibt (spec E-02). /// `audible-cli` später austauschbar bleibt (spec E-02).
pub trait AudibleClient: Send + Sync { pub trait AudibleClient: Send + Sync {
fn auth_status(&self) -> Result<AuthStatus, String>; fn auth_status(&self) -> Result<AuthStatus, String>;
/// Startet die interaktive Anmeldung (audible quickstart) im Terminal. /// Startet die interaktive Anmeldung (audible-cli) im Terminal.
/// true = Terminal gestartet, der Nutzer meldet sich dort an; dann /// true = Terminal gestartet, der Nutzer meldet sich dort an; dann
/// `auth_status` erneut prüfen. /// `auth_status` erneut prüfen.
fn start_login(&self) -> Result<bool, String>; fn start_login(&self) -> Result<bool, String>;
/// Startet `manage auth-file decrypt` im Terminal — nötig, wenn die
/// Auth-Datei verschlüsselt erzeugt wurde.
fn start_decrypt(&self) -> Result<bool, String>;
fn library(&self) -> Result<Vec<Book>, String>; fn library(&self) -> Result<Vec<Book>, String>;
} }
#[derive(Clone)]
pub struct CliAudibleClient { pub struct CliAudibleClient {
auth_dir: PathBuf, auth_dir: PathBuf,
} }
@ -141,6 +145,31 @@ impl AudibleClient for CliAudibleClient {
}) })
} }
/// Startet `manage auth-file decrypt` im Terminal (interaktiv, Passwort
/// bleibt im Terminal). Bekannter Profilname wird mitgeliefert.
fn start_decrypt(&self) -> Result<bool, String> {
let bin = self.audible_binary()?;
let Some((term, needs_e, hold)) = find_terminal() else {
return Err("kein unterstütztes Terminal gefunden — bitte in deiner Shell: audible manage auth-file decrypt".to_string());
};
let mut cmd = Command::new(term);
if !hold.is_empty() {
cmd.arg(hold);
}
if needs_e {
cmd.arg("-e");
}
cmd.arg(&bin).args(["manage", "auth-file", "decrypt"]);
if let Ok(s) = self.auth_status() {
if let Some(name) = s.profiles.first() {
cmd.arg("-f").arg(name);
}
}
cmd.spawn()
.map_err(|e| format!("Terminal konnte nicht gestartet werden: {e}"))?;
Ok(true)
}
fn start_login(&self) -> Result<bool, String> { fn start_login(&self) -> Result<bool, String> {
let bin = self.audible_binary()?; let bin = self.audible_binary()?;
let Some((term, needs_e, hold)) = find_terminal() else { let Some((term, needs_e, hold)) = find_terminal() else {

View File

@ -13,16 +13,32 @@ fn start_login(client: tauri::State<CliAudibleClient>) -> Result<bool, String> {
} }
#[tauri::command] #[tauri::command]
fn get_library(client: tauri::State<CliAudibleClient>) -> Result<Vec<Book>, String> { fn start_decrypt(client: tauri::State<CliAudibleClient>) -> Result<bool, String> {
client.library() client.start_decrypt()
} }
/// Bibliothek laden: externer CLI-Export dauert (Netzwerk) — läuft deshalb
/// auf einem Blocking-Thread, damit die UI nicht friert.
#[tauri::command]
async fn get_library(
client: tauri::State<'_, CliAudibleClient>,
) -> Result<Vec<Book>, String> {
let c = client.inner().clone();
tauri::async_runtime::spawn_blocking(move || c.library())
.await
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
}
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_opener::init())
.manage(CliAudibleClient::new()) .manage(CliAudibleClient::new())
.invoke_handler(tauri::generate_handler![auth_status, start_login, get_library]) .invoke_handler(tauri::generate_handler![
auth_status,
start_login,
start_decrypt,
get_library
])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { authStatus, startLogin, getLibrary, type Book } from './lib/audible' import { authStatus, startLogin, startDecrypt, getLibrary, type Book } from './lib/audible'
type View = 'loading' | 'login' | 'library' type View = 'loading' | 'login' | 'library'
type SortKey = 'added' | 'title' | 'author' | 'runtime' | 'percent' type SortKey = 'added' | 'title' | 'author' | 'runtime' | 'percent'
@ -61,10 +61,16 @@
books = await getLibrary() books = await getLibrary()
view = 'library' view = 'library'
} catch (e) { } catch (e) {
// Fehler sichtbar machen, nicht in der Lade-Ansicht hängen const msg = String(e)
error = String(e) if (msg.toLowerCase().includes('encrypted')) {
view = 'library' // Auth-Datei verschlüsselt — Entschlüsselung im Terminal anbieten
books = [] error = 'Auth-Datei ist verschlüsselt. Bitte einmal entschriften:'
view = 'login'
} else {
error = msg
view = 'library'
books = []
}
} finally { } finally {
checking = false checking = false
loadingMsg = 'Prüfe Anmeldung…' loadingMsg = 'Prüfe Anmeldung…'
@ -78,6 +84,7 @@
const s = await authStatus() const s = await authStatus()
if (s.logged_in) { if (s.logged_in) {
clearPoll() clearPoll()
view = 'loading'
loadingMsg = 'Lade Bibliothek…' loadingMsg = 'Lade Bibliothek…'
await loadLibrary() await loadLibrary()
} else if (view !== 'login') { } else if (view !== 'login') {
@ -136,6 +143,11 @@
<button class="ghost" onclick={() => checkAuth()} disabled={checking}> <button class="ghost" onclick={() => checkAuth()} disabled={checking}>
{checking ? 'Prüfe…' : 'Jetzt prüfen'} {checking ? 'Prüfe…' : 'Jetzt prüfen'}
</button> </button>
{#if error === 'Auth-Datei ist verschlüsselt. Bitte einmal entschriften:'}
<button class="ghost" onclick={() => startDecrypt().catch((e) => (error = String(e)))}>
Entschlüsseln im Terminal…
</button>
{/if}
{#if info}<p class="info">{info}</p>{/if} {#if info}<p class="info">{info}</p>{/if}
{#if error}<p class="error">{error}</p>{/if} {#if error}<p class="error">{error}</p>{/if}
</main> </main>

View File

@ -21,4 +21,5 @@ export interface Book {
export const authStatus = () => invoke<AuthStatus>('auth_status') export const authStatus = () => invoke<AuthStatus>('auth_status')
export const startLogin = () => invoke<boolean>('start_login') export const startLogin = () => invoke<boolean>('start_login')
export const startDecrypt = () => invoke<boolean>('start_decrypt')
export const getLibrary = () => invoke<Book[]>('get_library') export const getLibrary = () => invoke<Book[]>('get_library')