mirror of
https://github.com/librespot-org/librespot.git
synced 2024-11-08 16:45:43 +00:00
Merge pull request #506 from LaurentLouf/feature-split_cache_folders
Feature split cache folders, resolves #505
This commit is contained in:
commit
414383db18
2 changed files with 32 additions and 12 deletions
|
@ -11,7 +11,8 @@ use crate::volume::Volume;
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct Cache {
|
||||
root: PathBuf,
|
||||
audio_root: PathBuf,
|
||||
system_root: PathBuf,
|
||||
use_audio_cache: bool,
|
||||
}
|
||||
|
||||
|
@ -26,12 +27,16 @@ fn mkdir_existing(path: &Path) -> io::Result<()> {
|
|||
}
|
||||
|
||||
impl Cache {
|
||||
pub fn new(location: PathBuf, use_audio_cache: bool) -> Cache {
|
||||
mkdir_existing(&location).unwrap();
|
||||
mkdir_existing(&location.join("files")).unwrap();
|
||||
pub fn new(audio_location: PathBuf, system_location: PathBuf, use_audio_cache: bool) -> Cache {
|
||||
if use_audio_cache == true {
|
||||
mkdir_existing(&audio_location).unwrap();
|
||||
mkdir_existing(&audio_location.join("files")).unwrap();
|
||||
}
|
||||
mkdir_existing(&system_location).unwrap();
|
||||
|
||||
Cache {
|
||||
root: location,
|
||||
audio_root: audio_location,
|
||||
system_root: system_location,
|
||||
use_audio_cache: use_audio_cache,
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +44,7 @@ impl Cache {
|
|||
|
||||
impl Cache {
|
||||
fn credentials_path(&self) -> PathBuf {
|
||||
self.root.join("credentials.json")
|
||||
self.system_root.join("credentials.json")
|
||||
}
|
||||
|
||||
pub fn credentials(&self) -> Option<Credentials> {
|
||||
|
@ -53,10 +58,10 @@ impl Cache {
|
|||
}
|
||||
}
|
||||
|
||||
// cache volume to root/volume
|
||||
// cache volume to system_root/volume
|
||||
impl Cache {
|
||||
fn volume_path(&self) -> PathBuf {
|
||||
self.root.join("volume")
|
||||
self.system_root.join("volume")
|
||||
}
|
||||
|
||||
pub fn volume(&self) -> Option<u16> {
|
||||
|
@ -73,7 +78,10 @@ impl Cache {
|
|||
impl Cache {
|
||||
fn file_path(&self, file: FileId) -> PathBuf {
|
||||
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> {
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -97,6 +97,11 @@ fn setup(args: &[String]) -> Setup {
|
|||
"cache",
|
||||
"Path to a directory where files will be cached.",
|
||||
"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.")
|
||||
.reqopt("n", "name", "Device name", "NAME")
|
||||
.optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
|
||||
|
@ -251,9 +256,16 @@ fn setup(args: &[String]) -> Setup {
|
|||
|
||||
let use_audio_cache = !matches.opt_present("disable-audio-cache");
|
||||
|
||||
let cache = matches
|
||||
.opt_str("c")
|
||||
.map(|cache_location| Cache::new(PathBuf::from(cache_location), use_audio_cache));
|
||||
let cache_directory = matches.opt_str("c").unwrap_or(String::from(""));
|
||||
let system_cache_directory = matches
|
||||
.opt_str("system-cache")
|
||||
.unwrap_or(String::from(cache_directory.clone()));
|
||||
|
||||
let cache = Some(Cache::new(
|
||||
PathBuf::from(cache_directory),
|
||||
PathBuf::from(system_cache_directory),
|
||||
use_audio_cache,
|
||||
));
|
||||
|
||||
let initial_volume = matches
|
||||
.opt_str("initial-volume")
|
||||
|
|
Loading…
Reference in a new issue