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

fix: add fallback logic for CDN urls (#1524)

This commit is contained in:
./lemon.sh 2025-08-08 16:32:20 +02:00 committed by GitHub
parent be37402421
commit 3a700f0020
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 81 additions and 25 deletions

View file

@ -78,6 +78,7 @@ impl CdnUrl {
Ok(cdn_url)
}
#[deprecated = "This function only returns the first valid URL. Use try_get_urls instead, which allows for fallback logic."]
pub fn try_get_url(&self) -> Result<&str, Error> {
if self.urls.is_empty() {
return Err(CdnUrlError::Unresolved.into());
@ -95,6 +96,34 @@ impl CdnUrl {
Err(CdnUrlError::Expired.into())
}
}
pub fn try_get_urls(&self) -> Result<Vec<&str>, Error> {
if self.urls.is_empty() {
return Err(CdnUrlError::Unresolved.into());
}
let now = Date::now_utc();
let urls: Vec<&str> = self
.urls
.iter()
.filter_map(|MaybeExpiringUrl(url, expiry)| match *expiry {
Some(expiry) => {
if now < expiry {
Some(url.as_str())
} else {
None
}
}
None => Some(url.as_str()),
})
.collect();
if urls.is_empty() {
Err(CdnUrlError::Expired.into())
} else {
Ok(urls)
}
}
}
impl TryFrom<CdnUrlMessage> for MaybeExpiringUrls {