Compare commits
5 Commits
13bc89cd98
...
02631fc14c
| Author | SHA1 | Date | |
|---|---|---|---|
|
02631fc14c
|
|||
|
25c85212ca
|
|||
|
250a009fad
|
|||
|
fef43b9799
|
|||
|
fa5203013c
|
@@ -1,8 +1,16 @@
|
||||
/// Gets the file type of a CoverArt given it's path
|
||||
pub fn file_type_from_filepath(filepath: &str) -> Result<String, std::io::Error> {
|
||||
pub fn file_type_from_filepath(
|
||||
filepath: &str,
|
||||
) -> Result<crate::detection::FileType, 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(crate::detection::FileType {
|
||||
mime: String::from("image/jpeg"),
|
||||
file_type: String::from(constants::JPEG_TYPE),
|
||||
}),
|
||||
Ok(Some(imghdr::Type::Png)) => Ok(crate::detection::FileType {
|
||||
mime: String::from("image/png"),
|
||||
file_type: 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")),
|
||||
@@ -10,15 +18,27 @@ 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> {
|
||||
pub fn file_type_from_data(data: &Vec<u8>) -> Result<crate::detection::FileType, 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(crate::detection::FileType {
|
||||
mime: String::from("image/jpeg"),
|
||||
file_type: String::from(constants::JPEG_TYPE),
|
||||
}),
|
||||
Some(imghdr::Type::Png) => Ok(crate::detection::FileType {
|
||||
mime: String::from("image/png"),
|
||||
file_type: 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]
|
||||
@@ -30,7 +50,8 @@ mod tests {
|
||||
match super::file_type_from_filepath(&filepath) {
|
||||
Ok(filetype) => {
|
||||
assert_eq!(
|
||||
filetype, "png",
|
||||
filetype.file_type,
|
||||
super::constants::PNG_TYPE,
|
||||
"The file type of the CoverArt should have been png"
|
||||
);
|
||||
}
|
||||
@@ -50,7 +71,8 @@ mod tests {
|
||||
match super::file_type_from_data(&data) {
|
||||
Ok(filetype) => {
|
||||
assert_eq!(
|
||||
filetype, "png",
|
||||
filetype.file_type,
|
||||
super::constants::PNG_TYPE,
|
||||
"The file type of the CoverArt should have been png"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1 +1,7 @@
|
||||
pub mod coverart;
|
||||
pub mod song;
|
||||
|
||||
pub struct FileType {
|
||||
pub mime: String,
|
||||
pub file_type: String,
|
||||
}
|
||||
|
||||
23
src/detection/song.rs
Normal file
23
src/detection/song.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
pub fn file_type_from_filepath(
|
||||
filepath: &str,
|
||||
) -> Result<crate::detection::FileType, std::io::Error> {
|
||||
match infer::get_from_path(filepath) {
|
||||
Ok(Some(kind)) => {
|
||||
let mime = kind.mime_type();
|
||||
if mime == "audio/x-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"))
|
||||
}
|
||||
}
|
||||
Ok(None) => Err(std::io::Error::other("File type not determined")),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub mod constants {
|
||||
pub const FLAC_TYPE: &str = "flac";
|
||||
}
|
||||
Reference in New Issue
Block a user