Skip to content

Commit

Permalink
fix APIC picture type error
Browse files Browse the repository at this point in the history
  • Loading branch information
algoshipda committed Aug 18, 2020
1 parent 225a0e8 commit 7edd4db
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
28 changes: 16 additions & 12 deletions lib/src/frames/id3v2/apic_frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,27 @@ class ApicFrame with ID3V2Frame<AttachedPicture> {

@override
AttachedPicture decodeBody(List<int> data, Encoding enc) {
final splitIndex1 = data.indexOf(0x00);
final endOfMimeType = data.indexOf(0x00);

final mime = latin1.decode(data.sublist(0, splitIndex1));
final imageType = data[splitIndex1 + 1];
final mime = latin1.decode(data.sublist(0, endOfMimeType));
final imageType = data[endOfMimeType + 1];

final splitIndex2 = enc is UTF16
? indexOfSplitPattern(
data.sublist(splitIndex1 + 1), [0x00, 0x00], splitIndex1)
: data.sublist(splitIndex1 + 1).indexOf(0x00) + splitIndex1 + 1;
final startOfDescription = endOfMimeType + 2;
final endOfDescription = indexOfSplitPattern(
data, enc is UTF16 ? [0x00, 0x00] : [0x00], startOfDescription);

final description = enc.decode(data.sublist(splitIndex1 + 2, splitIndex2));
final description =
enc.decode(data.sublist(startOfDescription, endOfDescription));

final imageData = _imageExtractors.containsKey(mime)
? _imageExtractors[mime]().parse(data.sublist(splitIndex2))
: data.sublist(splitIndex2);
final startOfImageData = endOfDescription + (enc is UTF16 ? 2 : 1);

return AttachedPicture(mime, imageType, description, imageData);
final imageData = data.sublist(startOfImageData);

final extractedImageData = _imageExtractors.containsKey(mime)
? _imageExtractors[mime]().parse(imageData)
: imageData;

return AttachedPicture(mime, imageType, description, extractedImageData);
}

@override
Expand Down
24 changes: 23 additions & 1 deletion test/dart_tags_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void main() {
..writeAsBytesSync(pic.imageData);

final html =
'<div><p>${pic.description}}</p><img src="data:${pic.mime};base64, ${pic.imageData64}" alt="Red dot" /></div>';
'<div><p>${pic.description}</p><img src="data:${pic.mime};base64, ${pic.imageData64}" alt="Red dot" /></div>';

File('$outputDir/${pic.description}.html')
..createSync(recursive: true)
Expand All @@ -454,6 +454,28 @@ void main() {
expect(pic, equals(pic1));
});

//https://github.com/NiKoTron/dart-tags/issues/19
test('Wrong APIC tags decoding on on Other picture type.', () async {
final pic1 = AttachedPicture(
'image/jpeg', 0x00, 'foo.jpg', picture.readAsBytesSync());

final tag = Tag()
..tags = {'picture': pic1}
..type = 'ID3'
..version = '2.4';

final writer = ID3V2Writer();

final blocks = writer.write(await file2.readAsBytes(), tag);

final r = ID3V2Reader();
final f = await r.read(blocks);

// ignore: avoid_as
final AttachedPicture pic = (f.tags['picture'] as Map)['Other'];
expect(pic, equals(pic1));
});

//https://github.com/NiKoTron/dart-tags/issues/13
test('Wrong utf16 decoding', () async {
final expectedArtist = 'Полина Гагарина&Егор Крид';
Expand Down

0 comments on commit 7edd4db

Please sign in to comment.