diff --git a/lib/src/utils/matrix_file.dart b/lib/src/utils/matrix_file.dart index 97127b2f2..ffb17c164 100644 --- a/lib/src/utils/matrix_file.dart +++ b/lib/src/utils/matrix_file.dart @@ -68,6 +68,49 @@ class MatrixFile { return MatrixFile(bytes: bytes, name: name, mimeType: mimeType, filePath: filePath); } + factory MatrixFile.fromFileInfo( + {required FileInfo fileInfo}) { + final msgType = msgTypeFromMime(fileInfo.mimeType); + if (msgType == MessageTypes.Image) { + return MatrixImageFile( + name: fileInfo.fileName, + mimeType: fileInfo.mimeType, + filePath: fileInfo.filePath, + bytes: null, + width: (fileInfo.metadata['w'] as double?)?.toInt(), + height: (fileInfo.metadata['h'] as double?)?.toInt(), + ); + } + if (msgType == MessageTypes.Video) { + return MatrixVideoFile( + bytes: fileInfo is VideoFileInfo + ? fileInfo.imagePlaceholderBytes + : null, + name: fileInfo.fileName, + mimeType: fileInfo.mimeType, + filePath: fileInfo.filePath, + width: (fileInfo.metadata['w'] as double?)?.toInt(), + height: (fileInfo.metadata['h'] as double?)?.toInt(), + duration: fileInfo.metadata['duration'], + ); + } + if (msgType == MessageTypes.Audio) { + return MatrixAudioFile( + bytes: Uint8List.fromList([]), + name: fileInfo.fileName, + mimeType: fileInfo.mimeType, + filePath: fileInfo.filePath, + duration: fileInfo.metadata['duration'], + ); + } + return MatrixFile( + bytes: null, + name: fileInfo.fileName, + mimeType: fileInfo.mimeType, + filePath: fileInfo.filePath, + ); + } + int get size => bytes?.length ?? 0; String get msgType { diff --git a/lib/src/utils/models/file_info.dart b/lib/src/utils/models/file_info.dart index f5f1c0e6f..0d636fc9e 100644 --- a/lib/src/utils/models/file_info.dart +++ b/lib/src/utils/models/file_info.dart @@ -28,27 +28,31 @@ class FileInfo with EquatableMixin { }); - FileInfo fromMatrixFile(MatrixFile file) { + factory FileInfo.fromMatrixFile(MatrixFile file) { if (file.msgType == MessageTypes.Image) { return ImageFileInfo( - fileName, - filePath, - fileSize, + file.name, + file.filePath ?? '', + file.size, width: file.info['w'], height: file.info['h'], ); } else if (file.msgType == MessageTypes.Video) { return VideoFileInfo( - fileName, - filePath, - fileSize, + file.name, + file.filePath ?? '', + file.size, imagePlaceholderBytes: file.bytes ?? Uint8List(0), width: file.info['w'], height: file.info['h'], - duration: file.info['duration'], + duration: Duration(milliseconds: file.info['duration']), ); } - return FileInfo(fileName, filePath, fileSize, readStream: readStream); + return FileInfo( + file.name, + file.filePath ?? '', + file.size + ); } @override