mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-21 22:15:40 +00:00
ff456dd823
Moves analysis_cache + analysis_runtime out of the top crate into a new
psysonic-analysis crate, plus the runtime user-agent facade into
psysonic-core. The audio↔analysis dependency cycle is broken via a
PlaybackQueryHandle port registered as Tauri State.
crates/psysonic-analysis/ new lib crate
src/analysis_cache/{mod,store,compute} unchanged behaviour
src/analysis_runtime.rs + enqueue_analysis_seed (was
in lib_commands/cache/offline)
crates/psysonic-core/src/
user_agent.rs subsonic_wire_user_agent +
runtime/default helpers
(was in top lib.rs)
ports.rs PlaybackQueryHandle: closure
wrapper, not Arc<dyn Trait>,
so existing State<AudioEngine>
callsites stay unchanged
The shell setup hook registers the real PlaybackQueryHandle once the
AppHandle is available; the closure captures it and re-resolves
AudioEngine via try_state at each call.
Top crate keeps `crate::analysis_cache`, `crate::analysis_runtime`,
`crate::subsonic_wire_user_agent`, and `crate::submit_analysis_cpu_seed`
working via re-exports — no audio/lib_commands callsite needed editing.
Behaviour preserving. Cargo check + clippy --workspace clean (only
pre-existing warnings carry over).
232 lines
7.1 KiB
Rust
232 lines
7.1 KiB
Rust
use crate::analysis_runtime::enqueue_analysis_seed;
|
|
use crate::audio;
|
|
use crate::subsonic_wire_user_agent;
|
|
|
|
use super::super::file_transfer::stream_to_file;
|
|
use super::downloads::{resolve_hot_cache_root, HotCacheDownloadResult};
|
|
use super::offline::enqueue_analysis_seed_from_file;
|
|
|
|
#[tauri::command]
|
|
pub(crate) async fn download_track_hot_cache(
|
|
track_id: String,
|
|
server_id: String,
|
|
url: String,
|
|
suffix: String,
|
|
custom_dir: Option<String>,
|
|
app: tauri::AppHandle,
|
|
) -> Result<HotCacheDownloadResult, String> {
|
|
let root = resolve_hot_cache_root(custom_dir, &app)?;
|
|
let cache_dir = root.join(&server_id);
|
|
|
|
tokio::fs::create_dir_all(&cache_dir)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let file_path = cache_dir.join(format!("{}.{}", track_id, suffix));
|
|
let path_str = file_path.to_string_lossy().to_string();
|
|
|
|
if file_path.exists() {
|
|
let size = tokio::fs::metadata(&file_path)
|
|
.await
|
|
.map(|m| m.len())
|
|
.unwrap_or(0);
|
|
crate::app_deprintln!(
|
|
"[hot-cache] download disk_hit track_id={} server_id={} bytes={}",
|
|
track_id,
|
|
server_id,
|
|
size
|
|
);
|
|
// Disk hit: still seed analysis, but do not block the command (full-file read); the
|
|
// prefetch worker runs invokes sequentially.
|
|
let app_seed = app.clone();
|
|
let tid = track_id.clone();
|
|
let fp = file_path.clone();
|
|
tokio::spawn(async move {
|
|
enqueue_analysis_seed_from_file(&app_seed, &tid, &fp).await;
|
|
});
|
|
return Ok(HotCacheDownloadResult {
|
|
path: path_str,
|
|
size,
|
|
});
|
|
}
|
|
|
|
crate::app_deprintln!(
|
|
"[hot-cache] download http_start track_id={} server_id={}",
|
|
track_id,
|
|
server_id
|
|
);
|
|
|
|
let client = reqwest::Client::builder()
|
|
.user_agent(subsonic_wire_user_agent())
|
|
.timeout(std::time::Duration::from_secs(120))
|
|
.build()
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let response = client.get(&url).send().await.map_err(|e| e.to_string())?;
|
|
if !response.status().is_success() {
|
|
return Err(format!("HTTP {}", response.status().as_u16()));
|
|
}
|
|
|
|
// Stream directly to a .part file; rename on success to avoid partial files.
|
|
let part_path = file_path.with_extension(format!("{suffix}.part"));
|
|
if let Err(e) = stream_to_file(response, &part_path).await {
|
|
let _ = tokio::fs::remove_file(&part_path).await;
|
|
return Err(e);
|
|
}
|
|
tokio::fs::rename(&part_path, &file_path)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let app_seed = app.clone();
|
|
let tid = track_id.clone();
|
|
let fp = file_path.clone();
|
|
tokio::spawn(async move {
|
|
enqueue_analysis_seed_from_file(&app_seed, &tid, &fp).await;
|
|
});
|
|
|
|
let size = tokio::fs::metadata(&file_path)
|
|
.await
|
|
.map(|m| m.len())
|
|
.unwrap_or(0);
|
|
crate::app_deprintln!(
|
|
"[hot-cache] download http_done track_id={} server_id={} bytes={}",
|
|
track_id,
|
|
server_id,
|
|
size
|
|
);
|
|
Ok(HotCacheDownloadResult {
|
|
path: path_str,
|
|
size,
|
|
})
|
|
}
|
|
|
|
/// Promotes bytes captured by the manual streaming path into hot cache on disk.
|
|
/// Returns `Ok(None)` when no completed stream cache is available for this URL.
|
|
#[tauri::command]
|
|
pub(crate) async fn promote_stream_cache_to_hot_cache(
|
|
track_id: String,
|
|
server_id: String,
|
|
url: String,
|
|
suffix: String,
|
|
custom_dir: Option<String>,
|
|
app: tauri::AppHandle,
|
|
state: tauri::State<'_, audio::AudioEngine>,
|
|
) -> Result<Option<HotCacheDownloadResult>, String> {
|
|
let root = resolve_hot_cache_root(custom_dir, &app)?;
|
|
let cache_dir = root.join(&server_id);
|
|
tokio::fs::create_dir_all(&cache_dir)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let file_path = cache_dir.join(format!("{}.{}", track_id, suffix));
|
|
let path_str = file_path.to_string_lossy().to_string();
|
|
|
|
if file_path.exists() {
|
|
let size = tokio::fs::metadata(&file_path)
|
|
.await
|
|
.map(|m| m.len())
|
|
.unwrap_or(0);
|
|
crate::app_deprintln!(
|
|
"[hot-cache] promote disk_hit track_id={} server_id={} bytes={}",
|
|
track_id,
|
|
server_id,
|
|
size
|
|
);
|
|
let app_seed = app.clone();
|
|
let tid = track_id.clone();
|
|
let fp = file_path.clone();
|
|
tokio::spawn(async move {
|
|
enqueue_analysis_seed_from_file(&app_seed, &tid, &fp).await;
|
|
});
|
|
return Ok(Some(HotCacheDownloadResult { path: path_str, size }));
|
|
}
|
|
|
|
let bytes = match audio::take_stream_completed_for_url(&state, &url) {
|
|
Some(b) => b,
|
|
None => {
|
|
crate::app_deprintln!(
|
|
"[hot-cache] promote skip track_id={} reason=no_completed_stream_for_url",
|
|
track_id
|
|
);
|
|
return Ok(None);
|
|
}
|
|
};
|
|
|
|
let part_path = file_path.with_extension(format!("{suffix}.part"));
|
|
if let Err(e) = tokio::fs::write(&part_path, &bytes).await {
|
|
let _ = tokio::fs::remove_file(&part_path).await;
|
|
return Err(e.to_string());
|
|
}
|
|
tokio::fs::rename(&part_path, &file_path)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
|
|
let _ = enqueue_analysis_seed(&app, &track_id, &bytes).await;
|
|
|
|
let size = tokio::fs::metadata(&file_path)
|
|
.await
|
|
.map(|m| m.len())
|
|
.unwrap_or(0);
|
|
crate::app_deprintln!(
|
|
"[hot-cache] promote from_stream track_id={} server_id={} bytes={}",
|
|
track_id,
|
|
server_id,
|
|
size
|
|
);
|
|
Ok(Some(HotCacheDownloadResult { path: path_str, size }))
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) async fn get_hot_cache_size(custom_dir: Option<String>, app: tauri::AppHandle) -> u64 {
|
|
resolve_hot_cache_root(custom_dir, &app)
|
|
.map(|root| super::fs_utils::dir_size_recursive(&root))
|
|
.unwrap_or(0)
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub(crate) async fn delete_hot_cache_track(
|
|
local_path: String,
|
|
custom_dir: Option<String>,
|
|
app: tauri::AppHandle,
|
|
) -> Result<(), String> {
|
|
let file_path = std::path::PathBuf::from(&local_path);
|
|
let existed = file_path.exists();
|
|
if existed {
|
|
tokio::fs::remove_file(&file_path)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
}
|
|
crate::app_deprintln!(
|
|
"[hot-cache] delete file existed={} path_suffix={}",
|
|
existed,
|
|
file_path
|
|
.file_name()
|
|
.and_then(|s| s.to_str())
|
|
.unwrap_or("?")
|
|
);
|
|
|
|
let boundary = resolve_hot_cache_root(custom_dir, &app)?;
|
|
if let Some(parent) = file_path.parent() {
|
|
super::fs_utils::prune_empty_dirs_up_to(parent, &boundary);
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Removes the entire hot cache root (`psysonic-hot-cache` for the active location).
|
|
#[tauri::command]
|
|
pub(crate) async fn purge_hot_cache(custom_dir: Option<String>, app: tauri::AppHandle) -> Result<(), String> {
|
|
let root = resolve_hot_cache_root(custom_dir, &app)?;
|
|
if !root.exists() {
|
|
return Ok(());
|
|
}
|
|
tokio::fs::remove_dir_all(&root)
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
crate::app_deprintln!(
|
|
"[hot-cache] purge root={} status=ok",
|
|
root.to_string_lossy()
|
|
);
|
|
Ok(())
|
|
}
|