refactor: download method should not return null

If the decryption fails, it should
throw an exception and not
return null.
This commit is contained in:
Krille Fear 2021-11-04 12:20:30 +01:00
parent fd2256c5a5
commit eb200afe18
2 changed files with 13 additions and 10 deletions

View File

@ -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<MatrixFile?> downloadAndDecryptAttachment(
Future<MatrixFile> downloadAndDecryptAttachment(
{bool getThumbnail = false,
Future<Uint8List> Function(Uri)? downloadCallback}) async {
if (![EventTypes.Message, EventTypes.Sticker].contains(type)) {
@ -584,8 +584,11 @@ class Event extends MatrixEvent {
);
uint8list = await client.runInBackground<Uint8List?, EncryptedFile>(
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.

View File

@ -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);