From eb200afe182327cf316e9ac75294f286eaacf1da Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Thu, 4 Nov 2021 12:20:30 +0100 Subject: [PATCH] refactor: download method should not return null If the decryption fails, it should throw an exception and not return null. --- lib/src/event.dart | 7 +++++-- test/event_test.dart | 16 ++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index 17fb4d18..fc81e213 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -523,7 +523,7 @@ class Event extends MatrixEvent { /// event and returns it as a [MatrixFile]. If this event doesn't /// contain an attachment, this throws an error. Set [getThumbnail] to /// true to download the thumbnail instead. - Future downloadAndDecryptAttachment( + Future downloadAndDecryptAttachment( {bool getThumbnail = false, Future Function(Uri)? downloadCallback}) async { if (![EventTypes.Message, EventTypes.Sticker].contains(type)) { @@ -584,8 +584,11 @@ class Event extends MatrixEvent { ); uint8list = await client.runInBackground( decryptFile, encryptedFile); + if (uint8list == null) { + throw ('Unable to decrypt file'); + } } - return uint8list != null ? MatrixFile(bytes: uint8list, name: body) : null; + return MatrixFile(bytes: uint8list, name: body); } /// Returns if this is a known event type. diff --git a/test/event_test.dart b/test/event_test.dart index 6be6317b..28d43c0e 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -1196,7 +1196,7 @@ void main() { }, room); var buffer = await event.downloadAndDecryptAttachment( downloadCallback: downloadCallback); - expect(buffer?.bytes, FILE_BUFF); + expect(buffer.bytes, FILE_BUFF); expect(event.attachmentOrThumbnailMxcUrl().toString(), 'mxc://example.org/file'); expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true).toString(), @@ -1251,11 +1251,11 @@ void main() { buffer = await event.downloadAndDecryptAttachment( downloadCallback: downloadCallback); - expect(buffer?.bytes, FILE_BUFF); + expect(buffer.bytes, FILE_BUFF); buffer = await event.downloadAndDecryptAttachment( getThumbnail: true, downloadCallback: downloadCallback); - expect(buffer?.bytes, THUMBNAIL_BUFF); + expect(buffer.bytes, THUMBNAIL_BUFF); }); test('encrypted attachments', () async { if (!olmEnabled) return; @@ -1298,7 +1298,7 @@ void main() { }, room); var buffer = await event.downloadAndDecryptAttachment( downloadCallback: downloadCallback); - expect(buffer?.bytes, FILE_BUFF_DEC); + expect(buffer.bytes, FILE_BUFF_DEC); event = Event.fromJson({ 'type': EventTypes.Message, @@ -1351,11 +1351,11 @@ void main() { expect(event.thumbnailMxcUrl.toString(), 'mxc://example.com/thumb'); buffer = await event.downloadAndDecryptAttachment( downloadCallback: downloadCallback); - expect(buffer?.bytes, FILE_BUFF_DEC); + expect(buffer.bytes, FILE_BUFF_DEC); buffer = await event.downloadAndDecryptAttachment( getThumbnail: true, downloadCallback: downloadCallback); - expect(buffer?.bytes, THUMB_BUFF_DEC); + expect(buffer.bytes, THUMB_BUFF_DEC); await room.client.dispose(closeDatabase: true); }); @@ -1389,11 +1389,11 @@ void main() { downloadCallback: downloadCallback); expect(await event.isAttachmentInLocalStore(), event.room?.client.database?.supportsFileStoring); - expect(buffer?.bytes, FILE_BUFF); + expect(buffer.bytes, FILE_BUFF); expect(serverHits, 1); buffer = await event.downloadAndDecryptAttachment( downloadCallback: downloadCallback); - expect(buffer?.bytes, FILE_BUFF); + expect(buffer.bytes, FILE_BUFF); expect( serverHits, event.room!.client.database!.supportsFileStoring ? 1 : 2);