diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs index f12ab5cff..6a8e5c04c 100644 --- a/src-tauri/src/commands_v2.rs +++ b/src-tauri/src/commands_v2.rs @@ -1429,6 +1429,117 @@ pub fn v2_get_alsa_plugins() -> Result, String> { // ==================== Link Resolver ==================== +/// Result of resolving a cross-platform music link. +#[derive(Debug, Clone, serde::Serialize)] +#[serde(tag = "kind")] +pub enum MusicLinkResult { + /// Successfully resolved to a Qobuz entity. + Resolved { + link: qbz_qobuz::ResolvedLink, + provider: Option, + }, + /// The URL is a playlist — redirect to the Playlist Importer. + PlaylistDetected { + provider: String, + }, + /// The content exists on the source platform but is not available on Qobuz. + NotOnQobuz { + provider: Option, + }, +} + +/// Resolve a cross-platform music link to a Qobuz navigation action. +/// +/// Accepts URLs from Qobuz, Spotify, Apple Music, Tidal, Deezer, song.link, and album.link. +/// For non-Qobuz tracks/albums, uses the Odesli API to find the Qobuz equivalent. +/// For playlists, returns `PlaylistDetected` so the frontend can redirect to the importer. +#[tauri::command] +pub async fn v2_resolve_music_link( + url: String, + state: State<'_, AppState>, +) -> Result { + use crate::playlist_import::providers::{detect_music_resource, MusicResource}; + + let url = url.trim().to_string(); + if url.is_empty() { + return Err(RuntimeError::Internal("Empty URL".to_string())); + } + + // 1. Try Qobuz native resolve first (sync, no network) + if let Ok(resolved) = qbz_qobuz::resolve_link(&url) { + return Ok(MusicLinkResult::Resolved { + link: resolved, + provider: None, + }); + } + + // 2. Detect what kind of resource this is + let resource = detect_music_resource(&url).ok_or_else(|| { + RuntimeError::Internal("Unsupported or invalid music link".to_string()) + })?; + + match resource { + MusicResource::Qobuz => { + // Already handled above, but just in case + let resolved = qbz_qobuz::resolve_link(&url) + .map_err(|e| RuntimeError::Internal(e.to_string()))?; + Ok(MusicLinkResult::Resolved { + link: resolved, + provider: None, + }) + } + + MusicResource::Playlist { provider } => Ok(MusicLinkResult::PlaylistDetected { + provider: format!("{:?}", provider), + }), + + MusicResource::Track { provider, url: source_url } => { + resolve_via_odesli(&state.songlink, &source_url, Some(&provider)).await + } + + MusicResource::Album { provider, url: source_url } => { + resolve_via_odesli(&state.songlink, &source_url, Some(&provider)).await + } + + MusicResource::SongLink { url: source_url } => { + resolve_via_odesli(&state.songlink, &source_url, None).await + } + } +} + +/// Use the Odesli API to find the Qobuz equivalent of a cross-platform URL, +/// then parse the Qobuz URL into a ResolvedLink. +async fn resolve_via_odesli( + songlink: &crate::share::SongLinkClient, + url: &str, + provider: Option<&crate::playlist_import::providers::MusicProvider>, +) -> Result { + let provider_name = provider.map(|p| format!("{:?}", p)); + + // Call Odesli API — it accepts any supported music URL + let response = songlink + .get_by_url(url, crate::share::ContentType::Track) + .await + .map_err(|e| RuntimeError::Internal(format!("Odesli API error: {}", e)))?; + + // Look for a Qobuz URL in the platforms map + let qobuz_url = response.platforms.get("qobuz"); + + match qobuz_url { + Some(qobuz_url) => { + let resolved = qbz_qobuz::resolve_link(qobuz_url) + .map_err(|e| RuntimeError::Internal(format!("Failed to parse Qobuz URL from Odesli: {}", e)))?; + Ok(MusicLinkResult::Resolved { + link: resolved, + provider: provider_name, + }) + } + None => Ok(MusicLinkResult::NotOnQobuz { + provider: provider_name, + }), + } +} + #[tauri::command] pub fn v2_resolve_qobuz_link(url: String) -> Result { qbz_qobuz::resolve_link(&url).map_err(|e| RuntimeError::Internal(e.to_string())) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3cfd389d4..d7d84df14 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1090,6 +1090,7 @@ pub fn run() { commands_v2::v2_purchases_get_downloaded_track_ids, commands_v2::v2_dynamic_suggest, commands_v2::v2_dynamic_suggest_raw, + commands_v2::v2_resolve_music_link, commands_v2::v2_resolve_qobuz_link, commands_v2::v2_get_qobuz_track_url, commands_v2::v2_check_qobuzapp_handler, diff --git a/src-tauri/src/playlist_import/providers/apple.rs b/src-tauri/src/playlist_import/providers/apple.rs index f8aa421ac..4eeed08ce 100644 --- a/src-tauri/src/playlist_import/providers/apple.rs +++ b/src-tauri/src/playlist_import/providers/apple.rs @@ -5,6 +5,50 @@ use serde_json::Value; use crate::playlist_import::errors::PlaylistImportError; use crate::playlist_import::models::{ImportPlaylist, ImportProvider, ImportTrack}; +/// Detect if a URL is an Apple Music track, album, or playlist. +/// +/// Apple Music URLs: +/// - Track: `music.apple.com/{storefront}/album/{name}/{id}?i={track_id}` +/// - Album: `music.apple.com/{storefront}/album/{name}/{id}` (no `?i=`) +/// - Playlist: `music.apple.com/{storefront}/playlist/{name}/{pl.xxx}` +/// - Song: `music.apple.com/{storefront}/song/{name}/{id}` +pub fn detect_resource(url: &str) -> Option { + if !url.contains("music.apple.com/") { + return None; + } + + // Playlist + if parse_playlist_id(url).is_some() { + return Some(super::MusicResource::Playlist { + provider: super::MusicProvider::AppleMusic, + }); + } + + // Song page (explicit song URL) + if url.contains("/song/") { + return Some(super::MusicResource::Track { + provider: super::MusicProvider::AppleMusic, + url: url.to_string(), + }); + } + + // Album page — with ?i= parameter means specific track + if url.contains("/album/") { + if url.contains("?i=") || url.contains("&i=") { + return Some(super::MusicResource::Track { + provider: super::MusicProvider::AppleMusic, + url: url.to_string(), + }); + } + return Some(super::MusicResource::Album { + provider: super::MusicProvider::AppleMusic, + url: url.to_string(), + }); + } + + None +} + pub fn parse_playlist_id(url: &str) -> Option<(String, String)> { if !url.contains("music.apple.com/") { return None; diff --git a/src-tauri/src/playlist_import/providers/deezer.rs b/src-tauri/src/playlist_import/providers/deezer.rs index d1788c03e..435afa2fc 100644 --- a/src-tauri/src/playlist_import/providers/deezer.rs +++ b/src-tauri/src/playlist_import/providers/deezer.rs @@ -5,6 +5,50 @@ use serde_json::Value; use crate::playlist_import::errors::PlaylistImportError; use crate::playlist_import::models::{ImportPlaylist, ImportProvider, ImportTrack}; +/// Detect if a URL is a Deezer track, album, or playlist. +/// +/// Deezer URLs: +/// - Track: `deezer.com/track/{id}` or `deezer.com/{locale}/track/{id}` +/// - Album: `deezer.com/album/{id}` or `deezer.com/{locale}/album/{id}` +/// - Playlist: `deezer.com/playlist/{id}` or `deezer.com/{locale}/playlist/{id}` +pub fn detect_resource(url: &str) -> Option { + if !url.contains("deezer.com") { + return None; + } + + // Playlist + if parse_playlist_id(url).is_some() { + return Some(super::MusicResource::Playlist { + provider: super::MusicProvider::Deezer, + }); + } + + let parts: Vec<&str> = url.split('/').collect(); + for (idx, part) in parts.iter().enumerate() { + match *part { + "track" => { + if parts.get(idx + 1).map(|s| !s.is_empty()).unwrap_or(false) { + return Some(super::MusicResource::Track { + provider: super::MusicProvider::Deezer, + url: url.to_string(), + }); + } + } + "album" => { + if parts.get(idx + 1).map(|s| !s.is_empty()).unwrap_or(false) { + return Some(super::MusicResource::Album { + provider: super::MusicProvider::Deezer, + url: url.to_string(), + }); + } + } + _ => {} + } + } + + None +} + pub fn parse_playlist_id(url: &str) -> Option { if !url.contains("deezer.com") { return None; diff --git a/src-tauri/src/playlist_import/providers/mod.rs b/src-tauri/src/playlist_import/providers/mod.rs index 82f1dc6c2..0a5da558d 100644 --- a/src-tauri/src/playlist_import/providers/mod.rs +++ b/src-tauri/src/playlist_import/providers/mod.rs @@ -5,9 +5,75 @@ pub mod deezer; pub mod spotify; pub mod tidal; +use serde::{Deserialize, Serialize}; + use crate::playlist_import::errors::PlaylistImportError; use crate::playlist_import::models::ImportPlaylist; +/// Which streaming platform a music link belongs to. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum MusicProvider { + Spotify, + AppleMusic, + Tidal, + Deezer, +} + +/// The kind of resource a music URL points to. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum MusicResource { + /// A native Qobuz URL — resolve directly. + Qobuz, + /// A single track on a third-party platform. + Track { provider: MusicProvider, url: String }, + /// An album on a third-party platform. + Album { provider: MusicProvider, url: String }, + /// A playlist — should be redirected to the Playlist Importer. + Playlist { provider: MusicProvider }, + /// A song.link / album.link / odesli.co URL — resolve via Odesli API. + SongLink { url: String }, +} + +/// Detect what kind of music resource a URL points to. +/// +/// Returns `None` for URLs that don't match any supported platform. +pub fn detect_music_resource(url: &str) -> Option { + let url = url.trim(); + if url.is_empty() { + return None; + } + + // 1. Qobuz — resolve_link() handles this natively + if qbz_qobuz::resolve_link(url).is_ok() { + return Some(MusicResource::Qobuz); + } + + // 2. song.link / album.link / odesli.co URLs + let lower = url.to_ascii_lowercase(); + if lower.contains("song.link/") + || lower.contains("album.link/") + || lower.contains("odesli.co/") + { + return Some(MusicResource::SongLink { url: url.to_string() }); + } + + // 3. Per-provider detection (track/album/playlist) + if let Some(resource) = spotify::detect_resource(url) { + return Some(resource); + } + if let Some(resource) = apple::detect_resource(url) { + return Some(resource); + } + if let Some(resource) = tidal::detect_resource(url) { + return Some(resource); + } + if let Some(resource) = deezer::detect_resource(url) { + return Some(resource); + } + + None +} + /// User-provided credentials for a provider #[derive(Debug, Clone, Default)] pub struct ProviderCredentials { diff --git a/src-tauri/src/playlist_import/providers/spotify.rs b/src-tauri/src/playlist_import/providers/spotify.rs index efa68a53a..b61c385a2 100644 --- a/src-tauri/src/playlist_import/providers/spotify.rs +++ b/src-tauri/src/playlist_import/providers/spotify.rs @@ -9,6 +9,39 @@ use crate::playlist_import::models::{ImportPlaylist, ImportProvider, ImportTrack const SPOTIFY_PROXY_URL: &str = "https://qbz-api-proxy.blitzkriegfc.workers.dev/spotify"; const SPOTIFY_API_BASE: &str = "https://api.spotify.com/v1"; +/// Detect if a URL is a Spotify track, album, or playlist. +pub fn detect_resource(url: &str) -> Option { + let lower = url.to_ascii_lowercase(); + if !lower.contains("spotify.com/") && !lower.starts_with("spotify:") { + return None; + } + + // Playlist check first (so parse_playlist_id takes priority for playlists) + if parse_playlist_id(url).is_some() { + return Some(super::MusicResource::Playlist { + provider: super::MusicProvider::Spotify, + }); + } + + // Track: open.spotify.com/track/ or spotify:track: + if lower.contains("/track/") || lower.contains(":track:") { + return Some(super::MusicResource::Track { + provider: super::MusicProvider::Spotify, + url: url.to_string(), + }); + } + + // Album: open.spotify.com/album/ or spotify:album: + if lower.contains("/album/") || lower.contains(":album:") { + return Some(super::MusicResource::Album { + provider: super::MusicProvider::Spotify, + url: url.to_string(), + }); + } + + None +} + pub fn parse_playlist_id(url: &str) -> Option { if let Some(rest) = url.strip_prefix("spotify:playlist:") { if !rest.is_empty() { diff --git a/src-tauri/src/playlist_import/providers/tidal.rs b/src-tauri/src/playlist_import/providers/tidal.rs index 7df144af1..81192f120 100644 --- a/src-tauri/src/playlist_import/providers/tidal.rs +++ b/src-tauri/src/playlist_import/providers/tidal.rs @@ -15,6 +15,45 @@ const RATE_LIMIT_DELAY_MS: u64 = 200; // Delay between API calls to avoid 429 const TIDAL_PROXY_URL: &str = "https://qbz-api-proxy.blitzkriegfc.workers.dev/tidal"; const TIDAL_API_BASE: &str = "https://openapi.tidal.com/v2"; +/// Detect if a URL is a Tidal track, album, or playlist. +/// +/// Tidal URLs: +/// - Track: `tidal.com/browse/track/{id}` or `tidal.com/track/{id}` +/// - Album: `tidal.com/browse/album/{id}` or `tidal.com/album/{id}` +/// - Playlist: `tidal.com/browse/playlist/{id}` or `tidal.com/playlist/{id}` +pub fn detect_resource(url: &str) -> Option { + if !url.contains("tidal.com") { + return None; + } + + // Playlist + if parse_playlist_id(url).is_some() { + return Some(super::MusicResource::Playlist { + provider: super::MusicProvider::Tidal, + }); + } + + let lower = url.to_ascii_lowercase(); + + // Track + if lower.contains("/track/") || lower.contains("/browse/track/") { + return Some(super::MusicResource::Track { + provider: super::MusicProvider::Tidal, + url: url.to_string(), + }); + } + + // Album + if lower.contains("/album/") || lower.contains("/browse/album/") { + return Some(super::MusicResource::Album { + provider: super::MusicProvider::Tidal, + url: url.to_string(), + }); + } + + None +} + pub fn parse_playlist_id(url: &str) -> Option { if !url.contains("tidal.com") { return None; diff --git a/src/lib/components/LinkResolverModal.svelte b/src/lib/components/LinkResolverModal.svelte index 972c54a08..1210713e9 100644 --- a/src/lib/components/LinkResolverModal.svelte +++ b/src/lib/components/LinkResolverModal.svelte @@ -1,6 +1,7 @@