Skip to content

Commit

Permalink
docs: metadata.rs: add comments about magic_bytes length
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Sep 19, 2020
1 parent be87e0d commit af321dc
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/metadata.rs
Expand Up @@ -47,15 +47,21 @@ macro_rules! impl_tag_getter {
impl Metadata {
pub fn from_path(path: impl AsRef<Path>) -> AnyResult<Self> {
let mut file = File::open(&path)?;
// NOTE(erichdongubler): This could be smaller if media types with larger magic bytes
// length requirements for `infer` get removed, so let's keep a table below of length
// required for each.
let mut magic_bytes = [0; 4];
file.read_exact(&mut magic_bytes)
.map_err(|_| MusoError::NotSupported)?;

let infer = infer::Infer::new();
let ftype = infer.get(&magic_bytes).ok_or(MusoError::NotSupported)?;
match ftype.mime.as_str() {
// Minimum: 4 bytes
"audio/x-flac" => Metadata::from_flac_vorbis(&path),
// Minimum: 4 bytes
"audio/mpeg" => Metadata::from_id3(&path),
// Minimum: 4 bytes
"audio/ogg" => Metadata::from_ogg_vorbis(&path),
_ => Err(MusoError::NotSupported.into()),
}
Expand Down

0 comments on commit af321dc

Please sign in to comment.