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:
parent
fd2256c5a5
commit
eb200afe18
|
|
@ -523,7 +523,7 @@ class Event extends MatrixEvent {
|
||||||
/// event and returns it as a [MatrixFile]. If this event doesn't
|
/// event and returns it as a [MatrixFile]. If this event doesn't
|
||||||
/// contain an attachment, this throws an error. Set [getThumbnail] to
|
/// contain an attachment, this throws an error. Set [getThumbnail] to
|
||||||
/// true to download the thumbnail instead.
|
/// true to download the thumbnail instead.
|
||||||
Future<MatrixFile?> downloadAndDecryptAttachment(
|
Future<MatrixFile> downloadAndDecryptAttachment(
|
||||||
{bool getThumbnail = false,
|
{bool getThumbnail = false,
|
||||||
Future<Uint8List> Function(Uri)? downloadCallback}) async {
|
Future<Uint8List> Function(Uri)? downloadCallback}) async {
|
||||||
if (![EventTypes.Message, EventTypes.Sticker].contains(type)) {
|
if (![EventTypes.Message, EventTypes.Sticker].contains(type)) {
|
||||||
|
|
@ -584,8 +584,11 @@ class Event extends MatrixEvent {
|
||||||
);
|
);
|
||||||
uint8list = await client.runInBackground<Uint8List?, EncryptedFile>(
|
uint8list = await client.runInBackground<Uint8List?, EncryptedFile>(
|
||||||
decryptFile, 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.
|
/// Returns if this is a known event type.
|
||||||
|
|
|
||||||
|
|
@ -1196,7 +1196,7 @@ void main() {
|
||||||
}, room);
|
}, room);
|
||||||
var buffer = await event.downloadAndDecryptAttachment(
|
var buffer = await event.downloadAndDecryptAttachment(
|
||||||
downloadCallback: downloadCallback);
|
downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, FILE_BUFF);
|
expect(buffer.bytes, FILE_BUFF);
|
||||||
expect(event.attachmentOrThumbnailMxcUrl().toString(),
|
expect(event.attachmentOrThumbnailMxcUrl().toString(),
|
||||||
'mxc://example.org/file');
|
'mxc://example.org/file');
|
||||||
expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true).toString(),
|
expect(event.attachmentOrThumbnailMxcUrl(getThumbnail: true).toString(),
|
||||||
|
|
@ -1251,11 +1251,11 @@ void main() {
|
||||||
|
|
||||||
buffer = await event.downloadAndDecryptAttachment(
|
buffer = await event.downloadAndDecryptAttachment(
|
||||||
downloadCallback: downloadCallback);
|
downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, FILE_BUFF);
|
expect(buffer.bytes, FILE_BUFF);
|
||||||
|
|
||||||
buffer = await event.downloadAndDecryptAttachment(
|
buffer = await event.downloadAndDecryptAttachment(
|
||||||
getThumbnail: true, downloadCallback: downloadCallback);
|
getThumbnail: true, downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, THUMBNAIL_BUFF);
|
expect(buffer.bytes, THUMBNAIL_BUFF);
|
||||||
});
|
});
|
||||||
test('encrypted attachments', () async {
|
test('encrypted attachments', () async {
|
||||||
if (!olmEnabled) return;
|
if (!olmEnabled) return;
|
||||||
|
|
@ -1298,7 +1298,7 @@ void main() {
|
||||||
}, room);
|
}, room);
|
||||||
var buffer = await event.downloadAndDecryptAttachment(
|
var buffer = await event.downloadAndDecryptAttachment(
|
||||||
downloadCallback: downloadCallback);
|
downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, FILE_BUFF_DEC);
|
expect(buffer.bytes, FILE_BUFF_DEC);
|
||||||
|
|
||||||
event = Event.fromJson({
|
event = Event.fromJson({
|
||||||
'type': EventTypes.Message,
|
'type': EventTypes.Message,
|
||||||
|
|
@ -1351,11 +1351,11 @@ void main() {
|
||||||
expect(event.thumbnailMxcUrl.toString(), 'mxc://example.com/thumb');
|
expect(event.thumbnailMxcUrl.toString(), 'mxc://example.com/thumb');
|
||||||
buffer = await event.downloadAndDecryptAttachment(
|
buffer = await event.downloadAndDecryptAttachment(
|
||||||
downloadCallback: downloadCallback);
|
downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, FILE_BUFF_DEC);
|
expect(buffer.bytes, FILE_BUFF_DEC);
|
||||||
|
|
||||||
buffer = await event.downloadAndDecryptAttachment(
|
buffer = await event.downloadAndDecryptAttachment(
|
||||||
getThumbnail: true, downloadCallback: downloadCallback);
|
getThumbnail: true, downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, THUMB_BUFF_DEC);
|
expect(buffer.bytes, THUMB_BUFF_DEC);
|
||||||
|
|
||||||
await room.client.dispose(closeDatabase: true);
|
await room.client.dispose(closeDatabase: true);
|
||||||
});
|
});
|
||||||
|
|
@ -1389,11 +1389,11 @@ void main() {
|
||||||
downloadCallback: downloadCallback);
|
downloadCallback: downloadCallback);
|
||||||
expect(await event.isAttachmentInLocalStore(),
|
expect(await event.isAttachmentInLocalStore(),
|
||||||
event.room?.client.database?.supportsFileStoring);
|
event.room?.client.database?.supportsFileStoring);
|
||||||
expect(buffer?.bytes, FILE_BUFF);
|
expect(buffer.bytes, FILE_BUFF);
|
||||||
expect(serverHits, 1);
|
expect(serverHits, 1);
|
||||||
buffer = await event.downloadAndDecryptAttachment(
|
buffer = await event.downloadAndDecryptAttachment(
|
||||||
downloadCallback: downloadCallback);
|
downloadCallback: downloadCallback);
|
||||||
expect(buffer?.bytes, FILE_BUFF);
|
expect(buffer.bytes, FILE_BUFF);
|
||||||
expect(
|
expect(
|
||||||
serverHits, event.room!.client.database!.supportsFileStoring ? 1 : 2);
|
serverHits, event.room!.client.database!.supportsFileStoring ? 1 : 2);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue