Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix APIC Other picture type error #20

Merged
merged 1 commit into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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