mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Handle corrupt cache files (#591)
This commit is contained in:
parent
4990351dba
commit
3446864838
3 changed files with 39 additions and 1 deletions
|
@ -459,6 +459,10 @@ impl AudioFile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_cached(&self) -> bool {
|
||||||
|
matches!(self, AudioFile::Cached { .. })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_range(session: &Session, file: FileId, offset: usize, length: usize) -> Channel {
|
fn request_range(session: &Session, file: FileId, offset: usize, length: usize) -> Channel {
|
||||||
|
|
|
@ -162,4 +162,17 @@ impl Cache {
|
||||||
warn!("Cannot save file to cache: {}", e)
|
warn!("Cannot save file to cache: {}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_file(&self, file: FileId) -> bool {
|
||||||
|
if let Some(path) = self.file_path(file) {
|
||||||
|
if let Err(err) = fs::remove_file(path) {
|
||||||
|
warn!("Unable to remove file from cache: {}", err);
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -682,6 +682,7 @@ impl PlayerTrackLoader {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let is_cached = encrypted_file.is_cached();
|
||||||
|
|
||||||
let mut stream_loader_controller = encrypted_file.get_stream_loader_controller();
|
let mut stream_loader_controller = encrypted_file.get_stream_loader_controller();
|
||||||
|
|
||||||
|
@ -715,7 +716,27 @@ impl PlayerTrackLoader {
|
||||||
|
|
||||||
let audio_file = Subfile::new(decrypted_file, 0xa7);
|
let audio_file = Subfile::new(decrypted_file, 0xa7);
|
||||||
|
|
||||||
let mut decoder = VorbisDecoder::new(audio_file).unwrap();
|
let mut decoder = match VorbisDecoder::new(audio_file) {
|
||||||
|
Ok(decoder) => decoder,
|
||||||
|
Err(e) if is_cached => {
|
||||||
|
warn!(
|
||||||
|
"Unable to read cached audio file: {}. Trying to download it.",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
|
||||||
|
// unwrap safety: The file is cached, so session must have a cache
|
||||||
|
if !self.session.cache().unwrap().remove_file(file_id) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Just try it again
|
||||||
|
return self.load_track(spotify_id, position_ms);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Unable to read audio file: {}", e);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if position_ms != 0 {
|
if position_ms != 0 {
|
||||||
match decoder.seek(position_ms as i64) {
|
match decoder.seek(position_ms as i64) {
|
||||||
|
|
Loading…
Reference in a new issue