diff --git a/src/components/tags/ape_tag.rs b/src/components/tags/ape_tag.rs index 60605b3c..6337061e 100644 --- a/src/components/tags/ape_tag.rs +++ b/src/components/tags/ape_tag.rs @@ -1,4 +1,4 @@ -use crate::types::picture::APE_PICTYPES; +use crate::types::picture::{APE_PICTYPES, PicType}; use crate::{ Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, Picture, Result, TagType, ToAny, ToAnyTag, }; @@ -259,6 +259,22 @@ impl AudioTagEdit for ApeTag { Some(Cow::from(pics)) } } + fn set_pictures(&mut self, pictures: Vec) { + self.remove_pictures(); + + for p in pictures { + let key = p.pic_type.as_ape_key(); + + if let Ok(item) = ape::Item::from_binary(key, p.as_ape_bytes()) { + self.inner.set_item(item) + } + } + } + fn remove_pictures(&mut self) { + for key in &APE_PICTYPES { + self.inner.remove_item(key); + } + } // Track number and total tracks are stored together as num/total? fn track_number(&self) -> Option { diff --git a/src/components/tags/id3_tag.rs b/src/components/tags/id3_tag.rs index a604a976..1462a226 100644 --- a/src/components/tags/id3_tag.rs +++ b/src/components/tags/id3_tag.rs @@ -310,6 +310,18 @@ impl AudioTagEdit for Id3v2Tag { None } + fn set_pictures(&mut self, pictures: Vec) { + self.remove_pictures(); + + for p in pictures { + if let Ok(pic) = TryInto::::try_into(p) { + self.inner.add_picture(pic) + } + } + } + fn remove_pictures(&mut self) { + self.inner.remove_all_pictures() + } fn track_number(&self) -> Option { self.inner.track() diff --git a/src/components/tags/mp4_tag.rs b/src/components/tags/mp4_tag.rs index 52afe2c3..c5fb7b3b 100644 --- a/src/components/tags/mp4_tag.rs +++ b/src/components/tags/mp4_tag.rs @@ -184,14 +184,12 @@ impl AudioTagEdit for Mp4Tag { } fn set_front_cover(&mut self, cover: Picture) { - self.inner.remove_artwork(); - - self.inner.add_artwork(match cover.mime_type { - MimeType::Png => mp4ameta::Data::Png(Vec::from(cover.data)), - MimeType::Jpeg => mp4ameta::Data::Jpeg(Vec::from(cover.data)), - MimeType::Bmp => mp4ameta::Data::Bmp(Vec::from(cover.data)), - _ => panic!("Attempt to add an invalid image format to MP4"), - }); + match cover.mime_type { + MimeType::Png => self.inner.add_artwork(mp4ameta::Data::Png(Vec::from(cover.data))), + MimeType::Jpeg => self.inner.add_artwork(mp4ameta::Data::Jpeg(Vec::from(cover.data))), + MimeType::Bmp => self.inner.add_artwork(mp4ameta::Data::Bmp(Vec::from(cover.data))), + _ => {}, + } } fn remove_front_cover(&mut self) { @@ -204,9 +202,6 @@ impl AudioTagEdit for Mp4Tag { fn set_back_cover(&mut self, cover: Picture) { self.set_front_cover(cover) } - fn remove_back_cover(&mut self) { - self.inner.remove_artwork(); - } fn pictures(&self) -> Option> { let mut pictures = Vec::new(); @@ -235,7 +230,17 @@ impl AudioTagEdit for Mp4Tag { Some(Cow::from(pictures)) } } + fn set_pictures(&mut self, pictures: Vec) { + self.remove_pictures(); + for p in pictures { + self.set_front_cover(p) + } + } + fn remove_pictures(&mut self) { + self.inner.remove_artwork() + } + fn track_number(&self) -> Option { self.inner.track_number().map(u32::from) } diff --git a/src/components/tags/ogg_tag.rs b/src/components/tags/ogg_tag.rs index 73915279..d74803ab 100644 --- a/src/components/tags/ogg_tag.rs +++ b/src/components/tags/ogg_tag.rs @@ -377,6 +377,13 @@ impl AudioTagEdit for OggTag { fn pictures(&self) -> Option> { self.inner.pictures.clone() } + fn set_pictures(&mut self, pictures: Vec) { + self.remove_pictures(); + self.inner.pictures = Some(Cow::from(pictures)) + } + fn remove_pictures(&mut self) { + self.inner.pictures = None + } fn track_number(&self) -> Option { if let Some(Ok(n)) = self.inner.get_value("TRACKNUMBER").map(str::parse::) { diff --git a/src/traits.rs b/src/traits.rs index ed964f05..e9f197fb 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -91,6 +91,10 @@ pub trait AudioTagEdit { fn pictures(&self) -> Option> { None } + /// Replace all pictures + fn set_pictures(&mut self, _pictures: Vec) {} + /// Remove all pictures + fn remove_pictures(&mut self) {} /// Returns the track number and total tracks fn track(&self) -> (Option, Option) {