Skip to content

Commit 15d9ca8

Browse files
committed
OGG: Support reading COVERART fields
1 parent 51517d3 commit 15d9ca8

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- **MP4**:
1313
- `Atom::into_data`
1414
- `Atom::merge`
15+
- **OGG**: Support for reading "COVERART" fields, an old deprecated image storage format.
1516

1617
## Changed
1718
- **ID3v2**:

src/ogg/read.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ use super::tag::VorbisComments;
22
use super::verify_signature;
33
use crate::error::{ErrorKind, LoftyError, Result};
44
use crate::macros::{decode_err, err, parse_mode_choice};
5-
use crate::picture::Picture;
5+
use crate::picture::{MimeType, Picture, PictureInformation, PictureType};
66
use crate::probe::ParsingMode;
77

8+
use std::borrow::Cow;
89
use std::io::{Read, Seek, SeekFrom};
910

11+
use base64::Engine;
1012
use byteorder::{LittleEndian, ReadBytesExt};
1113
use ogg_pager::{Packets, PageHeader};
1214

@@ -104,6 +106,37 @@ where
104106
},
105107
}
106108
},
109+
k if k.eq_ignore_ascii_case(b"COVERART") => {
110+
// `COVERART` is an old deprecated image storage format. We have to convert it
111+
// to a `METADATA_BLOCK_PICTURE` for it to be useful.
112+
//
113+
// <https://wiki.xiph.org/VorbisComment#Conversion_to_METADATA_BLOCK_PICTURE>
114+
let picture_data = base64::engine::general_purpose::STANDARD.decode(value);
115+
116+
match picture_data {
117+
Ok(picture_data) => {
118+
let mime_type = Picture::mimetype_from_bin(&picture_data)
119+
.unwrap_or_else(|_| MimeType::Unknown(String::from("image/")));
120+
121+
let picture = Picture {
122+
pic_type: PictureType::Other,
123+
mime_type,
124+
description: None,
125+
data: Cow::from(picture_data),
126+
};
127+
128+
tag.pictures.push((picture, PictureInformation::default()))
129+
},
130+
Err(_) => {
131+
if parse_mode == ParsingMode::Strict {
132+
return Err(LoftyError::new(ErrorKind::NotAPicture));
133+
}
134+
135+
log::warn!("Failed to decode FLAC picture, discarding field");
136+
continue;
137+
},
138+
}
139+
},
107140
// The valid range is 0x20..=0x7D not including 0x3D
108141
k if k.iter().all(|c| (b' '..=b'}').contains(c) && *c != b'=') => {
109142
// SAFETY: We just verified that all of the bytes fall within the subset of ASCII

src/picture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ impl Picture {
784784
})
785785
}
786786

787-
fn mimetype_from_bin(bytes: &[u8]) -> Result<MimeType> {
787+
pub(crate) fn mimetype_from_bin(bytes: &[u8]) -> Result<MimeType> {
788788
match bytes[..8] {
789789
[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A] => Ok(MimeType::Png),
790790
[0xFF, 0xD8, ..] => Ok(MimeType::Jpeg),

0 commit comments

Comments
 (0)