From 71374eb73180966d11a7298df06fdc6e999c3c78 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 21 Oct 2025 16:20:51 +0000 Subject: [PATCH] tsk-44 (#47) Reviewed-on: https://git.kundeng.us/phoenix/icarus_meta/pulls/47 Co-authored-by: phoenix Co-committed-by: phoenix --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/detection/coverart.rs | 34 ++++++++++++++++++++++++++++++++-- src/lib.rs | 15 ++++++++++++++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 77eebfe..896c873 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,7 +81,7 @@ dependencies = [ [[package]] name = "icarus_meta" -version = "0.4.1" +version = "0.4.2" dependencies = [ "imghdr", "lofty", diff --git a/Cargo.toml b/Cargo.toml index 25441fa..398a1e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus_meta" -version = "0.4.1" +version = "0.4.2" edition = "2024" rust-version = "1.90" diff --git a/src/detection/coverart.rs b/src/detection/coverart.rs index 6bd5d01..e5d86de 100644 --- a/src/detection/coverart.rs +++ b/src/detection/coverart.rs @@ -1,5 +1,5 @@ /// Gets the file type of a CoverArt given it's path -pub fn file_type(filepath: &str) -> Result { +pub fn file_type_from_filepath(filepath: &str) -> Result { match imghdr::from_file(filepath) { Ok(Some(imghdr::Type::Jpeg)) => Ok(String::from("jpeg")), Ok(Some(imghdr::Type::Png)) => Ok(String::from("png")), @@ -9,6 +9,16 @@ pub fn file_type(filepath: &str) -> Result { } } +/// Gets the file type of a CoverArt given it's data +pub fn file_type_from_data(data: &Vec) -> Result { + match imghdr::from_bytes(data) { + Some(imghdr::Type::Jpeg) => Ok(String::from("jpeg")), + Some(imghdr::Type::Png) => Ok(String::from("png")), + None => Err(std::io::Error::other("Image file not supported")), + _ => Err(std::io::Error::other("Image file not supported")), + } +} + #[cfg(test)] mod tests { #[test] @@ -17,7 +27,27 @@ mod tests { let filename = String::from("Sample Tracks 3.png"); let filepath = format!("{directory}/{filename}"); - match super::file_type(&filepath) { + match super::file_type_from_filepath(&filepath) { + Ok(filetype) => { + assert_eq!( + filetype, "png", + "The file type of the CoverArt should have been png" + ); + } + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } + } + + #[test] + fn test_coverart_file_type_from_data() { + let directory = String::from(crate::test_util::util::TESTFILEDIRECTORY); + let filename = String::from("Sample Tracks 3.png"); + let filepath = format!("{directory}/{filename}"); + let data = crate::test_util::util::get_data_from_file(&filepath).unwrap(); + + match super::file_type_from_data(&data) { Ok(filetype) => { assert_eq!( filetype, "png", diff --git a/src/lib.rs b/src/lib.rs index 97c6803..dd4820b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ pub mod types; pub mod test_util { pub mod util { - use std::io::{self, Write}; + use std::io::{self, Read, Write}; use rand::Rng; @@ -39,6 +39,19 @@ pub mod test_util { std::fs::copy(src_path, dest_path) } + pub fn get_data_from_file(source_path: &str) -> Result, std::io::Error> { + match std::fs::File::open(source_path) { + Ok(mut file) => { + let mut data: Vec = Vec::new(); + match file.read_to_end(&mut data) { + Ok(_) => Ok(data), + Err(err) => Err(err), + } + } + Err(err) => Err(err), + } + } + pub fn file_exists(directory: &String, filename: &String) -> Result { match path_buf(directory, filename) { Ok(pf) => Ok(pf.exists()),