Add disable credential cache flag

As mentioned in https://github.com/librespot-org/librespot/discussions/870, this allows someone who would otherwise like to take advantage of audio file and volume caching to disable credential caching.
This commit is contained in:
JasonLG1979 2021-10-26 22:06:52 -05:00
parent b4784ebf9c
commit 52bd212e43
2 changed files with 32 additions and 14 deletions

View file

@ -238,29 +238,38 @@ pub struct RemoveFileError(());
impl Cache { impl Cache {
pub fn new<P: AsRef<Path>>( pub fn new<P: AsRef<Path>>(
system_location: Option<P>, credentials: Option<P>,
audio_location: Option<P>, volume: Option<P>,
audio: Option<P>,
size_limit: Option<u64>, size_limit: Option<u64>,
) -> io::Result<Self> { ) -> io::Result<Self> {
if let Some(location) = &system_location { let mut size_limiter = None;
if let Some(location) = &credentials {
fs::create_dir_all(location)?; fs::create_dir_all(location)?;
} }
let mut size_limiter = None; let credentials_location = credentials
.as_ref()
.map(|p| p.as_ref().join("credentials.json"));
if let Some(location) = &audio_location { if let Some(location) = &volume {
fs::create_dir_all(location)?; fs::create_dir_all(location)?;
}
let volume_location = volume.as_ref().map(|p| p.as_ref().join("volume"));
if let Some(location) = &audio {
fs::create_dir_all(location)?;
if let Some(limit) = size_limit { if let Some(limit) = size_limit {
let limiter = FsSizeLimiter::new(location.as_ref(), limit); let limiter = FsSizeLimiter::new(location.as_ref(), limit);
size_limiter = Some(Arc::new(limiter)); size_limiter = Some(Arc::new(limiter));
} }
} }
let audio_location = audio_location.map(|p| p.as_ref().to_owned()); let audio_location = audio.map(|p| p.as_ref().to_owned());
let volume_location = system_location.as_ref().map(|p| p.as_ref().join("volume"));
let credentials_location = system_location
.as_ref()
.map(|p| p.as_ref().join("credentials.json"));
let cache = Cache { let cache = Cache {
credentials_location, credentials_location,

View file

@ -203,6 +203,7 @@ fn get_setup(args: &[String]) -> Setup {
const DEVICE: &str = "device"; const DEVICE: &str = "device";
const DEVICE_TYPE: &str = "device-type"; const DEVICE_TYPE: &str = "device-type";
const DISABLE_AUDIO_CACHE: &str = "disable-audio-cache"; const DISABLE_AUDIO_CACHE: &str = "disable-audio-cache";
const DISABLE_CREDENTIAL_CACHE: &str = "disable-credential-cache";
const DISABLE_DISCOVERY: &str = "disable-discovery"; const DISABLE_DISCOVERY: &str = "disable-discovery";
const DISABLE_GAPLESS: &str = "disable-gapless"; const DISABLE_GAPLESS: &str = "disable-gapless";
const DITHER: &str = "dither"; const DITHER: &str = "dither";
@ -256,6 +257,7 @@ fn get_setup(args: &[String]) -> Setup {
"Limits the size of the cache for audio files.", "Limits the size of the cache for audio files.",
"SIZE" "SIZE"
).optflag("", DISABLE_AUDIO_CACHE, "Disable caching of the audio data.") ).optflag("", DISABLE_AUDIO_CACHE, "Disable caching of the audio data.")
.optflag("", DISABLE_CREDENTIAL_CACHE, "Disable caching of credentials.")
.optopt("n", NAME, "Device name.", "NAME") .optopt("n", NAME, "Device name.", "NAME")
.optopt("", DEVICE_TYPE, "Displayed device type. Defaults to 'Speaker'.", "TYPE") .optopt("", DEVICE_TYPE, "Displayed device type. Defaults to 'Speaker'.", "TYPE")
.optopt( .optopt(
@ -560,10 +562,11 @@ fn get_setup(args: &[String]) -> Setup {
let cache = { let cache = {
let audio_dir; let audio_dir;
let system_dir; let cred_dir;
let volume_dir;
if matches.opt_present(DISABLE_AUDIO_CACHE) { if matches.opt_present(DISABLE_AUDIO_CACHE) {
audio_dir = None; audio_dir = None;
system_dir = matches volume_dir = matches
.opt_str(SYSTEM_CACHE) .opt_str(SYSTEM_CACHE)
.or_else(|| matches.opt_str(CACHE)) .or_else(|| matches.opt_str(CACHE))
.map(|p| p.into()); .map(|p| p.into());
@ -572,12 +575,18 @@ fn get_setup(args: &[String]) -> Setup {
audio_dir = cache_dir audio_dir = cache_dir
.as_ref() .as_ref()
.map(|p| AsRef::<Path>::as_ref(p).join("files")); .map(|p| AsRef::<Path>::as_ref(p).join("files"));
system_dir = matches volume_dir = matches
.opt_str(SYSTEM_CACHE) .opt_str(SYSTEM_CACHE)
.or(cache_dir) .or(cache_dir)
.map(|p| p.into()); .map(|p| p.into());
} }
if matches.opt_present(DISABLE_CREDENTIAL_CACHE) {
cred_dir = None;
} else {
cred_dir = volume_dir.clone();
}
let limit = if audio_dir.is_some() { let limit = if audio_dir.is_some() {
matches matches
.opt_str(CACHE_SIZE_LIMIT) .opt_str(CACHE_SIZE_LIMIT)
@ -593,7 +602,7 @@ fn get_setup(args: &[String]) -> Setup {
None None
}; };
match Cache::new(system_dir, audio_dir, limit) { match Cache::new(cred_dir, volume_dir, audio_dir, limit) {
Ok(cache) => Some(cache), Ok(cache) => Some(cache),
Err(e) => { Err(e) => {
warn!("Cannot create cache: {}", e); warn!("Cannot create cache: {}", e);