Skip to content

Commit 3bb7508

Browse files
authored
Fixes #200 and copies Pictures over from Id3v2 Tag to File.Tag (#201)
* Fixes #200 and copies Pictures over from Id3v2 Tag to File.Tag * Add null-safety for Pictures on Tag.CopyTo
1 parent 8fe0836 commit 3bb7508

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/TaglibSharp.Tests/FileFormats/Id3V2FormatTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using NUnit.Framework;
23
using TagLib;
34

@@ -137,5 +138,61 @@ public void URLLinkFrameTest ()
137138

138139
System.IO.File.Delete (tempFile);
139140
}
141+
142+
/// <summary>
143+
/// If we construct a new Id3v2 tag, then try to copy that onto a File.Tag
144+
/// We observe that simple text frames are copied, but APIC and GEOB aren't
145+
/// </summary>
146+
[Test]
147+
public void TestPicturesAreCopied ()
148+
{
149+
string tempFile = TestPath.Samples + "tmpwrite_sample_v2_only.mp3";
150+
151+
System.IO.File.Copy (sample_file, tempFile, true);
152+
153+
// Put a picture on the starting file
154+
File file = TagLib.File.Create (tempFile);
155+
var picture = new Picture (TestPath.Samples + "sample_gimp.gif") {
156+
Type = PictureType.BackCover,
157+
Description = "TEST description 1"
158+
};
159+
file.Tag.Pictures = new[] { picture };
160+
file.Save ();
161+
162+
Assert.IsTrue (file.Tag.Pictures.Count () == 1, "File should start with 1 picture");
163+
164+
// Now construct a new tag with a title, APIC and GEOB frame
165+
166+
var tag = new TagLib.Id3v2.Tag();
167+
tag.Title = "FOOBAR";
168+
169+
// single red dot (1x1 px red image) APIC frame found in wild
170+
var redDot = new byte[] { 65, 80, 73, 67, 0, 0, 0, 155, 0, 0, 0, 105, 109, 97, 103, 101, 47, 112, 110, 103, 0, 3, 0, 137, 80, 78,
171+
71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0, 144, 119, 83, 222, 0, 0, 0, 4, 103, 65,
172+
77, 65, 0, 0, 177, 143, 11, 252, 97, 5, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 18, 0, 0, 11, 18, 1, 210, 221, 126, 252, 0, 0, 0,
173+
24, 116, 69, 88, 116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 112, 97, 105, 110, 116, 46, 110, 101, 116, 32, 52, 46, 49, 46, 53,
174+
100, 71, 88, 82, 0, 0, 0, 12, 73, 68, 65, 84, 24, 87, 99, 248, 47, 162, 0, 0, 3, 73, 1, 52, 163, 224, 5, 179, 0, 0, 0, 0, 73, 69,
175+
78, 68, 174, 66, 96, 130 };
176+
var pictureFrame = new TagLib.Id3v2.AttachmentFrame (redDot, 3);
177+
178+
var geobFrame = new TagLib.Id3v2.AttachmentFrame ();
179+
geobFrame.MimeType = "plain/text";
180+
geobFrame.Description = "random";
181+
geobFrame.Type = PictureType.NotAPicture;
182+
geobFrame.Data = "random text in geob";
183+
184+
tag.AddFrame (pictureFrame);
185+
tag.AddFrame (geobFrame);
186+
187+
tag.CopyTo (file.Tag, false);
188+
189+
Assert.AreEqual ("MP3 title", file.Tag.Title, "Title shouldn't be copied if overwrite=false");
190+
Assert.AreEqual (1, file.Tag.Pictures.Count (), "GEOB/APIC frames shouldn't be copied if overwrite=false");
191+
192+
tag.CopyTo (file.Tag, true);
193+
194+
Assert.AreEqual (tag.Title, file.Tag.Title, "Title wasn't copied");
195+
Assert.AreEqual (tag.Pictures.Count (), file.Tag.Pictures.Count (), "GEOB/APIC frames weren't copied");
196+
}
140197
}
141198
}

src/TaglibSharp/Tag.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,9 @@ public virtual void CopyTo (Tag target, bool overwrite)
15631563

15641564
if (overwrite || target.DateTagged == null)
15651565
target.DateTagged = DateTagged;
1566+
1567+
if (overwrite || target.Pictures == null || target.Pictures.Length == 0)
1568+
target.Pictures = Pictures;
15661569
}
15671570

15681571
/// <summary>

0 commit comments

Comments
 (0)