Handle corrupt cache files (#591)

This commit is contained in:
johannesd3 2021-02-02 02:18:58 +01:00
parent 4990351dba
commit 3446864838
3 changed files with 39 additions and 1 deletions

View file

@ -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 {

View file

@ -162,4 +162,17 @@ impl Cache {
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
}
}
}

View file

@ -682,6 +682,7 @@ impl PlayerTrackLoader {
return None;
}
};
let is_cached = encrypted_file.is_cached();
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 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 {
match decoder.seek(position_ms as i64) {