tsk-44: Identify CoverArt type (#46)
Some checks failed
Rust Build / Check (push) Successful in 27s
Release Tagging / release (push) Successful in 35s
Rust Build / Test Suite (push) Successful in 38s
Rust Build / Clippy (push) Successful in 35s
Rust Build / Rustfmt (push) Failing after 37s
Rust Build / build (push) Successful in 36s

Closes #44

Reviewed-on: #46
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
2025-10-21 15:48:40 +00:00
committed by phoenix
parent f372d05915
commit 5101f36aed
6 changed files with 139 additions and 25 deletions

32
src/detection/coverart.rs Normal file
View File

@@ -0,0 +1,32 @@
/// Gets the file type of a CoverArt given it's path
pub fn file_type(filepath: &str) -> Result<String, std::io::Error> {
match imghdr::from_file(filepath) {
Ok(Some(imghdr::Type::Jpeg)) => Ok(String::from("jpeg")),
Ok(Some(imghdr::Type::Png)) => Ok(String::from("png")),
Ok(None) => Err(std::io::Error::other("Image file not supported")),
Err(err) => Err(err),
_ => Err(std::io::Error::other("Image file not supported")),
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_coverart_file_type() {
let directory = String::from(crate::test_util::util::TESTFILEDIRECTORY);
let filename = String::from("Sample Tracks 3.png");
let filepath = format!("{directory}/{filename}");
match super::file_type(&filepath) {
Ok(filetype) => {
assert_eq!(
filetype, "png",
"The file type of the CoverArt should have been png"
);
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
}
}
}

1
src/detection/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod coverart;

View File

@@ -1,3 +1,4 @@
pub mod detection;
pub mod meta;
pub mod properties;
pub mod types;
@@ -6,6 +7,8 @@ pub mod test_util {
pub mod util {
use std::io::{self, Write};
use rand::Rng;
// Function to save a Vec<u8> to a file
pub fn save_bytes_to_file(bytes: &[u8], file_path: &String) -> io::Result<()> {
let file = std::path::Path::new(file_path);
@@ -43,6 +46,25 @@ pub mod test_util {
}
}
pub fn generate_filename() -> Result<String, std::io::Error> {
let mut filename = String::from("track-");
let length = 20;
let characters = "abcdef0123456789";
let amount_of_characters = characters.len() - 1;
let mut rng = rand::rng();
for _ in 0..length {
let index = rng.random_range(0..=amount_of_characters);
let random_c = characters.chars().nth(index);
if let Some(c) = random_c {
filename.push(c);
}
}
Ok(format!("{filename}.flac"))
}
fn path_buf(
directory: &String,
filename: &String,

View File

@@ -544,7 +544,7 @@ mod tests {
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 test_filename = util::generate_filename().unwrap();
let new_filepath = test_dir + "/" + &test_filename;
match file_exists(&dir, &filename) {
@@ -597,7 +597,7 @@ mod tests {
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 test_filename = util::generate_filename().unwrap();
let new_filepath = test_dir + "/" + &test_filename;
match file_exists(&dir, &filename) {
@@ -806,7 +806,7 @@ mod tests {
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 test_filename = util::generate_filename().unwrap();
let new_filepath = test_dir + "/" + &test_filename;
match file_exists(&dir, &filename) {
@@ -907,7 +907,7 @@ mod tests {
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 test_filename = util::generate_filename().unwrap();
let new_filepath = test_dir + "/" + &test_filename;
match file_exists(&dir, &filename) {
@@ -1021,7 +1021,7 @@ mod tests {
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 test_filename = util::generate_filename().unwrap();
let new_filepath = test_dir + "/" + &test_filename;
match file_exists(&dir, &filename) {