use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use crate::config::Config;
use crate::db::connection::Database;
use crate::db::queries;
use crate::db::queries::shares::{ShareKind, Slice};
use crate::player::commands::PlayerCommand;
use crate::player::state::{ItemState, PlaylistItem, QueueItemId, SharedPlayerState};
use crate::remote::client::{SubsonicAuth, SubsonicClient};
pub fn get_remote_password(cfg: &Config) -> Option<String> {
(!cfg.remote.password.is_empty()).then(|| cfg.remote.password.clone())
}
pub fn spawn_library_watch(
db_path: std::path::PathBuf,
on_state: impl Fn(bool) + Send + Sync + 'static,
) -> Option<std::thread::JoinHandle<()>> {
use notify::{RecursiveMode, Watcher};
std::thread::Builder::new()
.name("koan-library-watch".into())
.spawn(move || {
let scan_now = |reason: &str| {
let cfg = Config::load().unwrap_or_default();
if cfg.library.folders.is_empty() {
return;
}
let Ok(db) = Database::open(&db_path) else {
return;
};
on_state(true);
let result = crate::index::scanner::full_scan(
&db,
&cfg.library.folders,
crate::index::scanner::ScanOptions::default(),
None,
);
on_state(false);
log::info!(
"{reason} scan: {} added, {} updated, {} removed, {} unchanged",
result.added,
result.updated,
result.removed,
result.skipped
);
};
std::thread::sleep(std::time::Duration::from_secs(3));
scan_now("startup");
let (tx, rx) = std::sync::mpsc::channel();
let Ok(mut watcher) = notify::recommended_watcher(move |event| {
let _ = tx.send(event);
}) else {
log::warn!("could not watch the library folders");
return;
};
let cfg = Config::load().unwrap_or_default();
for folder in &cfg.library.folders {
if let Err(e) = watcher.watch(folder, RecursiveMode::Recursive) {
log::warn!("could not watch {}: {e}", folder.display());
}
}
const SETTLE: std::time::Duration = std::time::Duration::from_secs(5);
const RESCAN: std::time::Duration = std::time::Duration::from_secs(15 * 60);
loop {
match rx.recv_timeout(RESCAN) {
Ok(Err(_)) => continue,
Ok(Ok(_)) => {
while rx.recv_timeout(SETTLE).is_ok() {}
scan_now("watched change");
}
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => scan_now("periodic"),
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
}
}
})
.ok()
}
pub fn spawn_auto_sync(
db_path: std::path::PathBuf,
on_state: impl Fn(bool) + Send + 'static,
on_progress: impl Fn(crate::remote::sync::SyncProgress) + Send + Sync + 'static,
) -> Option<std::thread::JoinHandle<()>> {
std::thread::Builder::new()
.name("koan-auto-sync".into())
.spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(5));
loop {
let cfg = Config::load().unwrap_or_default();
if !cfg.remote.enabled || !cfg.remote.auto_sync {
std::thread::sleep(std::time::Duration::from_secs(60));
continue;
}
if let Some(client) = subsonic_client(&cfg)
&& let Ok(db) = Database::open(&db_path)
{
on_state(true);
match sync_remote(
&db,
&client,
false,
&cfg.remote.url,
&cfg.remote.username,
&on_progress,
) {
Ok(s) => log::info!(
"auto sync: {} artists, {} albums, {} tracks ({} albums failed); \
favourites {}↑ {}↓; playlists {}↓ {}↑",
s.library.artists_synced,
s.library.albums_synced,
s.library.tracks_synced,
s.library.albums_failed,
s.favourites.pushed,
s.favourites.imported,
s.playlists.pulled,
s.playlists.pushed,
),
Err(e) => log::warn!("auto sync failed: {e}"),
}
on_state(false);
}
match cfg.remote.auto_sync_interval_mins {
0 => return,
mins => std::thread::sleep(std::time::Duration::from_secs(mins * 60)),
}
}
})
.ok()
}
#[derive(Debug, Clone, Copy, Default)]
pub struct RebuildSummary {
pub tracks: u64,
pub albums: u64,
pub artists: u64,
}
pub fn rebuild_index(db: &Database) -> Result<RebuildSummary, crate::db::connection::DbError> {
let count = |sql: &str| -> u64 {
db.conn
.query_row(sql, [], |r| r.get::<_, i64>(0))
.unwrap_or(0) as u64
};
let summary = RebuildSummary {
tracks: count("SELECT COUNT(*) FROM tracks"),
albums: count("SELECT COUNT(*) FROM albums"),
artists: count("SELECT COUNT(*) FROM artists"),
};
db.conn.execute_batch(
"BEGIN;
DELETE FROM track_vectors;
DELETE FROM lyrics_cache;
DELETE FROM play_history;
DELETE FROM scan_cache;
DELETE FROM tracks_fts;
DELETE FROM tracks;
DELETE FROM similar_artists;
DELETE FROM albums;
DELETE FROM artists;
COMMIT;",
)?;
let _ = db.conn.execute_batch("VACUUM");
Ok(summary)
}
pub fn cache_size_bytes(cfg: &Config) -> u64 {
walkdir::WalkDir::new(cfg.cache_dir())
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.filter_map(|e| e.metadata().ok())
.map(|m| m.len())
.sum()
}
pub fn tracks_under(db: &Database, folder: &Path) -> u64 {
let (lower, upper) = queries::folder_prefix_range(folder);
db.conn
.query_row(
"SELECT COUNT(*) FROM tracks WHERE path >= ?1 AND path < ?2",
[&lower, &upper],
|r| r.get::<_, i64>(0),
)
.unwrap_or(0) as u64
}
pub fn tracks_from_server(db: &Database) -> u64 {
db.conn
.query_row(
"SELECT COUNT(*) FROM tracks WHERE remote_id IS NOT NULL",
[],
|r| r.get::<_, i64>(0),
)
.unwrap_or(0) as u64
}
pub fn forget_folder(db: &Database, folder: &Path) -> Result<u64, crate::db::connection::DbError> {
let folder = &crate::index::spelling::on_disk(folder);
let (lower, upper) = queries::folder_prefix_range(folder);
let tx = db.conn.unchecked_transaction()?;
tx.execute(
"UPDATE tracks SET path = NULL, source = 'remote'
WHERE path >= ?1 AND path < ?2 AND remote_id IS NOT NULL",
[&lower, &upper],
)?;
let ids: Vec<i64> = {
let mut stmt = tx.prepare("SELECT id FROM tracks WHERE path >= ?1 AND path < ?2")?;
let rows = stmt.query_map([&lower, &upper], |r| r.get(0))?;
rows.filter_map(Result::ok).collect()
};
for id in &ids {
tx.execute("DELETE FROM track_vectors WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM lyrics_cache WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM play_history WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM scan_cache WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM tracks_fts WHERE rowid = ?1", [id])?;
tx.execute("DELETE FROM tracks WHERE id = ?1", [id])?;
}
prune_empty_albums_and_artists(&tx)?;
tx.commit()?;
Ok(ids.len() as u64)
}
pub fn forget_remote(db: &Database) -> Result<u64, crate::db::connection::DbError> {
let tx = db.conn.unchecked_transaction()?;
let ids: Vec<i64> = {
let mut stmt =
tx.prepare("SELECT id FROM tracks WHERE remote_id IS NOT NULL AND path IS NULL")?;
let rows = stmt.query_map([], |r| r.get(0))?;
rows.filter_map(Result::ok).collect()
};
for id in &ids {
tx.execute("DELETE FROM track_vectors WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM lyrics_cache WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM play_history WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM scan_cache WHERE track_id = ?1", [id])?;
tx.execute("DELETE FROM tracks_fts WHERE rowid = ?1", [id])?;
tx.execute("DELETE FROM tracks WHERE id = ?1", [id])?;
}
tx.execute(
"UPDATE tracks SET remote_id = NULL, remote_url = NULL, source = 'local'
WHERE remote_id IS NOT NULL",
[],
)?;
tx.execute("DELETE FROM similar_artists", [])?;
prune_empty_albums_and_artists(&tx)?;
tx.commit()?;
Ok(ids.len() as u64)
}
fn prune_empty_albums_and_artists(
tx: &rusqlite::Transaction<'_>,
) -> Result<(), crate::db::connection::DbError> {
tx.execute(
"DELETE FROM albums WHERE NOT EXISTS
(SELECT 1 FROM tracks WHERE tracks.album_id = albums.id)",
[],
)?;
tx.execute(
"DELETE FROM similar_artists WHERE NOT EXISTS
(SELECT 1 FROM albums WHERE albums.artist_id = similar_artists.artist_id)",
[],
)?;
tx.execute(
"DELETE FROM artists WHERE NOT EXISTS
(SELECT 1 FROM albums WHERE albums.artist_id = artists.id)
AND NOT EXISTS
(SELECT 1 FROM tracks WHERE tracks.artist_id = artists.id)",
[],
)?;
Ok(())
}
#[derive(Debug, Clone, Copy, Default)]
pub struct CacheCleared {
pub files: u64,
pub bytes: u64,
}
pub fn clear_download_cache(db: &Database, cfg: &Config) -> CacheCleared {
let dir = cfg.cache_dir();
let mut cleared = CacheCleared::default();
for entry in walkdir::WalkDir::new(&dir)
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
{
if let Ok(meta) = entry.metadata() {
cleared.bytes += meta.len();
cleared.files += 1;
}
}
let _ = std::fs::remove_dir_all(&dir);
let _ = std::fs::create_dir_all(&dir);
let _ = queries::clear_cached_paths(&db.conn);
cleared
}
pub fn clear_downloads_for(db: &Database, track_ids: &[i64]) -> CacheCleared {
let mut cleared = CacheCleared::default();
let paths = match queries::cached_paths_for(&db.conn, track_ids) {
Ok(paths) => paths,
Err(e) => {
log::warn!("could not read cached paths: {e}");
return cleared;
}
};
for path in &paths {
let size = std::fs::metadata(path).map(|m| m.len()).unwrap_or(0);
match std::fs::remove_file(path) {
Ok(()) => {
cleared.files += 1;
cleared.bytes += size;
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => log::warn!("could not remove {path}: {e}"),
}
}
if let Err(e) = queries::clear_cached_paths_for(&db.conn, track_ids) {
log::warn!("removed downloads but failed to forget them ({e})");
}
cleared
}
pub fn sweep_partial_downloads(cfg: &Config) -> CacheCleared {
let mut swept = CacheCleared::default();
for entry in walkdir::WalkDir::new(cfg.cache_dir())
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_file())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "part"))
{
let size = entry.metadata().map(|m| m.len()).unwrap_or(0);
match std::fs::remove_file(entry.path()) {
Ok(()) => {
swept.files += 1;
swept.bytes += size;
}
Err(e) => log::warn!("could not remove {}: {e}", entry.path().display()),
}
}
if swept.files > 0 {
log::info!(
"swept {} unfinished download(s), {} bytes",
swept.files,
swept.bytes
);
}
swept
}
pub fn requeue_cleared_downloads(
state: &Arc<SharedPlayerState>,
tx: &crossbeam_channel::Sender<PlayerCommand>,
) {
let stale = state.reset_items_with_missing_files();
if stale.is_empty() {
return;
}
log::info!(
"{} queued tracks lost their copy — fetching again",
stale.len()
);
spawn_downloads(stale, tx.clone(), state.clone());
}
pub fn sync_favourite_to_remote(db: &Database, path: &Path, star: bool) {
let cfg = Config::load().unwrap_or_default();
if !cfg.remote.enabled {
return;
}
let Ok(Some(remote_id)) = queries::remote_id_for_path(&db.conn, path) else {
log::warn!("not syncing favourite: {} has no remote id", path.display());
return;
};
let Some(client) = subsonic_client(&cfg) else {
log::warn!("not syncing favourite: no usable server credentials");
return;
};
std::thread::Builder::new()
.name("koan-fav-sync".into())
.spawn(move || {
let result = if star {
client.star(&remote_id)
} else {
client.unstar(&remote_id)
};
match result {
Ok(()) => log::info!("synced favourite to remote: {remote_id} = {star}"),
Err(e) => log::warn!("failed to sync favourite to remote: {e}"),
}
})
.ok();
}
#[derive(Debug, Default)]
pub struct FullSync {
pub library: crate::remote::sync::SyncResult,
pub favourites: FavouriteSync,
pub playlists: crate::playlists::PlaylistSync,
}
pub fn sync_remote(
db: &Database,
client: &SubsonicClient,
full: bool,
url: &str,
username: &str,
progress: &(dyn Fn(crate::remote::sync::SyncProgress) + Sync),
) -> Result<FullSync, crate::remote::sync::SyncError> {
let library = crate::remote::sync::sync_library(db, client, full, url, username, progress)?;
Ok(FullSync {
library,
favourites: reconcile_favourites(db, client),
playlists: crate::playlists::reconcile_playlists(db, client, username),
})
}
#[derive(Debug, Default, Clone, Copy)]
pub struct FavouriteSync {
pub pushed: usize,
pub imported: usize,
}
pub fn reconcile_favourites(db: &Database, client: &SubsonicClient) -> FavouriteSync {
let mut out = FavouriteSync::default();
let tracks = queries::favourites_with_remote_id(&db.conn).unwrap_or_default();
for (_path, remote_id) in &tracks {
if client.star(remote_id).is_ok() {
out.pushed += 1;
}
}
for (_id, remote_id) in queries::favourite_albums_with_remote_id(&db.conn).unwrap_or_default() {
if client.star_album(&remote_id).is_ok() {
out.pushed += 1;
}
}
for (_id, remote_id) in queries::favourite_artists_with_remote_id(&db.conn).unwrap_or_default()
{
if client.star_artist(&remote_id).is_ok() {
out.pushed += 1;
}
}
let starred = match client.get_starred_all() {
Ok(s) => s,
Err(e) => {
log::warn!("could not fetch starred items from the server: {e}");
return out;
}
};
let songs: Vec<String> = starred.song.into_iter().map(|s| s.id).collect();
let albums: Vec<String> = starred.album.into_iter().map(|a| a.id).collect();
let artists: Vec<String> = starred.artist.into_iter().map(|a| a.id).collect();
out.imported += queries::import_remote_favourites(&db.conn, &songs).unwrap_or(0);
out.imported += queries::import_remote_favourite_albums(&db.conn, &albums).unwrap_or(0);
out.imported += queries::import_remote_favourite_artists(&db.conn, &artists).unwrap_or(0);
out
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FavouriteKind {
Track,
Album,
Artist,
}
pub fn sync_collection_favourite_to_remote(
db: &Database,
kind: FavouriteKind,
id: i64,
star: bool,
) {
let cfg = Config::load().unwrap_or_default();
if !cfg.remote.enabled {
return;
}
let remote_id = match kind {
FavouriteKind::Album => queries::album_remote_id(&db.conn, id),
FavouriteKind::Artist => queries::artist_remote_id(&db.conn, id),
FavouriteKind::Track => return,
};
let Ok(Some(remote_id)) = remote_id else {
log::warn!("not syncing favourite: {kind:?} {id} has no remote id");
return;
};
let Some(client) = subsonic_client(&cfg) else {
log::warn!("not syncing favourite: no usable server credentials");
return;
};
std::thread::Builder::new()
.name("koan-fav-sync".into())
.spawn(move || {
let result = match (kind, star) {
(FavouriteKind::Album, true) => client.star_album(&remote_id),
(FavouriteKind::Album, false) => client.unstar_album(&remote_id),
(FavouriteKind::Artist, true) => client.star_artist(&remote_id),
(FavouriteKind::Artist, false) => client.unstar_artist(&remote_id),
(FavouriteKind::Track, _) => Ok(()),
};
match result {
Ok(()) => log::info!("synced favourite to remote: {kind:?} {remote_id} = {star}"),
Err(e) => log::warn!("failed to sync favourite to remote: {e}"),
}
})
.ok();
}
#[derive(Debug, thiserror::Error)]
pub enum SignInError {
#[error("the server did not accept those credentials: {0}")]
Rejected(#[from] crate::remote::client::SubsonicError),
#[error("could not write the configuration: {0}")]
Config(#[from] crate::config::ConfigError),
}
pub fn set_remote_credentials(
url: &str,
username: &str,
password: &str,
) -> Result<(), SignInError> {
let url = url.trim_end_matches('/');
SubsonicClient::new(url, username, password).ping()?;
Config::persist(|cfg| {
cfg.remote.enabled = true;
cfg.remote.url = url.to_string();
cfg.remote.username = username.to_string();
cfg.remote.password = password.to_string();
})?;
Ok(())
}
pub fn get_subsonic_password(cfg: &Config) -> Option<String> {
(!cfg.subsonic.password.is_empty()).then(|| cfg.subsonic.password.clone())
}
pub fn subsonic_auth(cfg: &Config) -> Option<SubsonicAuth> {
if !cfg.remote.enabled || cfg.remote.url.is_empty() {
return None;
}
let password = get_remote_password(cfg)?;
Some(SubsonicAuth::new(
&cfg.remote.url,
&cfg.remote.username,
&password,
))
}
pub fn subsonic_client(cfg: &Config) -> Option<Arc<SubsonicClient>> {
let auth = subsonic_auth(cfg)?;
let mut slot = SUBSONIC_CLIENT.lock();
if let Some((cached, client)) = slot.as_ref()
&& *cached == auth
{
return Some(client.clone());
}
let client = Arc::new(SubsonicClient::from_auth(auth.clone()));
*slot = Some((auth, client.clone()));
Some(client)
}
type CachedClient = Option<(SubsonicAuth, Arc<SubsonicClient>)>;
static SUBSONIC_CLIENT: std::sync::LazyLock<parking_lot::Mutex<CachedClient>> =
std::sync::LazyLock::new(|| parking_lot::Mutex::new(None));
#[derive(Debug, thiserror::Error)]
pub enum ShareError {
#[error("no remote server is configured")]
NoRemote,
#[error("sharing.public_url is not set, so there is no address to give out")]
NoPublicUrl,
#[error("none of these tracks are in the library")]
NothingToShare,
#[error("none of these tracks are on the server, so a link has nothing to point at")]
NothingRemote,
#[error("the server refused to share these: {0}")]
Server(#[from] crate::remote::client::SubsonicError),
#[error(transparent)]
Database(#[from] crate::db::connection::DbError),
}
#[derive(Debug, Clone)]
pub struct ShareOutcome {
pub url: String,
pub id: String,
pub shared: usize,
pub skipped: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShareTarget {
Tracks(Vec<i64>),
Album {
album_id: i64,
start_track_id: Option<i64>,
},
Artist(i64),
}
pub fn resolve_share(
conn: &rusqlite::Connection,
target: &ShareTarget,
) -> Result<(Slice, Vec<i64>), ShareError> {
let album_tracks = |album_id| -> Result<Vec<i64>, ShareError> {
Ok(queries::tracks_for_album(conn, album_id)?
.into_iter()
.map(|t| t.id)
.collect())
};
let (slice, ids) = match target {
ShareTarget::Tracks(ids) => {
let rows = queries::tracks_by_ids(conn, ids)?;
match (ids.as_slice(), rows.first().and_then(|t| t.album_id)) {
([one], Some(album_id)) => {
return resolve_share(
conn,
&ShareTarget::Album {
album_id,
start_track_id: Some(*one),
},
);
}
_ => {
let ids = ids
.iter()
.copied()
.filter(|id| rows.iter().any(|t| t.id == *id))
.collect();
(Slice::TRACKS, ids)
}
}
}
ShareTarget::Album {
album_id,
start_track_id,
} => {
let ids = album_tracks(*album_id)?;
let slice = Slice {
kind: ShareKind::Album,
subject_id: Some(*album_id),
start_track_id: start_track_id.filter(|s| ids.contains(s)),
};
(slice, ids)
}
ShareTarget::Artist(artist_id) => {
let mut ids = Vec::new();
for album in queries::albums_for_artist(conn, *artist_id)? {
ids.extend(album_tracks(album.id)?);
}
let slice = Slice {
kind: ShareKind::Artist,
subject_id: Some(*artist_id),
start_track_id: None,
};
(slice, ids)
}
};
if ids.is_empty() {
return Err(ShareError::NothingToShare);
}
Ok((slice, ids))
}
pub fn create_share(
db: &Database,
cfg: &Config,
target: &ShareTarget,
description: Option<&str>,
) -> Result<ShareOutcome, ShareError> {
let Some(client) = subsonic_client(cfg) else {
return create_native_share(db, cfg, target, description);
};
let resolved;
let track_ids = match target {
ShareTarget::Tracks(ids) => ids.as_slice(),
_ => {
resolved = resolve_share(&db.conn, target)?.1;
resolved.as_slice()
}
};
let rows = queries::tracks_by_ids(&db.conn, track_ids)?;
let shared = rows.iter().filter(|t| t.remote_id.is_some()).count();
if shared == 0 {
return Err(ShareError::NothingRemote);
}
let one_album = rows
.first()
.and_then(|f| f.album_id)
.filter(|first| rows.iter().all(|t| t.album_id == Some(*first)))
.and_then(|album_id| album_remote_id(&db.conn, album_id, rows.len()));
let remote_ids: Vec<String> = match one_album {
Some(rid) => vec![album_share_id(&client, rid)],
None => rows.into_iter().filter_map(|t| t.remote_id).collect(),
};
let refs: Vec<&str> = remote_ids.iter().map(String::as_str).collect();
let share = client.create_share(&refs, description)?;
let url = share
.url
.clone()
.unwrap_or_else(|| format!("{}/s/{}", client.base_url(), share.id));
Ok(ShareOutcome {
url,
id: share.id,
shared,
skipped: track_ids.len().saturating_sub(shared),
})
}
fn create_native_share(
db: &Database,
cfg: &Config,
target: &ShareTarget,
description: Option<&str>,
) -> Result<ShareOutcome, ShareError> {
let base = cfg
.sharing
.public_url
.as_deref()
.filter(|u| !u.trim().is_empty())
.ok_or(ShareError::NoPublicUrl)?;
let (slice, ids) = resolve_share(&db.conn, target)?;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs() as i64);
let share = queries::shares::create_share(&db.conn, slice, &ids, description, now, None)?;
Ok(ShareOutcome {
url: share_url(base, &share.id),
id: share.id,
shared: ids.len(),
skipped: match (target, slice.kind) {
(ShareTarget::Tracks(asked), ShareKind::Tracks) => asked.len() - ids.len(),
_ => 0,
},
})
}
pub fn share_url(public_url: &str, id: &str) -> String {
format!("{}/share/{id}", public_url.trim_end_matches('/'))
}
fn album_share_id(client: &crate::remote::client::SubsonicClient, remote_id: String) -> String {
let koan = matches!(client.server_type(), Ok(Some(t)) if t == "koan");
album_share_id_for(koan, remote_id)
}
fn album_share_id_for(koan: bool, remote_id: String) -> String {
if koan && remote_id.parse::<i64>().is_ok() {
format!("al-{remote_id}")
} else {
remote_id
}
}
fn album_remote_id(conn: &rusqlite::Connection, album_id: i64, selected: usize) -> Option<String> {
let (remote_id, total): (Option<String>, i64) = conn
.query_row(
"SELECT al.remote_id, (SELECT COUNT(*) FROM tracks WHERE album_id = al.id)
FROM albums al WHERE al.id = ?1",
[album_id],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.ok()?;
(total == selected as i64).then_some(remote_id).flatten()
}
pub fn shuffle<T>(items: &mut [T]) {
let mut seed = [0u8; 8];
if getrandom::fill(&mut seed).is_err() {
return; }
let mut state = u64::from_le_bytes(seed) | 1;
for i in (1..items.len()).rev() {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
items.swap(i, (state % (i as u64 + 1)) as usize);
}
}
pub fn truncate_bytes(s: &str, max: usize) -> &str {
if s.len() <= max {
return s;
}
let mut end = max;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
pub fn sanitise_filename(s: &str) -> String {
let cleaned: String = s
.chars()
.map(|c| match c {
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
_ => c,
})
.collect::<String>()
.trim()
.to_string();
truncate_bytes(&cleaned, 240).trim_end().to_string()
}
pub fn year_of(date: &str) -> Option<&str> {
date.get(..4)
}
pub fn cache_path_for_track(
cache_dir: &Path,
track: &queries::TrackRow,
album_date: Option<&str>,
) -> PathBuf {
let artist_dir = sanitise_filename(&track.artist_name);
let year = album_date
.and_then(year_of)
.map(|y| format!("({}) ", y))
.unwrap_or_default();
let codec = track
.codec
.as_deref()
.map(|c| format!(" [{}]", c))
.unwrap_or_default();
let album_dir = sanitise_filename(&format!("{}{}{}", year, track.album_title, codec));
let disc_prefix = match track.disc {
Some(d) if d > 1 => format!("{}-", d),
_ => String::new(),
};
let track_num = track
.track_number
.map(|n| format!("{:02}. ", n))
.unwrap_or_default();
let ext = track
.codec
.as_deref()
.map(|c| c.to_lowercase())
.unwrap_or_else(|| "flac".into());
let filename = sanitise_filename(&format!(
"{}{}{} - {}",
disc_prefix, track_num, track.artist_name, track.title
));
cache_dir
.join(artist_dir)
.join(album_dir)
.join(format!("{}.{}", filename, ext))
}
pub fn resolve_item_path(
db: &Database,
cfg: &Config,
id: i64,
track: &queries::TrackRow,
album_date: Option<&str>,
) -> (PathBuf, ItemState) {
match queries::resolve_playback_path(&db.conn, id) {
Ok(Some(queries::PlaybackSource::Local(p))) => (p, ItemState::Ready),
Ok(Some(queries::PlaybackSource::Cached(p))) => {
let state = if is_cached_audio(&p) {
ItemState::Ready
} else {
ItemState::Pending
};
(p, state)
}
Ok(Some(queries::PlaybackSource::Remote(_))) => {
let dest = cache_path_for_track(&cfg.cache_dir(), track, album_date);
if dest.exists() && is_cached_audio(&dest) {
(dest, ItemState::Ready)
} else {
(dest, ItemState::Pending)
}
}
_ => {
let dest = cache_path_for_track(&cfg.cache_dir(), track, album_date);
(dest, ItemState::Pending)
}
}
}
pub fn playlist_item_from_track(
track: &queries::TrackRow,
album_date: Option<&str>,
dest: PathBuf,
state: ItemState,
) -> PlaylistItem {
let year = album_date.and_then(year_of).map(str::to_string);
PlaylistItem {
playlist_entry_id: None,
id: QueueItemId::new(),
db_id: Some(track.id),
path: dest,
title: track.title.clone(),
artist: track.artist_name.clone(),
album_artist: track.album_artist_name.clone(),
album: track.album_title.clone(),
year,
codec: track.codec.clone(),
track_number: track.track_number.map(|n| n as i64),
disc: track.disc.map(|n| n as i64),
duration_ms: track.duration_ms.map(|d| d as u64),
state,
}
}
pub fn playlist_items_for_tracks(db: &Database, tracks: &[queries::TrackRow]) -> Vec<PlaylistItem> {
use std::collections::HashMap;
let cfg = Config::load().unwrap_or_default();
let mut album_dates: HashMap<i64, Option<String>> = HashMap::new();
tracks
.iter()
.map(|track| {
let album_date = match track.album_id {
Some(aid) => album_dates
.entry(aid)
.or_insert_with(|| queries::album_date(&db.conn, aid).ok().flatten())
.clone(),
None => None,
};
let (path, state) = resolve_item_path(db, &cfg, track.id, track, album_date.as_deref());
playlist_item_from_track(track, album_date.as_deref(), path, state)
})
.collect()
}
pub fn track_to_playlist_item(track: &queries::TrackRow, db: &Database) -> PlaylistItem {
let album_date = track
.album_id
.and_then(|aid| queries::album_date(&db.conn, aid).ok().flatten());
let cfg = Config::load().unwrap_or_default();
let (path, state) = resolve_item_path(db, &cfg, track.id, track, album_date.as_deref());
let year = album_date.as_deref().and_then(year_of).map(str::to_string);
PlaylistItem {
playlist_entry_id: None,
id: QueueItemId::new(),
db_id: Some(track.id),
path,
title: track.title.clone(),
artist: track.artist_name.clone(),
album_artist: track.album_artist_name.clone(),
album: track.album_title.clone(),
year,
codec: track.codec.clone(),
track_number: track.track_number.map(|n| n as i64),
disc: track.disc.map(|n| n as i64),
duration_ms: track.duration_ms.map(|d| d as u64),
state,
}
}
fn is_cached_audio(path: &std::path::Path) -> bool {
const MIN_PLAUSIBLE_BYTES: u64 = 4096;
match std::fs::metadata(path) {
Ok(meta) if meta.len() >= MIN_PLAUSIBLE_BYTES => true,
Ok(_) => {
let mut first = [0u8; 1];
match std::fs::File::open(path)
.and_then(|mut f| std::io::Read::read_exact(&mut f, &mut first).map(|_| first[0]))
{
Ok(b) => b != b'{' && b != b'<',
Err(_) => false,
}
}
Err(_) => false,
}
}
pub fn download_track(
db_id: i64,
queue_id: QueueItemId,
tx: &crossbeam_channel::Sender<PlayerCommand>,
log_buf: &Arc<Mutex<Vec<String>>>,
state: &Arc<SharedPlayerState>,
cfg: &Config,
client: &SubsonicClient,
) {
let db = match crate::db::pool::shared().get() {
Ok(db) => db,
Err(e) => {
fail_track(state, tx, queue_id, format!("db error: {}", e));
return;
}
};
let track = match queries::get_track_row(&db.conn, db_id) {
Ok(Some(t)) => t,
_ => {
fail_track(state, tx, queue_id, "track not found".into());
return;
}
};
let remote_id = match &track.remote_id {
Some(rid) => rid.clone(),
None => {
if let Some(ref path) = track.path {
let p = std::path::PathBuf::from(path);
if p.exists() {
state.update_paths(&[(queue_id, p)]);
state.update_item_state(queue_id, ItemState::Ready);
if state.is_cursor(queue_id) {
tx.send(PlayerCommand::TrackReady(queue_id)).ok();
}
return;
}
}
fail_track(
state,
tx,
queue_id,
"not in the library folder, and no remote copy to fetch".into(),
);
return;
}
};
if let Some(ref local_path) = track.path {
let p = std::path::PathBuf::from(local_path);
if p.exists() {
log::info!("download_track: local file exists, using {}", p.display());
state.update_paths(&[(queue_id, p)]);
state.update_item_state(queue_id, ItemState::Ready);
if state.is_cursor(queue_id) {
tx.send(PlayerCommand::TrackReady(queue_id)).ok();
}
return;
}
}
let album_date: Option<String> = track
.album_id
.and_then(|aid| queries::album_date(&db.conn, aid).ok().flatten());
let dest = cache_path_for_track(&cfg.cache_dir(), &track, album_date.as_deref());
if dest.exists() && !is_cached_audio(&dest) {
log::warn!(
"discarding non-audio cache entry {} (likely a stored server error)",
dest.display()
);
let _ = std::fs::remove_file(&dest);
}
if dest.exists() {
state.update_paths(&[(queue_id, dest)]);
state.update_item_state(queue_id, ItemState::Ready);
if state.is_cursor(queue_id) {
tx.send(PlayerCommand::TrackReady(queue_id)).ok();
}
return;
}
state.update_paths(&[(queue_id, crate::remote::download::part_path(&dest))]);
let bytes_written = crate::remote::downloads::ByteFeed::new();
let store = crate::remote::downloads::store();
store.queued(crate::remote::downloads::Download {
id: queue_id,
track_id: db_id,
title: track.title.clone(),
artist: track.artist_name.clone(),
source: crate::remote::download::part_path(&dest),
dest: dest.clone(),
total: 0,
written: bytes_written.clone(),
state: crate::remote::downloads::DownloadState::Queued,
bytes_per_second: 0,
});
let progress_qid = queue_id;
let bytes_written_progress = bytes_written.clone();
let progress_tx = tx.clone();
let stream_ready_sent = Arc::new(std::sync::atomic::AtomicBool::new(false));
let stream_ready_flag = stream_ready_sent.clone();
let announced_total = AtomicU64::new(u64::MAX);
let result = client.download_with_progress(&remote_id, &dest, move |downloaded, total| {
bytes_written_progress.set(downloaded);
store.progressed();
if announced_total.swap(total, Ordering::Relaxed) != total {
store.started(progress_qid, total, bytes_written_progress.clone());
}
if !stream_ready_flag.load(Ordering::Relaxed)
&& downloaded >= crate::player::state::STREAM_THRESHOLD
{
stream_ready_flag.store(true, Ordering::Relaxed);
progress_tx
.send(PlayerCommand::TrackStreamReady(progress_qid))
.ok();
}
});
if let Err(e) = result {
store.failed(queue_id, e.to_string());
fail_track(state, tx, queue_id, e.to_string());
bytes_written.done();
push_log(log_buf, format!("x {} — {}", track.title, e));
return;
}
store.finished(queue_id);
state.update_paths(&[(queue_id, dest.clone())]);
state.update_item_state(queue_id, ItemState::Ready);
bytes_written.done();
if let Err(e) = queries::set_cached_path(&db.conn, db_id, &dest.to_string_lossy()) {
log::warn!(
"cached {} but failed to record it ({}) — it will not be evicted",
dest.display(),
e
);
}
push_log(
log_buf,
format!("+ {} — {}", track.title, track.artist_name),
);
if state.is_cursor(queue_id) {
tx.send(PlayerCommand::TrackReady(queue_id)).ok();
}
}
pub(crate) fn fail_track(
state: &Arc<SharedPlayerState>,
tx: &crossbeam_channel::Sender<PlayerCommand>,
queue_id: QueueItemId,
reason: String,
) {
state.update_item_state(queue_id, ItemState::Failed(reason));
if state.is_cursor(queue_id) {
tx.send(PlayerCommand::TrackFailed(queue_id)).ok();
}
}
fn push_log(log_buf: &Arc<Mutex<Vec<String>>>, msg: String) {
match log_buf.lock() {
Ok(mut buf) => buf.push(msg),
Err(_) => log::info!("{}", msg),
}
}
pub fn remote_unavailable(cfg: &Config) -> String {
if !cfg.remote.enabled {
return "no remote server is configured".into();
}
if cfg.remote.url.is_empty() {
return "the remote server has no address".into();
}
if get_remote_password(cfg).is_none() {
return "no password is stored for the remote server".into();
}
"the remote server could not be reached".into()
}
pub fn spawn_downloads(
pending: Vec<(i64, QueueItemId)>,
tx: crossbeam_channel::Sender<PlayerCommand>,
state: Arc<SharedPlayerState>,
) {
if pending.is_empty() {
return;
}
crate::remote::queue::shared(&tx, &state, None).enqueue(pending);
}
#[cfg(test)]
mod year_tests {
use super::year_of;
#[test]
fn a_year_is_the_first_four_characters_when_they_are_bytes_too() {
assert_eq!(year_of("1997-05-21"), Some("1997"));
assert_eq!(year_of("199"), None);
assert_eq!(year_of("1997"), None);
}
}
#[cfg(test)]
mod rebuild_tests {
use super::*;
use crate::db::queries::sample_meta;
fn test_db() -> Database {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "on").unwrap();
crate::db::schema::create_tables(&conn).unwrap();
Database { conn }
}
#[test]
fn clearing_one_download_leaves_the_others_and_the_library_alone() {
let dir = tempfile::tempdir().unwrap();
let db = test_db();
let mut cached = Vec::new();
for name in ["one", "two"] {
let mut meta = sample_meta(name, "Artist", "Album");
meta.source = "remote".into();
meta.path = None;
meta.remote_id = Some(name.into());
let id = queries::upsert_track(&db.conn, &meta).unwrap();
let file = dir.path().join(format!("{name}.opus"));
std::fs::write(&file, vec![0u8; 2048]).unwrap();
queries::set_cached_path(&db.conn, id, &file.to_string_lossy()).unwrap();
cached.push((id, file));
}
let cleared = clear_downloads_for(&db, &[cached[0].0]);
assert_eq!(cleared.files, 1);
assert_eq!(cleared.bytes, 2048);
assert!(!cached[0].1.exists(), "the copy asked for is gone");
assert!(cached[1].1.exists(), "the other one is untouched");
assert_eq!(queries::library_stats(&db.conn).unwrap().remote_tracks, 2);
assert_eq!(queries::library_stats(&db.conn).unwrap().cached_tracks, 1);
assert!(
queries::cached_paths_for(&db.conn, &[cached[0].0])
.unwrap()
.is_empty()
);
}
#[test]
fn clearing_a_download_that_is_already_gone_is_not_a_failure() {
let db = test_db();
let mut meta = sample_meta("ghost", "Artist", "Album");
meta.source = "remote".into();
meta.path = None;
meta.remote_id = Some("ghost".into());
let id = queries::upsert_track(&db.conn, &meta).unwrap();
queries::set_cached_path(&db.conn, id, "/nowhere/at/all.opus").unwrap();
let cleared = clear_downloads_for(&db, &[id]);
assert_eq!(cleared.files, 0, "nothing was there to remove");
assert!(
queries::cached_paths_for(&db.conn, &[id])
.unwrap()
.is_empty()
);
}
#[test]
fn sweeping_removes_half_finished_downloads_and_nothing_else() {
let dir = tempfile::tempdir().unwrap();
let cache = dir.path().join("cache");
std::fs::create_dir_all(cache.join("Artist")).unwrap();
let finished = cache.join("Artist/whole.opus");
let half = cache.join("Artist/half.opus.part");
std::fs::write(&finished, vec![0u8; 1024]).unwrap();
std::fs::write(&half, vec![0u8; 4096]).unwrap();
let cfg = Config {
remote: crate::config::RemoteConfig {
cache_dir: Some(cache.clone()),
..Default::default()
},
..Default::default()
};
let swept = sweep_partial_downloads(&cfg);
assert_eq!(swept.files, 1);
assert_eq!(swept.bytes, 4096);
assert!(!half.exists(), "the unfinished one is gone");
assert!(finished.exists(), "a downloaded track is not touched");
}
#[test]
fn sweeping_an_empty_cache_is_not_an_error() {
let dir = tempfile::tempdir().unwrap();
let cfg = Config {
remote: crate::config::RemoteConfig {
cache_dir: Some(dir.path().join("nothing-here")),
..Default::default()
},
..Default::default()
};
assert_eq!(sweep_partial_downloads(&cfg).files, 0);
}
#[test]
fn clearing_no_tracks_does_nothing() {
let db = test_db();
assert_eq!(clear_downloads_for(&db, &[]).files, 0);
}
#[test]
fn rebuild_drops_the_index_and_keeps_favourites() {
let db = test_db();
let mut meta = sample_meta("Windowlicker", "Aphex Twin", "Windowlicker EP");
meta.path = Some("/music/windowlicker.flac".into());
let track_id = queries::upsert_track(&db.conn, &meta).unwrap();
queries::toggle_favourite(&db.conn, Path::new("/music/windowlicker.flac")).unwrap();
db.conn
.execute(
"INSERT INTO lyrics_cache (track_id, source, content, fetched_at)
VALUES (?1, 'test', 'la la la', 0)",
[track_id],
)
.unwrap();
let summary = rebuild_index(&db).unwrap();
assert_eq!(summary.tracks, 1);
assert_eq!(summary.albums, 1);
let tracks: i64 = db
.conn
.query_row("SELECT COUNT(*) FROM tracks", [], |r| r.get(0))
.unwrap();
assert_eq!(tracks, 0, "the index is gone");
let favourites: i64 = db
.conn
.query_row("SELECT COUNT(*) FROM favourites", [], |r| r.get(0))
.unwrap();
assert_eq!(favourites, 1, "favourites survive — they key on the path");
let lyrics: i64 = db
.conn
.query_row("SELECT COUNT(*) FROM lyrics_cache", [], |r| r.get(0))
.unwrap();
assert_eq!(lyrics, 0, "anything keyed on a track id cannot survive");
}
#[test]
fn rebuilding_an_empty_library_is_not_an_error() {
let db = test_db();
let summary = rebuild_index(&db).unwrap();
assert_eq!(summary.tracks, 0);
}
}
#[cfg(test)]
mod share_tests {
use super::*;
use crate::db::queries::sample_meta;
fn test_db() -> Database {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "on").unwrap();
crate::db::schema::create_tables(&conn).unwrap();
Database { conn }
}
fn album_of_three(db: &Database) -> (i64, Vec<i64>) {
let ids: Vec<i64> = ["One", "Two", "Three"]
.iter()
.enumerate()
.map(|(i, title)| {
let mut meta = sample_meta(title, "Boards of Canada", "Geogaddi");
meta.path = Some(format!("/music/geogaddi/{i}.flac"));
meta.track_number = Some(i as i32 + 1);
queries::upsert_track(&db.conn, &meta).unwrap()
})
.collect();
let album_id: i64 = db
.conn
.query_row("SELECT album_id FROM tracks WHERE id = ?1", [ids[0]], |r| {
r.get(0)
})
.unwrap();
db.conn
.execute(
"UPDATE albums SET remote_id = 'al-1' WHERE id = ?1",
[album_id],
)
.unwrap();
(album_id, ids)
}
#[test]
fn whole_album_collapses_to_the_album_link() {
let db = test_db();
let (album_id, ids) = album_of_three(&db);
assert_eq!(
album_remote_id(&db.conn, album_id, ids.len()),
Some("al-1".into())
);
}
#[test]
fn part_of_an_album_does_not() {
let db = test_db();
let (album_id, _) = album_of_three(&db);
assert_eq!(album_remote_id(&db.conn, album_id, 2), None);
}
#[test]
fn a_local_only_album_has_no_link_to_collapse_to() {
let db = test_db();
let (album_id, ids) = album_of_three(&db);
db.conn
.execute(
"UPDATE albums SET remote_id = NULL WHERE id = ?1",
[album_id],
)
.unwrap();
assert_eq!(album_remote_id(&db.conn, album_id, ids.len()), None);
}
}
#[cfg(test)]
mod client_cache_tests {
use super::*;
#[test]
fn one_subsonic_client_is_shared_per_credentials() {
crate::config::isolate_config_for_tests();
let mut cfg = Config::default();
cfg.remote.enabled = true;
cfg.remote.url = "https://shared-client.invalid".into();
cfg.remote.username = "koan".into();
cfg.remote.password = "first".into();
let first = subsonic_client(&cfg).expect("a configured remote yields a client");
let again = subsonic_client(&cfg).expect("a configured remote yields a client");
assert!(
Arc::ptr_eq(&first, &again),
"rebuilding drops the connection pool and re-handshakes TLS per request"
);
cfg.remote.password = "second".into();
let relogged = subsonic_client(&cfg).expect("a configured remote yields a client");
assert!(
!Arc::ptr_eq(&first, &relogged),
"new credentials must not keep serving the client signed with the old ones"
);
}
}
#[cfg(test)]
mod native_share_tests {
use super::*;
use crate::db::queries::{sample_meta, upsert_track};
#[test]
fn a_standalone_server_shares_natively_in_the_order_asked() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "on").unwrap();
crate::db::schema::create_tables(&conn).unwrap();
let db = Database { conn };
let a = upsert_track(&db.conn, &sample_meta("A", "X", "Y")).unwrap();
let b = upsert_track(&db.conn, &sample_meta("B", "X", "Y")).unwrap();
let mut cfg = Config::default();
assert!(matches!(
create_share(&db, &cfg, &ShareTarget::Tracks(vec![a]), None),
Err(ShareError::NoPublicUrl)
));
cfg.sharing.public_url = Some("https://koan.example/".into());
let out = create_share(
&db,
&cfg,
&ShareTarget::Tracks(vec![b, 9999, a]),
Some("mix"),
)
.unwrap();
assert_eq!(out.url, format!("https://koan.example/share/{}", out.id));
assert_eq!((out.shared, out.skipped), (2, 1));
let share = queries::shares::get_share(&db.conn, &out.id)
.unwrap()
.unwrap();
assert_eq!(share.track_ids, [b, a]);
assert!(matches!(
create_share(&db, &cfg, &ShareTarget::Tracks(vec![9999]), None),
Err(ShareError::NothingToShare)
));
}
fn album_track(db: &Database, title: &str, album: &str, n: i32, date: &str) -> i64 {
let mut meta = sample_meta(title, "Rrose", album);
meta.track_number = Some(n);
meta.date = Some(date.into());
upsert_track(&db.conn, &meta).unwrap()
}
#[test]
fn shares_are_slices_fixed_when_made() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.pragma_update(None, "foreign_keys", "on").unwrap();
crate::db::schema::create_tables(&conn).unwrap();
let db = Database { conn };
let later = album_track(&db, "L1", "Later", 1, "2021");
let a1 = album_track(&db, "E1", "Earlier", 1, "2015");
let a2 = album_track(&db, "E2", "Earlier", 2, "2015");
let album_of = |t| {
queries::tracks_by_ids(&db.conn, &[t]).unwrap()[0]
.album_id
.unwrap()
};
let (earlier, later_album) = (album_of(a1), album_of(later));
let artist = queries::tracks_by_ids(&db.conn, &[a1]).unwrap()[0]
.artist_id
.unwrap();
let (slice, ids) = resolve_share(&db.conn, &ShareTarget::Tracks(vec![a2])).unwrap();
assert_eq!(
(slice.kind, slice.subject_id, slice.start_track_id),
(ShareKind::Album, Some(earlier), Some(a2))
);
assert_eq!(ids, [a1, a2]);
let (slice, ids) = resolve_share(
&db.conn,
&ShareTarget::Album {
album_id: later_album,
start_track_id: Some(a1),
},
)
.unwrap();
assert_eq!((slice.kind, slice.start_track_id), (ShareKind::Album, None));
assert_eq!(ids, [later]);
let (slice, ids) = resolve_share(&db.conn, &ShareTarget::Artist(artist)).unwrap();
assert_eq!(
(slice.kind, slice.subject_id),
(ShareKind::Artist, Some(artist))
);
assert_eq!(ids, [a1, a2, later]);
let (slice, ids) = resolve_share(&db.conn, &ShareTarget::Tracks(vec![later, a1])).unwrap();
assert_eq!(slice, Slice::TRACKS);
assert_eq!(ids, [later, a1]);
assert!(matches!(
resolve_share(&db.conn, &ShareTarget::Artist(9999)),
Err(ShareError::NothingToShare)
));
}
}
#[cfg(test)]
mod album_share_id_tests {
use super::album_share_id_for;
#[test]
fn a_koan_album_is_named_as_an_album() {
assert_eq!(album_share_id_for(true, "46215".into()), "al-46215");
assert_eq!(album_share_id_for(true, "al-7".into()), "al-7");
assert_eq!(album_share_id_for(false, "46215".into()), "46215");
assert_eq!(album_share_id_for(false, "3xJ9kQ2pZ".into()), "3xJ9kQ2pZ");
}
}