Skip to content

Commit 714cfe5

Browse files
committed
Write cached covers to ~/.cache/songrec instead of /tmp/ (issue #214)
1 parent e1a29a7 commit 714cfe5

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/gui/main_window.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::plugins::ksni::SystrayInterface;
2626
use crate::plugins::mpris_player::{get_player, update_song};
2727
use crate::utils::csv_song_history::SongHistoryRecord;
2828
use crate::utils::filesystem_operations::{
29-
obtain_favorites_csv_path, obtain_recognition_history_csv_path,
29+
clear_cache, obtain_favorites_csv_path, obtain_recognition_history_csv_path,
3030
};
3131

3232
use crate::gui::preferences::{Preferences, PreferencesInterface};
@@ -281,6 +281,7 @@ impl App {
281281
set_recording: bool,
282282
enable_mpris_cli: bool,
283283
) {
284+
clear_cache();
284285
self.setup_intercom(application, set_recording, enable_mpris_cli);
285286
self.setup_actions(application, enable_mpris_cli);
286287
#[cfg(target_os = "linux")]

src/plugins/mpris_player.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use log::debug;
12
use std::fs;
23
use std::panic;
34
use std::sync::Arc;
@@ -9,6 +10,7 @@ use std::sync::Arc;
910
use mpris_player::{Metadata, MprisPlayer, PlaybackStatus};
1011

1112
use crate::core::thread_messages::SongRecognizedMessage;
13+
use crate::utils::filesystem_operations::obtain_cache_directory;
1214
use std::os::unix::fs::MetadataExt;
1315
use std::time::SystemTime;
1416

@@ -87,7 +89,8 @@ pub fn update_song(
8789
let process_uid = std::fs::metadata("/proc/self")
8890
.map(|m| m.uid())
8991
.unwrap_or(0);
90-
let mut tmp = std::env::temp_dir();
92+
// TODO use cache dir
93+
let mut tmp = obtain_cache_directory().unwrap();
9194
let timestamp = SystemTime::now()
9295
.duration_since(SystemTime::UNIX_EPOCH)
9396
.unwrap()
@@ -96,6 +99,7 @@ pub fn update_song(
9699
"songrec_cover_{}_{}.{}",
97100
process_uid, timestamp, mime_ext
98101
));
102+
debug!("Writing cover file to {:?}", tmp);
99103
if fs::write(&tmp, buf).is_ok() {
100104
// Use file:// URL for better compatibility with MPRIS clients
101105
metadata.art_url = Some(format!("file://{}", tmp.display()));

src/utils/filesystem_operations.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ fn obtain_preferences_directory(project_directory: ProjectDirs) -> Result<PathBu
6060
Ok(preferences_dir.to_path_buf())
6161
}
6262

63+
pub fn obtain_cache_directory() -> Result<PathBuf, Box<dyn Error>> {
64+
let project_dir =
65+
ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION).ok_or("No valid path")?;
66+
let cache_path = project_dir.cache_dir();
67+
if !cache_path.exists() {
68+
create_dir_all(cache_path)?;
69+
}
70+
Ok(cache_path.to_path_buf())
71+
}
72+
73+
pub fn clear_cache() {
74+
if let Ok(contents) = std::fs::read_dir(obtain_cache_directory().unwrap()) {
75+
for entry in contents {
76+
if let Ok(entry) = entry {
77+
std::fs::remove_file(entry.path()).ok();
78+
}
79+
}
80+
}
81+
}
82+
6383
//Backwards compatibility
6484
fn get_old_data_dir_path() -> Result<PathBuf, Box<dyn Error>> {
6585
let app_info = AppInfo {

0 commit comments

Comments
 (0)