diff --git a/src/meta.rs b/src/meta.rs index 234ff50..00c77c5 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -15,6 +15,43 @@ fn get_type(t: types::Type) -> Result { } } +pub mod coverart { + + use lofty::{file::AudioFile, ogg::OggPictureStorage}; + + pub fn get_coverart( + song_filepath: &String, + coverart_filepath: &String, + ) -> Result, std::io::Error> { + let _coverart_path = std::path::Path::new(coverart_filepath); + + 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(); + let res = pictures.to_vec(); + 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 { match std::fs::File::open(filepath) { Ok(mut content) => { @@ -109,6 +146,23 @@ mod tests { use super::*; mod util { + + use std::io::{self, Write}; + + // Function to save a Vec to a file + pub fn save_bytes_to_file(bytes: &[u8], file_path: &String) -> io::Result<()> { + let file = std::path::Path::new(file_path); + let mut file = std::fs::File::create(file)?; + + match file.write_all(bytes) { + Ok(res) => { + Ok(()) + } + Err(err) => { + Err(err) + } + } + } pub fn get_full_path( directory: &String, filename: &String, @@ -787,4 +841,43 @@ mod tests { }; } } + + mod pictures { + + use super::*; + + #[test] + fn test_get_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("track08.flac"); + let new_filepath = test_dir.clone() + "/" + &test_filename; + + match file_exists(&dir, &filename) { + Ok(_) => { + let filepath = get_full_path(&dir, &filename).unwrap(); + + match coverart::get_coverart(&filepath, &new_filepath) { + Ok(coverart) => { + let is_empty = coverart.is_empty(); + assert_eq!(is_empty, false, "Should not be empty"); + // Save image + let mut new_coverart_path: String = test_dir.clone(); + new_coverart_path += &String::from("/newcovvv.png"); + let _ = util::save_bytes_to_file(&coverart, &new_coverart_path); + } + Err(err) => { + assert!(false, "Error: {:?}", err.to_string()); + } + } + } + Err(err) => { + assert!(false, "Error: File does not exist {:?}", err.to_string()); + } + }; + } + } }