diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index 549265597..bb05b16c6 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import 'dart:io'; import 'dart:typed_data'; import 'package:matrix/encryption/utils/olm_session.dart'; @@ -97,6 +98,8 @@ abstract class DatabaseApi { Future getFile(Uri mxcUri); + Future getFileEntity(Uri mxcUri); + Future storeFile(Uri mxcUri, Uint8List bytes, int time); Future storeSyncFilterId( diff --git a/lib/src/database/hive_collections_database.dart b/lib/src/database/hive_collections_database.dart index 9fdbf0b5e..123e90470 100644 --- a/lib/src/database/hive_collections_database.dart +++ b/lib/src/database/hive_collections_database.dart @@ -18,6 +18,7 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'dart:math'; import 'dart:typed_data'; @@ -461,6 +462,11 @@ class HiveCollectionsDatabase extends DatabaseApi { return null; } + @override + Future getFileEntity(Uri mxcUri) async { + return null; + } + @override Future getInboundGroupSession( String roomId, diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index 388633d6a..dbf46ee90 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -18,6 +18,7 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'dart:math'; import 'dart:typed_data'; @@ -1466,6 +1467,11 @@ class FamedlySdkHiveDatabase extends DatabaseApi { // see no need to implement this in a deprecated part throw UnimplementedError(); } + + @override + Future getFileEntity(Uri mxcUri) { + throw UnimplementedError(); + } } dynamic _castValue(dynamic value) { diff --git a/lib/src/event.dart b/lib/src/event.dart index 01445e4f5..ca82e8e40 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -588,6 +588,14 @@ class Event extends MatrixEvent { return uint8list != null; } + bool isFileStoreable({bool getThumbnail = false}) { + final database = room.client.database; + final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap; + return database != null && + thisInfoMap['size'] is int && + thisInfoMap['size'] <= database.maxFileSize; + } + /// Downloads (and decrypts if necessary) the attachment of this /// event and returns it as a [MatrixFile]. If this event doesn't /// contain an attachment, this throws an error. Set [getThumbnail] to @@ -613,12 +621,8 @@ class Event extends MatrixEvent { if (isEncrypted && !room.client.encryptionEnabled) { throw ('Encryption is not enabled in your Client.'); } - - // Is this file storeable? - final thisInfoMap = getThumbnail ? thumbnailInfoMap : infoMap; - var storeable = database != null && - thisInfoMap['size'] is int && - thisInfoMap['size'] <= database.maxFileSize; + + var storeable = isFileStoreable(getThumbnail: getThumbnail); Uint8List? uint8list; if (storeable) { diff --git a/lib/src/utils/crypto/native.dart b/lib/src/utils/crypto/native.dart index 8b713eeb8..675717983 100644 --- a/lib/src/utils/crypto/native.dart +++ b/lib/src/utils/crypto/native.dart @@ -217,7 +217,7 @@ abstract class Cipher { final intPointer = memNeeded.cast(); final keyPointer = memNeeded.elementAt(sizeOf()); final initialVectorPointer = keyPointer.elementAt(keyDecoded.length); - final hashValuePointer = initialVectorPointer.elementAt(maxHashSize); + final hashValuePointer = initialVectorPointer.elementAt(initialVectorDecoded.length); final hashSizePointer = hashValuePointer.elementAt(maxHashSize); IOSink? outIoSink; diff --git a/lib/src/utils/matrix_file.dart b/lib/src/utils/matrix_file.dart index ffb17c164..04d54cc6f 100644 --- a/lib/src/utils/matrix_file.dart +++ b/lib/src/utils/matrix_file.dart @@ -77,8 +77,8 @@ class MatrixFile { mimeType: fileInfo.mimeType, filePath: fileInfo.filePath, bytes: null, - width: (fileInfo.metadata['w'] as double?)?.toInt(), - height: (fileInfo.metadata['h'] as double?)?.toInt(), + width: fileInfo.metadata['w'], + height: fileInfo.metadata['h'], ); } if (msgType == MessageTypes.Video) { @@ -89,8 +89,8 @@ class MatrixFile { name: fileInfo.fileName, mimeType: fileInfo.mimeType, filePath: fileInfo.filePath, - width: (fileInfo.metadata['w'] as double?)?.toInt(), - height: (fileInfo.metadata['h'] as double?)?.toInt(), + width: fileInfo.metadata['w'], + height: fileInfo.metadata['h'], duration: fileInfo.metadata['duration'], ); } diff --git a/lib/src/utils/models/file_info.dart b/lib/src/utils/models/file_info.dart index 0d636fc9e..fc50697e6 100644 --- a/lib/src/utils/models/file_info.dart +++ b/lib/src/utils/models/file_info.dart @@ -11,7 +11,12 @@ class FileInfo with EquatableMixin { final int fileSize; final Stream>? readStream; - FileInfo(this.fileName, this.filePath, this.fileSize, {this.readStream}); + FileInfo( + this.fileName, + this.filePath, + this.fileSize,{ + this.readStream, + }); factory FileInfo.empty() { return FileInfo('', '', 0); diff --git a/lib/src/utils/models/image_file_info.dart b/lib/src/utils/models/image_file_info.dart index 279669eac..05423ad0b 100644 --- a/lib/src/utils/models/image_file_info.dart +++ b/lib/src/utils/models/image_file_info.dart @@ -3,8 +3,9 @@ import 'package:matrix/matrix.dart'; class ImageFileInfo extends FileInfo { ImageFileInfo( super.fileName, - super.filePath, + super.filePath, super.fileSize, { + super.readStream, this.width, this.height, } @@ -18,8 +19,8 @@ class ImageFileInfo extends FileInfo { Map get metadata => ({ 'mimetype': mimeType, 'size': fileSize, - 'w': width?.toDouble(), - 'h': height?.toDouble(), + 'w': width, + 'h': height, }); @override diff --git a/lib/src/utils/models/video_file_info.dart b/lib/src/utils/models/video_file_info.dart index 5b67cfa88..8eeb430a2 100644 --- a/lib/src/utils/models/video_file_info.dart +++ b/lib/src/utils/models/video_file_info.dart @@ -15,6 +15,7 @@ class VideoFileInfo extends FileInfo { super.fileName, super.filePath, super.fileSize, { + super.readStream, required this.imagePlaceholderBytes, this.width, this.height, @@ -25,8 +26,8 @@ class VideoFileInfo extends FileInfo { Map get metadata => ({ 'mimetype': mimeType, 'size': fileSize, - 'w': width?.toDouble(), - 'h': height?.toDouble(), + 'w': width, + 'h': height, if (duration != null) 'duration': duration!.inMilliseconds, });