refactor(api): make audio tagging and decryption async to avoid UI blocking

This commit is contained in:
2025-10-03 11:09:06 -04:00
parent 14a5b50942
commit 0ef56c3bed
4 changed files with 44 additions and 22 deletions

View File

@@ -12,13 +12,22 @@ fn greet(name: &str) -> String {
/// Tag an audio file with metadata, cover art, and lyrics
#[tauri::command]
fn tag_audio_file(
async fn tag_audio_file(
path: String,
metadata: tagger::TaggingMetadata,
cover_data: Option<Vec<u8>>,
embed_lyrics: bool,
) -> Result<(), String> {
tagger::tag_audio_file(&path, &metadata, cover_data.as_deref(), embed_lyrics)
// Run tagging on a background thread to avoid blocking the UI
tauri::async_runtime::spawn_blocking(move || {
tagger::tag_audio_file(&path, &metadata, cover_data.as_deref(), embed_lyrics)
})
.await
.map_err(|e| format!("Tagging task failed: {}", e))?
// Flatten the inner Result
?;
Ok(())
}
/// Read metadata from an audio file (MP3 or FLAC)
@@ -29,8 +38,15 @@ fn read_audio_metadata(path: String) -> Result<metadata::AudioMetadata, String>
/// Decrypt Deezer track data
#[tauri::command]
fn decrypt_deezer_track(data: Vec<u8>, track_id: String) -> Result<Vec<u8>, String> {
Ok(deezer_crypto::decrypt_track(&data, &track_id))
async fn decrypt_deezer_track(data: Vec<u8>, track_id: String) -> Result<Vec<u8>, String> {
// Run decryption on a background thread to avoid blocking the UI
let result = tauri::async_runtime::spawn_blocking(move || {
deezer_crypto::decrypt_track(&data, &track_id)
})
.await
.map_err(|e| format!("Decryption task failed: {}", e))?;
Ok(result)
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]