From 138381fac0c3504972fdfc044bc9c89c5b3d7784 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:29:56 -0400 Subject: [PATCH 1/7] Added code for checking if file has pictures --- src/meta.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/meta.rs b/src/meta.rs index 2c9443f..4013710 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -88,6 +88,45 @@ pub mod coverart { Err(err) => Err(err), } } + + pub fn contains_coverart(song_filepath: &String) -> Result<(bool, usize), std::io::Error> { + match std::fs::File::open(song_filepath) { + Ok(mut file) => { + match lofty::flac::FlacFile::read_from( + &mut file, + lofty::config::ParseOptions::new(), + ) { + Ok(flac_file) => { + let pictures = flac_file.pictures(); + if pictures.is_empty() { + Ok((false, 0)) + } else { + let res = pictures.to_vec(); + if res.is_empty() { + Ok((false, 0)) + } else { + Ok((true, res.len())) + } + } + + /* + if !res.is_empty() { + let picture = &res[0]; + Ok(picture.clone().0.into_data()) + } else { + Ok(Vec::new()) + } + */ + } + Err(err) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + err.to_string(), + )), + } + } + Err(err) => Err(err), + } + } } pub fn get_meta(t: types::Type, filepath: &String) -> Result { @@ -944,5 +983,33 @@ mod tests { } }; } + + #[test] + fn test_picture_exists() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + // let new_coverart = String::from("Sample Tracks 3 - Other one.png"); + // let new_cover_art_path = get_full_path(&dir, &new_coverart).unwrap(); + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match coverart::contains_coverart(&filepath) { + Ok((exists, pictures)) => { + assert!(exists, "File should have a cover art"); + assert!((pictures > 0), "No cover art was found in the file"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } } } -- 2.43.0 From d4258b9bcc45fedc719076d577833554409cd7eb Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:31:22 -0400 Subject: [PATCH 2/7] Code cleanup --- src/meta.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/meta.rs b/src/meta.rs index 4013710..42c3661 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -108,15 +108,6 @@ pub mod coverart { Ok((true, res.len())) } } - - /* - if !res.is_empty() { - let picture = &res[0]; - Ok(picture.clone().0.into_data()) - } else { - Ok(Vec::new()) - } - */ } Err(err) => Err(std::io::Error::new( std::io::ErrorKind::InvalidData, @@ -989,9 +980,6 @@ mod tests { let filename = util::get_filename(1); let dir = String::from(util::TESTFILEDIRECTORY); - // let new_coverart = String::from("Sample Tracks 3 - Other one.png"); - // let new_cover_art_path = get_full_path(&dir, &new_coverart).unwrap(); - match file_exists(&dir, &filename) { Ok(_) => { let filepath = get_full_path(&dir, &filename).unwrap(); -- 2.43.0 From 6fdfcb89bf1dc75ac2fa6c953b3e948d5d41e98f Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:31:47 -0400 Subject: [PATCH 3/7] Version bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a68cd0f..7f589f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus_meta" -version = "0.1.0" +version = "0.1.10" edition = "2024" rust-version = "1.86" -- 2.43.0 From 2c95407b784c1bcba958b714e95fbe56616edfcd Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:39:20 -0400 Subject: [PATCH 4/7] Linting --- src/meta.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/meta.rs b/src/meta.rs index 42c3661..a2ea158 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -118,6 +118,37 @@ pub mod coverart { Err(err) => Err(err), } } + + pub fn remove_coverart(song_filepath: &String) -> Result, std::io::Error> { + match std::fs::File::open(song_filepath) { + Ok(mut file) => { + match lofty::flac::FlacFile::read_from( + &mut file, + lofty::config::ParseOptions::new(), + ) { + Ok(mut flac_file) => { + let pictures = flac_file.pictures(); + let res = pictures.to_vec(); + if !res.is_empty() { + let picture = &res[0]; + flac_file.remove_picture(0); + Ok(picture.clone().0.into_data()) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "No pictures found", + )) + } + } + Err(err) => Err(std::io::Error::new( + std::io::ErrorKind::InvalidData, + err.to_string(), + )), + } + } + Err(err) => Err(err), + } + } } pub fn get_meta(t: types::Type, filepath: &String) -> Result { -- 2.43.0 From 41f11ed275949b131568abe6354bbb435b08a828 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:50:27 -0400 Subject: [PATCH 5/7] Added test --- src/meta.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/meta.rs b/src/meta.rs index a2ea158..b7c9855 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -1030,5 +1030,42 @@ mod tests { } }; } + + #[test] + fn test_remove_picture() { + let filename = util::get_filename(1); + let dir = String::from(util::TESTFILEDIRECTORY); + + let temp_file = tempfile::tempdir().expect("Could not create test directory"); + let test_dir = String::from(temp_file.path().to_str().unwrap()); + let test_filename = String::from("track09.flac"); + let new_filepath = get_full_path(&test_dir, &test_filename).unwrap(); + + // let new_coverart = String::from("Sample Tracks 3 - Other one.png"); + // let new_cover_art_path = get_full_path(&dir, &new_coverart).unwrap(); + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match util::copy_file(&filepath, &new_filepath) { + Ok(_o) => match coverart::remove_coverart(&new_filepath) { + Ok(bytes) => { + assert_eq!(false, bytes.is_empty(), "This should not be empty"); + } + Err(err) => { + assert!(false, "Error: {:?}", err); + } + }, + Err(err) => { + assert!(false, "Error: {:?}", err); + } + } + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } } } -- 2.43.0 From 0c97e5c4869505c07e7247a147b283edc97a553c Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:51:10 -0400 Subject: [PATCH 6/7] Code cleanup --- src/meta.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/meta.rs b/src/meta.rs index b7c9855..63f687b 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -1041,9 +1041,6 @@ mod tests { let test_filename = String::from("track09.flac"); let new_filepath = get_full_path(&test_dir, &test_filename).unwrap(); - // let new_coverart = String::from("Sample Tracks 3 - Other one.png"); - // let new_cover_art_path = get_full_path(&dir, &new_coverart).unwrap(); - match file_exists(&dir, &filename) { Ok(_) => { let filepath = get_full_path(&dir, &filename).unwrap(); -- 2.43.0 From 9e056a9f1787d4f3c7c4bd4dc21eaa1926be40b3 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 18 Apr 2025 20:51:48 -0400 Subject: [PATCH 7/7] Version bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7f589f8..133ca6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus_meta" -version = "0.1.10" +version = "0.1.11" edition = "2024" rust-version = "1.86" -- 2.43.0