mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Add the option to specify the system cache for credentials and volume files and adapt Cache to use two cache directories instead of one
This commit is contained in:
parent
e3abb87da1
commit
8fc9ebfa8c
2 changed files with 23 additions and 12 deletions
|
@ -11,7 +11,8 @@ use crate::volume::Volume;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
root: PathBuf,
|
audio_root: PathBuf,
|
||||||
|
system_root: PathBuf,
|
||||||
use_audio_cache: bool,
|
use_audio_cache: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,12 +27,14 @@ fn mkdir_existing(path: &Path) -> io::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cache {
|
impl Cache {
|
||||||
pub fn new(location: PathBuf, use_audio_cache: bool) -> Cache {
|
pub fn new(audio_location: PathBuf, system_location: PathBuf, use_audio_cache: bool) -> Cache {
|
||||||
mkdir_existing(&location).unwrap();
|
mkdir_existing(&audio_location).unwrap();
|
||||||
mkdir_existing(&location.join("files")).unwrap();
|
mkdir_existing(&audio_location.join("files")).unwrap();
|
||||||
|
mkdir_existing(&system_location).unwrap();
|
||||||
|
|
||||||
Cache {
|
Cache {
|
||||||
root: location,
|
audio_root: audio_location,
|
||||||
|
system_root: system_location,
|
||||||
use_audio_cache: use_audio_cache,
|
use_audio_cache: use_audio_cache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +42,7 @@ impl Cache {
|
||||||
|
|
||||||
impl Cache {
|
impl Cache {
|
||||||
fn credentials_path(&self) -> PathBuf {
|
fn credentials_path(&self) -> PathBuf {
|
||||||
self.root.join("credentials.json")
|
self.system_root.join("credentials.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn credentials(&self) -> Option<Credentials> {
|
pub fn credentials(&self) -> Option<Credentials> {
|
||||||
|
@ -53,10 +56,10 @@ impl Cache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache volume to root/volume
|
// cache volume to system_root/volume
|
||||||
impl Cache {
|
impl Cache {
|
||||||
fn volume_path(&self) -> PathBuf {
|
fn volume_path(&self) -> PathBuf {
|
||||||
self.root.join("volume")
|
self.system_root.join("volume")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn volume(&self) -> Option<u16> {
|
pub fn volume(&self) -> Option<u16> {
|
||||||
|
@ -73,7 +76,7 @@ impl Cache {
|
||||||
impl Cache {
|
impl Cache {
|
||||||
fn file_path(&self, file: FileId) -> PathBuf {
|
fn file_path(&self, file: FileId) -> PathBuf {
|
||||||
let name = file.to_base16();
|
let name = file.to_base16();
|
||||||
self.root.join("files").join(&name[0..2]).join(&name[2..])
|
self.audio_root.join("files").join(&name[0..2]).join(&name[2..])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file(&self, file: FileId) -> Option<File> {
|
pub fn file(&self, file: FileId) -> Option<File> {
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -97,6 +97,11 @@ fn setup(args: &[String]) -> Setup {
|
||||||
"cache",
|
"cache",
|
||||||
"Path to a directory where files will be cached.",
|
"Path to a directory where files will be cached.",
|
||||||
"CACHE",
|
"CACHE",
|
||||||
|
).optopt(
|
||||||
|
"",
|
||||||
|
"system-cache",
|
||||||
|
"Path to a directory where system files (credentials, volume) will be cached. Can be different from cache option value",
|
||||||
|
"SYTEMCACHE",
|
||||||
).optflag("", "disable-audio-cache", "Disable caching of the audio data.")
|
).optflag("", "disable-audio-cache", "Disable caching of the audio data.")
|
||||||
.reqopt("n", "name", "Device name", "NAME")
|
.reqopt("n", "name", "Device name", "NAME")
|
||||||
.optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
|
.optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
|
||||||
|
@ -244,9 +249,12 @@ fn setup(args: &[String]) -> Setup {
|
||||||
|
|
||||||
let use_audio_cache = !matches.opt_present("disable-audio-cache");
|
let use_audio_cache = !matches.opt_present("disable-audio-cache");
|
||||||
|
|
||||||
let cache = matches
|
let cache_directory = matches.opt_str("c").unwrap_or(String::from(""))
|
||||||
.opt_str("c")
|
let system_cache_directory = matches.opt_str("system-cache").unwrap_or(String::from(cache_directory))
|
||||||
.map(|cache_location| Cache::new(PathBuf::from(cache_location), use_audio_cache));
|
let cache = Cache::new(
|
||||||
|
PathBuf::from(cache_directory),
|
||||||
|
PathBuf::from(system_cache_directory),
|
||||||
|
use_audio_cache));
|
||||||
|
|
||||||
let initial_volume = matches
|
let initial_volume = matches
|
||||||
.opt_str("initial-volume")
|
.opt_str("initial-volume")
|
||||||
|
|
Loading…
Reference in a new issue