diff --git a/lib/presentation/extensions/send_file_extension.dart b/lib/presentation/extensions/send_file_extension.dart index 2ba7400960..8b50ab20c4 100644 --- a/lib/presentation/extensions/send_file_extension.dart +++ b/lib/presentation/extensions/send_file_extension.dart @@ -5,6 +5,7 @@ import 'package:collection/collection.dart'; import 'package:fluffychat/data/network/media/media_api.dart'; import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/di/global/get_it_initializer.dart'; +import 'package:fluffychat/domain/model/room/room_extension.dart'; import 'package:fluffychat/presentation/fake_sending_file_info.dart'; import 'package:fluffychat/presentation/model/file/file_asset_entity.dart'; import 'package:fluffychat/utils/date_time_extension.dart'; @@ -112,6 +113,7 @@ extension SendFileExtension on Room { shrinkImageMaxDimension: shrinkImageMaxDimension, extraContent: extraContent, ); + if (thumbnail != null && fileInfo.fileSize > 0 && fileInfo.fileSize < thumbnail.fileSize) { @@ -124,6 +126,29 @@ extension SendFileExtension on Room { FileSendingStatus.generatingThumbnail.name, ); thumbnail ??= await _getThumbnailVideo(tempThumbnailFile, fileInfo); + if (fileInfo.width == null || fileInfo.height == null) { + fileInfo = VideoFileInfo( + fileInfo.fileName, + fileInfo.filePath, + fileInfo.fileSize, + imagePlaceholderBytes: fileInfo.imagePlaceholderBytes, + width: thumbnail.width, + height: thumbnail.height, + ); + storePlaceholderFileInMem( + fileInfo: fileInfo, + txid: txid, + ); + fakeImageEvent = await sendFakeImagePickerFileEvent( + fileInfo, + txid: txid, + messageType: msgType, + inReplyTo: inReplyTo, + editEventId: editEventId, + shrinkImageMaxDimension: shrinkImageMaxDimension, + extraContent: extraContent, + ); + } } EncryptedFileInfo? encryptedFileInfo; @@ -429,9 +454,9 @@ extension SendFileExtension on Room { VideoFileInfo fileInfo, ) async { final int fileSize; - if (fileInfo.imagePlaceholderBytes != null) { - await tempThumbnailFile.writeAsBytes(fileInfo.imagePlaceholderBytes!); - fileSize = fileInfo.imagePlaceholderBytes!.lengthInBytes; + if (fileInfo.imagePlaceholderBytes.isNotEmpty) { + await tempThumbnailFile.writeAsBytes(fileInfo.imagePlaceholderBytes); + fileSize = fileInfo.imagePlaceholderBytes.lengthInBytes; } else { await VideoThumbnail.thumbnailFile( video: fileInfo.filePath, @@ -441,11 +466,23 @@ extension SendFileExtension on Room { ); fileSize = await tempThumbnailFile.length(); } + var width = fileInfo.width; + var height = fileInfo.height; + if (width == null || height == null) { + final imageDimension = await runBenchmarked( + '_calculateImageDimension', + () => _calculateImageBytesDimension(fileInfo.imagePlaceholderBytes), + ); + width = imageDimension.width.toInt(); + height = imageDimension.height.toInt(); + } Logs().d('Video thumbnail generated', tempThumbnailFile.path); final newThumbnail = ImageFileInfo( tempThumbnailFile.path.split("/").last, tempThumbnailFile.path, fileSize, + width: width, + height: height, ); return newThumbnail; } @@ -475,4 +512,20 @@ extension SendFileExtension on Room { ); return completer.future; } + + Future _calculateImageBytesDimension(Uint8List bytes) { + final completer = Completer(); + final Image image = Image.memory(bytes); + image.image.resolve(const ImageConfiguration()).addListener( + ImageStreamListener( + (ImageInfo image, bool synchronousCall) { + final myImage = image.image; + final Size size = + Size(myImage.width.toDouble(), myImage.height.toDouble()); + completer.complete(size); + }, + ), + ); + return completer.future; + } }