mirror of
https://github.com/librespot-org/librespot.git
synced 2025-01-27 17:44:04 +00:00
d40c0f50db
* create Volume struct for use with Cache * add "volume" file to Cache * load cached volume on start, intial overrides cached overrides default * amend volume_to_mixer function to cache the volume on every change * pass cache to Spirc and SpircTask so volume_to_mixer has access * rustfmt changes * revert volume_to_mixer function and Spirc/SpircTask cache variable * Volume implements Copy, pass by value instead of reference * clamp volume to 100 if cached value exceeds limit * convert Volume to u16 internally, use float and round to convert hex->dec * convert initial_volume and ConnectConfig.volume to u16 as well * add cache_volume function to SpircTask * remove conversion to/from percentage on cached volume * consolidate device.set_volume, mixer.set_volume, and caching * streamline intial volume logic
93 lines
2.1 KiB
Rust
93 lines
2.1 KiB
Rust
use std::fs;
|
|
use std::fs::File;
|
|
use std::io;
|
|
use std::io::Read;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
|
|
use authentication::Credentials;
|
|
use spotify_id::FileId;
|
|
use volume::Volume;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Cache {
|
|
root: PathBuf,
|
|
use_audio_cache: bool,
|
|
}
|
|
|
|
fn mkdir_existing(path: &Path) -> io::Result<()> {
|
|
fs::create_dir(path).or_else(|err| {
|
|
if err.kind() == io::ErrorKind::AlreadyExists {
|
|
Ok(())
|
|
} else {
|
|
Err(err)
|
|
}
|
|
})
|
|
}
|
|
|
|
impl Cache {
|
|
pub fn new(location: PathBuf, use_audio_cache: bool) -> Cache {
|
|
mkdir_existing(&location).unwrap();
|
|
mkdir_existing(&location.join("files")).unwrap();
|
|
|
|
Cache {
|
|
root: location,
|
|
use_audio_cache: use_audio_cache,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Cache {
|
|
fn credentials_path(&self) -> PathBuf {
|
|
self.root.join("credentials.json")
|
|
}
|
|
|
|
pub fn credentials(&self) -> Option<Credentials> {
|
|
let path = self.credentials_path();
|
|
Credentials::from_file(path)
|
|
}
|
|
|
|
pub fn save_credentials(&self, cred: &Credentials) {
|
|
let path = self.credentials_path();
|
|
cred.save_to_file(&path);
|
|
}
|
|
}
|
|
|
|
// cache volume to root/volume
|
|
impl Cache {
|
|
fn volume_path(&self) -> PathBuf {
|
|
self.root.join("volume")
|
|
}
|
|
|
|
pub fn volume(&self) -> Option<u16> {
|
|
let path = self.volume_path();
|
|
Volume::from_file(path)
|
|
}
|
|
|
|
pub fn save_volume(&self, volume: Volume) {
|
|
let path = self.volume_path();
|
|
volume.save_to_file(&path);
|
|
}
|
|
}
|
|
|
|
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..])
|
|
}
|
|
|
|
pub fn file(&self, file: FileId) -> Option<File> {
|
|
File::open(self.file_path(file)).ok()
|
|
}
|
|
|
|
pub fn save_file(&self, file: FileId, contents: &mut Read) {
|
|
if self.use_audio_cache {
|
|
let path = self.file_path(file);
|
|
|
|
mkdir_existing(path.parent().unwrap()).unwrap();
|
|
|
|
let mut cache_file = File::create(path).unwrap();
|
|
::std::io::copy(contents, &mut cache_file).unwrap();
|
|
}
|
|
}
|
|
}
|