|
|
|
|
@@ -1,8 +1,8 @@
|
|
|
|
|
/// Gets the file type of a CoverArt given it's path
|
|
|
|
|
pub fn file_type_from_filepath(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(Some(imghdr::Type::Jpeg)) => Ok(String::from(constants::JPEG_TYPE)),
|
|
|
|
|
Ok(Some(imghdr::Type::Png)) => Ok(String::from(constants::PNG_TYPE)),
|
|
|
|
|
Ok(None) => Err(std::io::Error::other("Image file not supported")),
|
|
|
|
|
Err(err) => Err(err),
|
|
|
|
|
_ => Err(std::io::Error::other("Image file not supported")),
|
|
|
|
|
@@ -12,13 +12,19 @@ pub fn file_type_from_filepath(filepath: &str) -> Result<String, std::io::Error>
|
|
|
|
|
/// Gets the file type of a CoverArt given it's data
|
|
|
|
|
pub fn file_type_from_data(data: &Vec<u8>) -> Result<String, std::io::Error> {
|
|
|
|
|
match imghdr::from_bytes(data) {
|
|
|
|
|
Some(imghdr::Type::Jpeg) => Ok(String::from("jpeg")),
|
|
|
|
|
Some(imghdr::Type::Png) => Ok(String::from("png")),
|
|
|
|
|
Some(imghdr::Type::Jpeg) => Ok(String::from(constants::JPEG_TYPE)),
|
|
|
|
|
Some(imghdr::Type::Png) => Ok(String::from(constants::PNG_TYPE)),
|
|
|
|
|
None => Err(std::io::Error::other("Image file not supported")),
|
|
|
|
|
_ => Err(std::io::Error::other("Image file not supported")),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub mod constants {
|
|
|
|
|
pub const PNG_TYPE: &str = "png";
|
|
|
|
|
pub const JPEG_TYPE: &str = "jpeg";
|
|
|
|
|
pub const JPG_TYPE: &str = "jpg";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
#[test]
|
|
|
|
|
|