From 1f333d647582c7dd800285f09f6d15c64ef11f35 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 1 Feb 2023 20:34:23 +0100 Subject: [PATCH 1/3] check if path exists (without extension) --- src/file_sink.rs | 15 ++++++--------- src/main.rs | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/file_sink.rs b/src/file_sink.rs index 8cfd812..966694b 100644 --- a/src/file_sink.rs +++ b/src/file_sink.rs @@ -22,16 +22,13 @@ impl 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, _audio_format: AudioFormat) -> Self { - if let Some(path) = path { - let file = path; - FileSink { - sink: file, - content: Vec::new(), - metadata: None - } - } else { - panic!(); + let file = path.unwrap_or_else(|| panic!()); + FileSink { + sink: file, + content: Vec::new(), + metadata: None } } } diff --git a/src/main.rs b/src/main.rs index c04a4b1..7d154d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod file_sink; extern crate rpassword; use std::{path::PathBuf}; +use std::path::Path; use librespot::{core::authentication::Credentials, metadata::Playlist}; use librespot::core::config::SessionConfig; @@ -82,15 +83,22 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec Date: Fri, 3 Feb 2023 17:26:47 +0100 Subject: [PATCH 2/3] clean filenames too --- src/main.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7d154d6..6e6b779 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,17 @@ async fn create_session(credentials: Credentials) -> Session { session } +fn make_filename_compatible(filename: &str) -> String { + let invalid_chars = ['<', '>', ':', '\'', '"', '/', '\\', '|', '?', '*']; + let mut clean = String::new(); + for c in filename.chars() { + if !invalid_chars.contains(&c) && c.is_ascii() && !c.is_control() && c.len_utf8() == 1 { + clean.push(c); + } + } + clean +} + async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec) { let player_config = PlayerConfig::default(); let bar_style = ProgressStyle::default_bar() @@ -79,10 +90,11 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec Date: Fri, 3 Feb 2023 19:01:03 +0100 Subject: [PATCH 3/3] fixed excluding logic - now it works --- src/file_sink.rs | 5 ++--- src/main.rs | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/file_sink.rs b/src/file_sink.rs index 966694b..3253273 100644 --- a/src/file_sink.rs +++ b/src/file_sink.rs @@ -22,11 +22,10 @@ impl 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, _audio_format: AudioFormat) -> Self { - let file = path.unwrap_or_else(|| panic!()); + let file_path = path.unwrap_or_else(|| panic!()); FileSink { - sink: file, + sink: file_path, content: Vec::new(), metadata: None } diff --git a/src/main.rs b/src/main.rs index 6e6b779..bda7df5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,8 +96,28 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec