1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-03 09:49:31 +02:00

Remove unwraps from librespot-audio

This commit is contained in:
Roderick van Domburg 2021-12-18 23:44:13 +01:00
parent d18a0d1803
commit 0d51fd43dc
No known key found for this signature in database
GPG key ID: A9EF5222A26F0451
9 changed files with 301 additions and 165 deletions

View file

@ -350,7 +350,7 @@ impl Cache {
}
}
pub fn file_path(&self, file: FileId) -> Option<PathBuf> {
fn file_path(&self, file: FileId) -> Option<PathBuf> {
self.audio_location.as_ref().map(|location| {
let name = file.to_base16();
let mut path = location.join(&name[0..2]);
@ -377,24 +377,22 @@ impl Cache {
}
}
pub fn save_file<F: Read>(&self, file: FileId, contents: &mut F) {
let path = if let Some(path) = self.file_path(file) {
path
} else {
return;
};
let parent = path.parent().unwrap();
let result = fs::create_dir_all(parent)
.and_then(|_| File::create(&path))
.and_then(|mut file| io::copy(contents, &mut file));
if let Ok(size) = result {
if let Some(limiter) = self.size_limiter.as_deref() {
limiter.add(&path, size);
limiter.prune();
pub fn save_file<F: Read>(&self, file: FileId, contents: &mut F) -> bool {
if let Some(path) = self.file_path(file) {
if let Some(parent) = path.parent() {
if let Ok(size) = fs::create_dir_all(parent)
.and_then(|_| File::create(&path))
.and_then(|mut file| io::copy(contents, &mut file))
{
if let Some(limiter) = self.size_limiter.as_deref() {
limiter.add(&path, size);
limiter.prune();
}
return true;
}
}
}
false
}
pub fn remove_file(&self, file: FileId) -> Result<(), RemoveFileError> {

View file

@ -80,7 +80,7 @@ impl CdnUrl {
return Err(CdnUrlError::Empty);
}
// remove expired URLs until the first one is current, or none are left
// prune expired URLs until the first one is current, or none are left
let now = Local::now();
while !self.urls.is_empty() {
let maybe_expiring = self.urls[0].1;

View file

@ -14,7 +14,9 @@ use url::Url;
use std::env::consts::OS;
use crate::version::{SPOTIFY_MOBILE_VERSION, SPOTIFY_VERSION, VERSION_STRING};
use crate::version::{
FALLBACK_USER_AGENT, SPOTIFY_MOBILE_VERSION, SPOTIFY_VERSION, VERSION_STRING,
};
pub struct HttpClient {
user_agent: HeaderValue,
@ -64,8 +66,8 @@ impl HttpClient {
let user_agent = HeaderValue::from_str(user_agent_str).unwrap_or_else(|err| {
error!("Invalid user agent <{}>: {}", user_agent_str, err);
error!("Parts of the API will probably not work. Please report this as a bug.");
HeaderValue::from_static("")
error!("Please report this as a bug.");
HeaderValue::from_static(FALLBACK_USER_AGENT)
});
// configuring TLS is expensive and should be done once per process

View file

@ -21,3 +21,6 @@ pub const SPOTIFY_VERSION: u64 = 117300517;
/// The protocol version of the Spotify mobile app.
pub const SPOTIFY_MOBILE_VERSION: &str = "8.6.84";
/// The user agent to fall back to, if one could not be determined dynamically.
pub const FALLBACK_USER_AGENT: &str = "Spotify/117300517 Linux/0 (librespot)";