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,17 +22,14 @@ 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,6 +83,9 @@ 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 path_with_no_extension = Path::new(path).with_extension("");
if !path_with_no_extension.exists() {
let mut file_sink = file_sink::FileSink::open(Some(path.to_owned()), librespot::playback::config::AudioFormat::S16); let mut file_sink = file_sink::FileSink::open(Some(path.to_owned()), librespot::playback::config::AudioFormat::S16);
file_sink.add_metadata(metadata); file_sink.add_metadata(metadata);
let (mut player, _) = Player::new(player_config.clone(), session.clone(), None, move || { let (mut player, _) = Player::new(player_config.clone(), session.clone(), None, move || {
@ -91,6 +95,10 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec<Sp
player.await_end_of_track().await; player.await_end_of_track().await;
player.stop(); player.stop();
bar.inc(1); bar.inc(1);
} else {
println!("File already exists: {}", path);
bar.inc(1);
}
} }
bar.finish(); bar.finish();
} }