From 82fe3ae8e835bc5a0f37f853b7e2615edb38b235 Mon Sep 17 00:00:00 2001 From: phoenix Date: Tue, 21 Oct 2025 21:00:06 -0400 Subject: [PATCH] tsk-45: Added another function --- src/detection/song.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/detection/song.rs b/src/detection/song.rs index 97fd4df..53f791e 100644 --- a/src/detection/song.rs +++ b/src/detection/song.rs @@ -1,10 +1,11 @@ +/// Gets the file type of a Song from it's path pub fn file_type_from_filepath( filepath: &str, ) -> Result { match infer::get_from_path(filepath) { Ok(Some(kind)) => { let mime = kind.mime_type(); - if mime == "audio/x-flac" { + if mime == constants::mime::FLAC { Ok(crate::detection::FileType { mime: String::from(mime), file_type: String::from(constants::FLAC_TYPE), @@ -18,8 +19,32 @@ pub fn file_type_from_filepath( } } +/// Gets the file type of a Song given it's data +pub fn file_type_from_data(data: &Vec) -> Result { + match infer::get(data) { + Some(kind) => { + let mime = kind.mime_type(); + if mime == constants::mime::FLAC { + Ok(crate::detection::FileType { + mime: String::from(mime), + file_type: String::from(constants::FLAC_TYPE) + }) + } else { + Err(std::io::Error::other("Unsupported file type")) + } + } + None => { + Err(std::io::Error::other("File type not determined")) + } + } +} + pub mod constants { pub const FLAC_TYPE: &str = "flac"; + + pub mod mime { + pub const FLAC: &str = "audio/x-flac"; + } } #[cfg(test)]