Skip to content

Commit 273451e

Browse files
committed
OGG: Move VorbisComments picture creation to new function
1 parent 983866c commit 273451e

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/ogg/write.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::file::FileType;
44
use crate::macros::{decode_err, err, try_vec};
55
use crate::ogg::constants::{OPUSTAGS, VORBIS_COMMENT_HEAD};
66
use crate::ogg::tag::{create_vorbis_comments_ref, VorbisCommentsRef};
7-
use crate::picture::PictureInformation;
7+
use crate::picture::{Picture, PictureInformation};
88
use crate::tag::{Tag, TagType};
99

1010
use std::convert::TryFrom;
@@ -85,6 +85,30 @@ pub(crate) fn create_comments(
8585
Ok(())
8686
}
8787

88+
fn create_pictures(
89+
packet: &mut impl Write,
90+
count: &mut u32,
91+
pictures: &mut dyn Iterator<Item = (Picture, PictureInformation)>,
92+
) -> Result<()> {
93+
const PICTURE_KEY: &str = "METADATA_BLOCK_PICTURE=";
94+
95+
for (pic, info) in pictures {
96+
let picture = pic.as_flac_bytes(info, true);
97+
98+
let bytes_len = picture.len() + PICTURE_KEY.len();
99+
100+
if u32::try_from(bytes_len as u64).is_ok() {
101+
*count += 1;
102+
103+
packet.write_u32::<LittleEndian>(bytes_len as u32)?;
104+
packet.write_all(PICTURE_KEY.as_bytes())?;
105+
packet.write_all(&picture)?;
106+
}
107+
}
108+
109+
Ok(())
110+
}
111+
88112
pub(super) fn write<'a, II, IP>(
89113
file: &mut File,
90114
tag: &mut VorbisCommentsRef<'a, II, IP>,
@@ -93,7 +117,7 @@ pub(super) fn write<'a, II, IP>(
93117
) -> Result<()>
94118
where
95119
II: Iterator<Item = (&'a str, &'a str)>,
96-
IP: Iterator<Item = (&'a crate::picture::Picture, PictureInformation)>,
120+
IP: Iterator<Item = (&'a Picture, PictureInformation)>,
97121
{
98122
// TODO: Would be nice if we didn't have to read just to seek and reread immediately
99123

@@ -163,7 +187,7 @@ pub(super) fn create_metadata_packet<'a, II, IP>(
163187
) -> Result<Vec<u8>>
164188
where
165189
II: Iterator<Item = (&'a str, &'a str)>,
166-
IP: Iterator<Item = (&'a crate::picture::Picture, PictureInformation)>,
190+
IP: Iterator<Item = (&'a Picture, PictureInformation)>,
167191
{
168192
const PICTURE_KEY: &str = "METADATA_BLOCK_PICTURE=";
169193

@@ -179,20 +203,7 @@ where
179203

180204
let mut count = 0;
181205
create_comments(&mut new_comment_packet, &mut count, &mut tag.items)?;
182-
183-
for (pic, info) in &mut tag.pictures {
184-
let picture = pic.as_flac_bytes(info, true);
185-
186-
let bytes_len = picture.len() + PICTURE_KEY.len();
187-
188-
if u32::try_from(bytes_len as u64).is_ok() {
189-
count += 1;
190-
191-
new_comment_packet.write_u32::<LittleEndian>(bytes_len as u32)?;
192-
new_comment_packet.write_all(PICTURE_KEY.as_bytes())?;
193-
new_comment_packet.write_all(&picture)?;
194-
}
195-
}
206+
create_pictures(&mut new_comment_packet, &mut count, &mut tag.pictures)?;
196207

197208
// Seek back and write the item count
198209
new_comment_packet.seek(SeekFrom::Start(item_count_pos))?;

0 commit comments

Comments
 (0)