tsk-45: Added another function

This commit is contained in:
2025-10-21 21:00:06 -04:00
parent 233bbdeaa1
commit 82fe3ae8e8

View File

@@ -1,10 +1,11 @@
/// Gets the file type of a Song from it's path
pub fn file_type_from_filepath( pub fn file_type_from_filepath(
filepath: &str, filepath: &str,
) -> Result<crate::detection::FileType, std::io::Error> { ) -> Result<crate::detection::FileType, std::io::Error> {
match infer::get_from_path(filepath) { match infer::get_from_path(filepath) {
Ok(Some(kind)) => { Ok(Some(kind)) => {
let mime = kind.mime_type(); let mime = kind.mime_type();
if mime == "audio/x-flac" { if mime == constants::mime::FLAC {
Ok(crate::detection::FileType { Ok(crate::detection::FileType {
mime: String::from(mime), mime: String::from(mime),
file_type: String::from(constants::FLAC_TYPE), 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<u8>) -> Result<crate::detection::FileType, std::io::Error> {
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 mod constants {
pub const FLAC_TYPE: &str = "flac"; pub const FLAC_TYPE: &str = "flac";
pub mod mime {
pub const FLAC: &str = "audio/x-flac";
}
} }
#[cfg(test)] #[cfg(test)]