check if path exists (without extension)

This commit is contained in:
Daniel 2023-02-01 20:34:23 +01:00
parent 961d975540
commit 1f333d6475
2 changed files with 23 additions and 18 deletions

View file

@ -22,16 +22,13 @@ impl FileSink {
} }
impl Open for FileSink { impl Open for FileSink {
// use the unwrap_or_else method instead of the if let statement to handle the absence of a path value
fn open(path: Option<String>, _audio_format: AudioFormat) -> Self { fn open(path: Option<String>, _audio_format: AudioFormat) -> Self {
if let Some(path) = path { let file = path.unwrap_or_else(|| panic!());
let file = path; FileSink {
FileSink { sink: file,
sink: file, content: Vec::new(),
content: Vec::new(), metadata: None
metadata: None
}
} else {
panic!();
} }
} }
} }

View file

@ -3,6 +3,7 @@ mod file_sink;
extern crate rpassword; extern crate rpassword;
use std::{path::PathBuf}; use std::{path::PathBuf};
use std::path::Path;
use librespot::{core::authentication::Credentials, metadata::Playlist}; use librespot::{core::authentication::Credentials, metadata::Playlist};
use librespot::core::config::SessionConfig; use librespot::core::config::SessionConfig;
@ -82,15 +83,22 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec<Sp
let joined_path = destination.join(&filename); let joined_path = destination.join(&filename);
let path = joined_path.to_str().unwrap(); let path = joined_path.to_str().unwrap();
bar.set_message(full_track_name.as_str()); bar.set_message(full_track_name.as_str());
let mut file_sink = file_sink::FileSink::open(Some(path.to_owned()), librespot::playback::config::AudioFormat::S16);
file_sink.add_metadata(metadata); let path_with_no_extension = Path::new(path).with_extension("");
let (mut player, _) = Player::new(player_config.clone(), session.clone(), None, move || { if !path_with_no_extension.exists() {
Box::new(file_sink) let mut file_sink = file_sink::FileSink::open(Some(path.to_owned()), librespot::playback::config::AudioFormat::S16);
}); file_sink.add_metadata(metadata);
player.load(track, true, 0); let (mut player, _) = Player::new(player_config.clone(), session.clone(), None, move || {
player.await_end_of_track().await; Box::new(file_sink)
player.stop(); });
bar.inc(1); player.load(track, true, 0);
player.await_end_of_track().await;
player.stop();
bar.inc(1);
} else {
println!("File already exists: {}", path);
bar.inc(1);
}
} }
bar.finish(); bar.finish();
} }