fix offline cache_dir not updating to user-scoped path
OfflineCacheState.cache_dir was set during new_empty() to the flat path (~/.cache/qbz/audio/) and never updated when init_at() opened the DB at the user-scoped path. This caused track files and artwork to be read/written at the wrong location. Wrapped cache_dir in Arc<RwLock<>> and update it during init_at().
This commit is contained in:
1 parent
9503ad9d49
commit
6ef50f7dc2
2 files changed
+24
-16
No files matched your search
@@ -364,7 +364,7 @@ pub async fn remove_cached_track(
|
|||||||
|
|
||||||
// Clean up empty album folder (and artist folder if also empty)
|
// Clean up empty album folder (and artist folder if also empty)
|
||||||
if let Some(album_dir) = path.parent() {
|
if let Some(album_dir) = path.parent() {
|
||||||
cleanup_empty_folder(album_dir, &cache_state.cache_dir);
|
cleanup_empty_folder(album_dir, &cache_state.cache_dir.read().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -444,7 +444,8 @@ pub async fn purge_all_cached_files(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Also clear the tracks directory (legacy unorganized files)
|
// Also clear the tracks directory (legacy unorganized files)
|
||||||
let tracks_dir = cache_state.cache_dir.join("tracks");
|
let cache_dir = cache_state.cache_dir.read().unwrap().clone();
|
||||||
|
let tracks_dir = cache_dir.join("tracks");
|
||||||
if tracks_dir.exists() {
|
if tracks_dir.exists() {
|
||||||
if let Ok(entries) = std::fs::read_dir(&tracks_dir) {
|
if let Ok(entries) = std::fs::read_dir(&tracks_dir) {
|
||||||
for entry in entries.flatten() {
|
for entry in entries.flatten() {
|
||||||
@@ -455,7 +456,7 @@ pub async fn purge_all_cached_files(
|
|||||||
|
|
||||||
// Clear organized artist/album folders
|
// Clear organized artist/album folders
|
||||||
// Look for any subdirectories in cache_dir that are not "tracks" or system files
|
// Look for any subdirectories in cache_dir that are not "tracks" or system files
|
||||||
if let Ok(entries) = std::fs::read_dir(&cache_state.cache_dir) {
|
if let Ok(entries) = std::fs::read_dir(&cache_dir) {
|
||||||
for entry in entries.flatten() {
|
for entry in entries.flatten() {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
@@ -512,7 +513,7 @@ pub async fn open_offline_cache_folder(
|
|||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
log::info!("Command: open_offline_cache_folder");
|
log::info!("Command: open_offline_cache_folder");
|
||||||
|
|
||||||
let path = cache_state.cache_dir.clone();
|
let path = cache_state.cache_dir.read().unwrap().clone();
|
||||||
|
|
||||||
// Ensure directory exists
|
// Ensure directory exists
|
||||||
std::fs::create_dir_all(&path)
|
std::fs::create_dir_all(&path)
|
||||||
@@ -662,7 +663,7 @@ async fn evict_if_needed(
|
|||||||
|
|
||||||
// Clean up empty album folder (and artist folder if also empty)
|
// Clean up empty album folder (and artist folder if also empty)
|
||||||
if let Some(album_dir) = path.parent() {
|
if let Some(album_dir) = path.parent() {
|
||||||
cleanup_empty_folder(album_dir, &cache_state.cache_dir);
|
cleanup_empty_folder(album_dir, &cache_state.cache_dir.read().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -678,7 +679,7 @@ pub async fn check_offline_root_mounted(
|
|||||||
) -> Result<bool, String> {
|
) -> Result<bool, String> {
|
||||||
log::info!("Command: check_offline_root_mounted");
|
log::info!("Command: check_offline_root_mounted");
|
||||||
|
|
||||||
let root_path = cache_state.cache_dir.to_string_lossy().to_string();
|
let root_path = cache_state.cache_dir.read().unwrap().to_string_lossy().to_string();
|
||||||
super::path_validator::is_offline_root_available(&root_path)
|
super::path_validator::is_offline_root_available(&root_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -695,7 +696,7 @@ pub async fn move_offline_cache_to_path(
|
|||||||
) -> Result<super::path_validator::MoveReport, String> {
|
) -> Result<super::path_validator::MoveReport, String> {
|
||||||
log::info!("Command: move_offline_cache_to_path: {}", new_path);
|
log::info!("Command: move_offline_cache_to_path: {}", new_path);
|
||||||
|
|
||||||
let old_path = cache_state.cache_dir.to_string_lossy().to_string();
|
let old_path = cache_state.cache_dir.read().unwrap().to_string_lossy().to_string();
|
||||||
super::path_validator::move_cached_files_to_new_path(&old_path, &new_path)
|
super::path_validator::move_cached_files_to_new_path(&old_path, &new_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -706,7 +707,7 @@ pub async fn detect_legacy_cached_files(
|
|||||||
) -> Result<super::MigrationStatus, String> {
|
) -> Result<super::MigrationStatus, String> {
|
||||||
log::info!("Command: detect_legacy_cached_files");
|
log::info!("Command: detect_legacy_cached_files");
|
||||||
|
|
||||||
let tracks_dir = cache_state.cache_dir.join("tracks");
|
let tracks_dir = cache_state.cache_dir.read().unwrap().join("tracks");
|
||||||
|
|
||||||
match super::detect_legacy_cached_files(&tracks_dir) {
|
match super::detect_legacy_cached_files(&tracks_dir) {
|
||||||
Ok(track_ids) => {
|
Ok(track_ids) => {
|
||||||
@@ -730,7 +731,7 @@ pub async fn start_legacy_migration(
|
|||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
log::info!("Command: start_legacy_migration");
|
log::info!("Command: start_legacy_migration");
|
||||||
|
|
||||||
let tracks_dir = cache_state.cache_dir.join("tracks");
|
let tracks_dir = cache_state.cache_dir.read().unwrap().join("tracks");
|
||||||
let track_ids = super::detect_legacy_cached_files(&tracks_dir)?;
|
let track_ids = super::detect_legacy_cached_files(&tracks_dir)?;
|
||||||
|
|
||||||
if track_ids.is_empty() {
|
if track_ids.is_empty() {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ pub mod metadata;
|
|||||||
pub mod migration;
|
pub mod migration;
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, RwLock};
|
||||||
use tokio::sync::{Mutex, Semaphore};
|
use tokio::sync::{Mutex, Semaphore};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ pub struct TrackCacheInfo {
|
|||||||
pub struct OfflineCacheState {
|
pub struct OfflineCacheState {
|
||||||
pub db: Arc<Mutex<Option<OfflineCacheDb>>>,
|
pub db: Arc<Mutex<Option<OfflineCacheDb>>>,
|
||||||
pub fetcher: Arc<StreamFetcher>,
|
pub fetcher: Arc<StreamFetcher>,
|
||||||
pub cache_dir: PathBuf,
|
pub cache_dir: Arc<RwLock<PathBuf>>,
|
||||||
/// Cache limit in bytes (None = unlimited)
|
/// Cache limit in bytes (None = unlimited)
|
||||||
pub limit_bytes: Arc<Mutex<Option<u64>>>,
|
pub limit_bytes: Arc<Mutex<Option<u64>>>,
|
||||||
pub cache_semaphore: Arc<Semaphore>,
|
pub cache_semaphore: Arc<Semaphore>,
|
||||||
@@ -162,7 +162,7 @@ impl OfflineCacheState {
|
|||||||
let state = Self {
|
let state = Self {
|
||||||
db: Arc::new(Mutex::new(Some(db))),
|
db: Arc::new(Mutex::new(Some(db))),
|
||||||
fetcher: Arc::new(StreamFetcher::new()),
|
fetcher: Arc::new(StreamFetcher::new()),
|
||||||
cache_dir: cache_dir.clone(),
|
cache_dir: Arc::new(RwLock::new(cache_dir.clone())),
|
||||||
limit_bytes: Arc::new(Mutex::new(default_limit)),
|
limit_bytes: Arc::new(Mutex::new(default_limit)),
|
||||||
cache_semaphore: Arc::new(Semaphore::new(3)),
|
cache_semaphore: Arc::new(Semaphore::new(3)),
|
||||||
};
|
};
|
||||||
@@ -180,7 +180,7 @@ impl OfflineCacheState {
|
|||||||
Self {
|
Self {
|
||||||
db: Arc::new(Mutex::new(None)),
|
db: Arc::new(Mutex::new(None)),
|
||||||
fetcher: Arc::new(StreamFetcher::new()),
|
fetcher: Arc::new(StreamFetcher::new()),
|
||||||
cache_dir,
|
cache_dir: Arc::new(RwLock::new(cache_dir)),
|
||||||
limit_bytes: Arc::new(Mutex::new(Some(2 * 1024 * 1024 * 1024u64))),
|
limit_bytes: Arc::new(Mutex::new(Some(2 * 1024 * 1024 * 1024u64))),
|
||||||
cache_semaphore: Arc::new(Semaphore::new(3)),
|
cache_semaphore: Arc::new(Semaphore::new(3)),
|
||||||
}
|
}
|
||||||
@@ -198,6 +198,10 @@ impl OfflineCacheState {
|
|||||||
let new_db = OfflineCacheDb::new(&db_path)?;
|
let new_db = OfflineCacheDb::new(&db_path)?;
|
||||||
let mut guard = self.db.lock().await;
|
let mut guard = self.db.lock().await;
|
||||||
*guard = Some(new_db);
|
*guard = Some(new_db);
|
||||||
|
// Update cache_dir to user-scoped path
|
||||||
|
if let Ok(mut dir_guard) = self.cache_dir.write() {
|
||||||
|
*dir_guard = cache_dir.clone();
|
||||||
|
}
|
||||||
log::info!("Offline cache initialized at: {:?}", cache_dir);
|
log::info!("Offline cache initialized at: {:?}", cache_dir);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -209,16 +213,19 @@ impl OfflineCacheState {
|
|||||||
|
|
||||||
/// Get the path for a track's audio file
|
/// Get the path for a track's audio file
|
||||||
pub fn track_file_path(&self, track_id: u64, format: &str) -> PathBuf {
|
pub fn track_file_path(&self, track_id: u64, format: &str) -> PathBuf {
|
||||||
self.cache_dir.join("tracks").join(format!("{}.{}", track_id, format))
|
let dir = self.cache_dir.read().unwrap();
|
||||||
|
dir.join("tracks").join(format!("{}.{}", track_id, format))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the path for an album's artwork
|
/// Get the path for an album's artwork
|
||||||
pub fn artwork_path(&self, album_id: &str) -> PathBuf {
|
pub fn artwork_path(&self, album_id: &str) -> PathBuf {
|
||||||
self.cache_dir.join("artwork").join(format!("{}.jpg", album_id))
|
let dir = self.cache_dir.read().unwrap();
|
||||||
|
dir.join("artwork").join(format!("{}.jpg", album_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the cache directory path
|
/// Get the cache directory path
|
||||||
pub fn get_cache_path(&self) -> String {
|
pub fn get_cache_path(&self) -> String {
|
||||||
self.cache_dir.to_string_lossy().to_string()
|
let dir = self.cache_dir.read().unwrap();
|
||||||
|
dir.to_string_lossy().to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user