From 3164053f6ee4fe89d13fd292ce471e6b2e98005e Mon Sep 17 00:00:00 2001 From: MinhDV Date: Mon, 16 Oct 2023 17:00:00 +0700 Subject: [PATCH 1/4] TW-823 Metadata: width-height-thumbnail-blurhash for File Sharing --- .../extensions/file_info_extension.dart | 10 ++++ lib/di/global/get_it_initializer.dart | 2 - lib/domain/usecase/send_image_interactor.dart | 30 ---------- .../usecase/send_images_interactor.dart | 2 +- lib/pages/chat/chat.dart | 4 +- .../extensions/send_file_extension.dart | 55 +++++++++++++++++-- pubspec.lock | 4 +- pubspec.yaml | 2 +- 8 files changed, 67 insertions(+), 42 deletions(-) delete mode 100644 lib/domain/usecase/send_image_interactor.dart diff --git a/lib/data/network/extensions/file_info_extension.dart b/lib/data/network/extensions/file_info_extension.dart index 7ba5ae3005..967b34d80e 100644 --- a/lib/data/network/extensions/file_info_extension.dart +++ b/lib/data/network/extensions/file_info_extension.dart @@ -30,4 +30,14 @@ extension FileInfoExtension on FileInfo { } return MessageTypes.File; } + + FileInfo get detectFileType { + if (msgType == MessageTypes.Image) { + return ImageFileInfo(fileName, filePath, fileSize); + } + if (msgType == MessageTypes.Video) { + return VideoFileInfo(fileName, filePath, fileSize); + } + return FileInfo(fileName, filePath, fileSize, readStream: readStream); + } } diff --git a/lib/di/global/get_it_initializer.dart b/lib/di/global/get_it_initializer.dart index cc728b5864..21ab9c6ae1 100644 --- a/lib/di/global/get_it_initializer.dart +++ b/lib/di/global/get_it_initializer.dart @@ -39,7 +39,6 @@ import 'package:fluffychat/domain/usecase/search/pre_search_recent_contacts_inte import 'package:fluffychat/domain/usecase/search/search_recent_chat_interactor.dart'; import 'package:fluffychat/domain/usecase/send_file_interactor.dart'; import 'package:fluffychat/domain/usecase/send_file_on_web_interactor.dart'; -import 'package:fluffychat/domain/usecase/send_image_interactor.dart'; import 'package:fluffychat/domain/usecase/send_images_interactor.dart'; import 'package:fluffychat/domain/usecase/settings/update_profile_interactor.dart'; import 'package:fluffychat/event/twake_event_dispatcher.dart'; @@ -141,7 +140,6 @@ class GetItInitializer { getIt.registerFactory( () => GetContactsInteractor(), ); - getIt.registerSingleton(SendImageInteractor()); getIt.registerSingleton(SendImagesInteractor()); getIt.registerSingleton( DownloadFileForPreviewInteractor(), diff --git a/lib/domain/usecase/send_image_interactor.dart b/lib/domain/usecase/send_image_interactor.dart deleted file mode 100644 index e45b7b7226..0000000000 --- a/lib/domain/usecase/send_image_interactor.dart +++ /dev/null @@ -1,30 +0,0 @@ -import 'package:fluffychat/presentation/extensions/send_file_extension.dart'; -import 'package:fluffychat/presentation/model/file/file_asset_entity.dart'; -import 'package:matrix/matrix.dart'; - -class SendImageInteractor { - Future execute({ - required Room room, - required FileAssetEntity entity, - String? txId, - Event? inReplyTo, - String? editEventId, - int? shrinkImageMaxDimension, - Map? extraContent, - }) async { - final fileInfo = await entity.toFileInfo(); - if (fileInfo != null) { - try { - await room.sendFileEvent( - fileInfo, - txid: txId, - editEventId: editEventId, - inReplyTo: inReplyTo, - shrinkImageMaxDimension: shrinkImageMaxDimension, - ); - } catch (error) { - Logs().d("SendImageInteractor: execute(): $error"); - } - } - } -} diff --git a/lib/domain/usecase/send_images_interactor.dart b/lib/domain/usecase/send_images_interactor.dart index ee6c951262..fa8c7a4f3a 100644 --- a/lib/domain/usecase/send_images_interactor.dart +++ b/lib/domain/usecase/send_images_interactor.dart @@ -27,7 +27,7 @@ class SendImagesInteractor { ); } } catch (error) { - Logs().d("SendImageInteractor: execute(): $error"); + Logs().d("SendImagesInteractor: execute(): $error"); } } } diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 35ac70ba0b..724119b60e 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -6,6 +6,7 @@ import 'package:debounce_throttle/debounce_throttle.dart'; import 'package:desktop_drop/desktop_drop.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:emoji_picker_flutter/emoji_picker_flutter.dart'; +import 'package:fluffychat/data/network/extensions/file_info_extension.dart'; import 'package:fluffychat/di/global/get_it_initializer.dart'; import 'package:fluffychat/domain/app_state/preview_file/download_file_for_preview_failure.dart'; import 'package:fluffychat/domain/app_state/preview_file/download_file_for_preview_loading.dart'; @@ -321,7 +322,8 @@ class ChatController extends State sendFileInteractor.execute( room: room!, fileInfos: [ - FileInfo(shareFile!.name, shareFile!.filePath!, shareFile!.size), + FileInfo(shareFile!.name, shareFile!.filePath!, shareFile!.size) + .detectFileType, ], ); } diff --git a/lib/presentation/extensions/send_file_extension.dart b/lib/presentation/extensions/send_file_extension.dart index 4ea0ecf162..ef61669ecd 100644 --- a/lib/presentation/extensions/send_file_extension.dart +++ b/lib/presentation/extensions/send_file_extension.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:collection/collection.dart'; @@ -7,12 +8,14 @@ import 'package:fluffychat/di/global/get_it_initializer.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'; +import 'package:flutter/widgets.dart'; import 'package:image/image.dart' as img; import 'package:blurhash_dart/blurhash_dart.dart'; import 'package:flutter/foundation.dart'; import 'package:matrix/matrix.dart'; import 'package:path_provider/path_provider.dart'; import 'package:flutter_image_compress/flutter_image_compress.dart'; +import 'package:video_thumbnail/video_thumbnail.dart'; typedef TransactionId = String; @@ -90,7 +93,9 @@ extension SendFileExtension on Room { targetPath: tempThumbnailFile.path, ); - if (thumbnail != null && fileInfo.fileSize < thumbnail.fileSize) { + if (thumbnail != null && + fileInfo.fileSize > 0 && + fileInfo.fileSize < thumbnail.fileSize) { thumbnail = null; // in this case, the thumbnail is not usefull } } else if (fileInfo is VideoFileInfo) { @@ -211,6 +216,7 @@ extension SendFileExtension on Room { 'url': uploadResp.toString(), if (encryptedFileInfo != null) 'file': encryptedFileInfo.toJson(), 'info': { + ...thumbnail?.metadata ?? {}, ...fileInfo.metadata, if (thumbnail != null && encryptedThumbnail == null) 'thumbnail_url': thumbnailUploadResp.toString(), @@ -351,15 +357,26 @@ extension SendFileExtension on Room { originalFile.filePath, targetPath, quality: AppConfig.thumbnailQuality, + format: CompressFormat.jpeg, ); if (result == null) return null; final size = await result.length(); + var width = originalFile.width; + var height = originalFile.height; + if (width == null || height == null) { + final imageDimension = await runBenchmarked( + '_calculateImageDimension', + () => _calculateImageDimension(result.path), + ); + width = imageDimension.width.toInt(); + height = imageDimension.height.toInt(); + } return ImageFileInfo( result.name, result.path, size, - width: originalFile.width, - height: originalFile.height, + width: width, + height: height, ); } catch (e) { Logs().e('Error while generating thumbnail', e); @@ -392,12 +409,24 @@ extension SendFileExtension on Room { File tempThumbnailFile, VideoFileInfo fileInfo, ) async { - await tempThumbnailFile.writeAsBytes(fileInfo.imagePlaceholderBytes); + final int fileSize; + if (fileInfo.imagePlaceholderBytes != null) { + await tempThumbnailFile.writeAsBytes(fileInfo.imagePlaceholderBytes!); + fileSize = fileInfo.imagePlaceholderBytes!.lengthInBytes; + } else { + await VideoThumbnail.thumbnailFile( + video: fileInfo.filePath, + imageFormat: ImageFormat.JPEG, + quality: AppConfig.thumbnailQuality, + thumbnailPath: tempThumbnailFile.path, + ); + fileSize = await tempThumbnailFile.length(); + } Logs().d('Video thumbnail generated', tempThumbnailFile.path); final newThumbnail = ImageFileInfo( tempThumbnailFile.path.split("/").last, tempThumbnailFile.path, - fileInfo.imagePlaceholderBytes.lengthInBytes, + fileSize, ); return newThumbnail; } @@ -411,4 +440,20 @@ extension SendFileExtension on Room { .unsigned![key] = value; await handleImageFakeSync(fakeImageEvent); } + + Future _calculateImageDimension(String filePath) { + final completer = Completer(); + final Image image = Image.file(File(filePath)); + 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; + } } diff --git a/pubspec.lock b/pubspec.lock index 060e6c41aa..d991fe40e3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1550,8 +1550,8 @@ packages: dependency: "direct main" description: path: "." - ref: "twake-supported-0.22.4" - resolved-ref: "7b85a14ebb663ecc25bf55fbb7ceec3a0663515d" + ref: "feat/file-info-from-mime-type" + resolved-ref: "47cc0a78152ed1c8e429bbb752bd084781737bd4" url: "git@github.com:linagora/matrix-dart-sdk.git" source: git version: "0.22.4" diff --git a/pubspec.yaml b/pubspec.yaml index be77f5a1fa..9d11a428b8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -68,7 +68,7 @@ dependencies: matrix: git: url: git@github.com:linagora/matrix-dart-sdk.git - ref: twake-supported-0.22.4 + ref: feat/file-info-from-mime-type matrix_homeserver_recommendations: ^0.3.0 matrix_link_text: ^2.0.0 native_imaging: ^0.1.0 From 4ed5210278cfd8b8592ed2432441f296b37b1914 Mon Sep 17 00:00:00 2001 From: sherlock Date: Tue, 17 Oct 2023 17:15:25 +0700 Subject: [PATCH 2/4] TW-823: share image with resolution --- .../extensions/send_file_extension.dart | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/presentation/extensions/send_file_extension.dart b/lib/presentation/extensions/send_file_extension.dart index ef61669ecd..2ba7400960 100644 --- a/lib/presentation/extensions/send_file_extension.dart +++ b/lib/presentation/extensions/send_file_extension.dart @@ -92,7 +92,26 @@ extension SendFileExtension on Room { fileInfo, targetPath: tempThumbnailFile.path, ); - + fileInfo = ImageFileInfo( + fileInfo.fileName, + fileInfo.filePath, + fileInfo.fileSize, + 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, + ); if (thumbnail != null && fileInfo.fileSize > 0 && fileInfo.fileSize < thumbnail.fileSize) { From 8b5e7addac93807ee84b1e956e832a2795633483 Mon Sep 17 00:00:00 2001 From: sherlock Date: Tue, 17 Oct 2023 17:16:28 +0700 Subject: [PATCH 3/4] TW-823: refactor share file flow with video and image --- .../extensions/file_info_extension.dart | 10 ------ lib/domain/model/room/room_extension.dart | 12 +++++++ lib/domain/usecase/send_file_interactor.dart | 18 ++-------- lib/pages/chat/chat.dart | 6 ++-- lib/pages/chat/events/message_content.dart | 15 ++++---- .../receive_sharing_intent_mixin.dart | 11 ++---- .../extensions/image_extension.dart | 20 +++++++++++ .../extensions/send_file_extension.dart | 20 ++++------- .../shared_media_file_extension.dart | 34 +++++++++++++++++++ pubspec.lock | 4 +-- pubspec.yaml | 2 +- 11 files changed, 91 insertions(+), 61 deletions(-) create mode 100644 lib/presentation/extensions/image_extension.dart create mode 100644 lib/presentation/extensions/shared_media_file_extension.dart diff --git a/lib/data/network/extensions/file_info_extension.dart b/lib/data/network/extensions/file_info_extension.dart index 967b34d80e..7ba5ae3005 100644 --- a/lib/data/network/extensions/file_info_extension.dart +++ b/lib/data/network/extensions/file_info_extension.dart @@ -30,14 +30,4 @@ extension FileInfoExtension on FileInfo { } return MessageTypes.File; } - - FileInfo get detectFileType { - if (msgType == MessageTypes.Image) { - return ImageFileInfo(fileName, filePath, fileSize); - } - if (msgType == MessageTypes.Video) { - return VideoFileInfo(fileName, filePath, fileSize); - } - return FileInfo(fileName, filePath, fileSize, readStream: readStream); - } } diff --git a/lib/domain/model/room/room_extension.dart b/lib/domain/model/room/room_extension.dart index b70f7ef3b5..313b66b4c8 100644 --- a/lib/domain/model/room/room_extension.dart +++ b/lib/domain/model/room/room_extension.dart @@ -45,4 +45,16 @@ extension RoomExtension on Room { Future unmute() async { await setPushRuleState(PushRuleState.notify); } + + String storePlaceholderFileInMem({ + required FileInfo fileInfo, + String? txid, + }) { + txid ??= client.generateUniqueTransactionId(); + final matrixFile = MatrixFile.fromFileInfo( + fileInfo: fileInfo, + ); + sendingFilePlaceholders[txid] = matrixFile; + return txid; + } } diff --git a/lib/domain/usecase/send_file_interactor.dart b/lib/domain/usecase/send_file_interactor.dart index 1c4006fedc..ac0291c0d6 100644 --- a/lib/domain/usecase/send_file_interactor.dart +++ b/lib/domain/usecase/send_file_interactor.dart @@ -1,4 +1,5 @@ import 'package:fluffychat/data/network/extensions/file_info_extension.dart'; +import 'package:fluffychat/domain/model/room/room_extension.dart'; import 'package:fluffychat/presentation/extensions/send_file_extension.dart'; import 'package:matrix/matrix.dart'; @@ -14,9 +15,8 @@ class SendFileInteractor { }) async { try { for (final fileInfo in fileInfos) { - final txid = _storePlaceholderFileInMem( + final txid = room.storePlaceholderFileInMem( fileInfo: fileInfo, - room: room, ); await room.sendFileEvent( fileInfo, @@ -31,18 +31,4 @@ class SendFileInteractor { Logs().d("SendFileInteractor: execute(): $error"); } } - - String _storePlaceholderFileInMem({ - required Room room, - required FileInfo fileInfo, - }) { - final txid = room.client.generateUniqueTransactionId(); - final matrixFile = MatrixFile.fromMimeType( - name: fileInfo.fileName, - filePath: fileInfo.filePath, - mimeType: fileInfo.mimeType, - ); - room.sendingFilePlaceholders[txid] = matrixFile; - return txid; - } } diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index 724119b60e..44990e6fd0 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -6,7 +6,6 @@ import 'package:debounce_throttle/debounce_throttle.dart'; import 'package:desktop_drop/desktop_drop.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:emoji_picker_flutter/emoji_picker_flutter.dart'; -import 'package:fluffychat/data/network/extensions/file_info_extension.dart'; import 'package:fluffychat/di/global/get_it_initializer.dart'; import 'package:fluffychat/domain/app_state/preview_file/download_file_for_preview_failure.dart'; import 'package:fluffychat/domain/app_state/preview_file/download_file_for_preview_loading.dart'; @@ -322,8 +321,9 @@ class ChatController extends State sendFileInteractor.execute( room: room!, fileInfos: [ - FileInfo(shareFile!.name, shareFile!.filePath!, shareFile!.size) - .detectFileType, + FileInfo.fromMatrixFile( + shareFile!, + ) ], ); } diff --git a/lib/pages/chat/events/message_content.dart b/lib/pages/chat/events/message_content.dart index be324278d3..af87bec991 100644 --- a/lib/pages/chat/events/message_content.dart +++ b/lib/pages/chat/events/message_content.dart @@ -316,6 +316,7 @@ class _MessageImageBuilder extends StatelessWidget { file.height?.toDouble() ?? MessageContentStyle.imageHeight(context), ).getDisplayImageInfo(context); return SendingImageInfoWidget( + key: ValueKey(event.eventId), matrixFile: file, event: event, onTapPreview: onTapPreview, @@ -387,10 +388,12 @@ class _MessageVideoBuilder extends StatelessWidget { } if (isSendingVideo(matrixFile)) { final file = matrixFile as MatrixVideoFile; - displayImageInfo = Size( - file.width!.toDouble(), - file.height!.toDouble(), - ).getDisplayImageInfo(context); + if (file.width != null && file.height != null) { + displayImageInfo = Size( + file.width!.toDouble(), + file.height!.toDouble(), + ).getDisplayImageInfo(context); + } return SendingVideoWidget( key: ValueKey(event.eventId), event: event, @@ -413,8 +416,6 @@ class _MessageVideoBuilder extends StatelessWidget { } bool isSendingVideo(MatrixFile? matrixFile) { - return matrixFile is MatrixVideoFile && - matrixFile.width != null && - matrixFile.height != null; + return matrixFile is MatrixVideoFile && matrixFile.bytes != null; } } diff --git a/lib/pages/chat_list/receive_sharing_intent_mixin.dart b/lib/pages/chat_list/receive_sharing_intent_mixin.dart index a5bb6a734a..4eaa44b961 100644 --- a/lib/pages/chat_list/receive_sharing_intent_mixin.dart +++ b/lib/pages/chat_list/receive_sharing_intent_mixin.dart @@ -1,15 +1,13 @@ import 'package:fluffychat/event/twake_event_types.dart'; -import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart'; +import 'package:fluffychat/presentation/extensions/shared_media_file_extension.dart'; import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/url_launcher.dart'; import 'package:fluffychat/widgets/twake_app.dart'; import 'package:fluffychat/widgets/matrix.dart'; import 'package:flutter/material.dart'; import 'dart:async'; -import 'dart:io'; import 'package:fluffychat/config/app_config.dart'; -import 'package:matrix/matrix.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:uni_links/uni_links.dart'; @@ -25,14 +23,9 @@ mixin ReceiveSharingIntentMixin on State { void _processIncomingSharedFiles(List files) { if (files.isEmpty) return; final shareFile = files.first; - final path = Uri.decodeFull(shareFile.path.replaceFirst('file://', '')); - final file = File(path); matrixState.shareContent = { 'msgtype': TwakeEventTypes.shareFileEventType, - 'file': MatrixFile( - name: file.path, - filePath: file.path, - ).detectFileType, + 'file': shareFile.toMatrixFile(), }; TwakeApp.router.go('/share'); } diff --git a/lib/presentation/extensions/image_extension.dart b/lib/presentation/extensions/image_extension.dart new file mode 100644 index 0000000000..b3f55d992e --- /dev/null +++ b/lib/presentation/extensions/image_extension.dart @@ -0,0 +1,20 @@ +import 'dart:async'; + +import 'package:flutter/widgets.dart'; + +extension ImageExtension on Image { + Future calculateImageDimension() { + final completer = Completer(); + 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; + } +} diff --git a/lib/presentation/extensions/send_file_extension.dart b/lib/presentation/extensions/send_file_extension.dart index 2ba7400960..547fa98dc5 100644 --- a/lib/presentation/extensions/send_file_extension.dart +++ b/lib/presentation/extensions/send_file_extension.dart @@ -5,6 +5,8 @@ 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/extensions/image_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'; @@ -461,18 +463,10 @@ extension SendFileExtension on Room { } Future _calculateImageDimension(String filePath) { - final completer = Completer(); - final Image image = Image.file(File(filePath)); - 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; + return Image.file(File(filePath)).calculateImageDimension(); + } + + Future _calculateImageBytesDimension(Uint8List bytes) { + return Image.memory(bytes).calculateImageDimension(); } } diff --git a/lib/presentation/extensions/shared_media_file_extension.dart b/lib/presentation/extensions/shared_media_file_extension.dart new file mode 100644 index 0000000000..3f064277be --- /dev/null +++ b/lib/presentation/extensions/shared_media_file_extension.dart @@ -0,0 +1,34 @@ +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:matrix/matrix.dart'; +import 'package:receive_sharing_intent/receive_sharing_intent.dart'; + +extension SharedMediaFileExtension on SharedMediaFile { + MatrixFile toMatrixFile() { + if (type == SharedMediaType.IMAGE) { + return MatrixImageFile( + bytes: null, + name: path.split("/").last, + filePath: path, + ); + } + if (type == SharedMediaType.VIDEO) { + Uint8List? thumbnailBytes; + if (thumbnail != null) { + thumbnailBytes = File(thumbnail!).readAsBytesSync(); + } + return MatrixVideoFile( + bytes: thumbnailBytes, + name: path.split("/").last, + filePath: path, + duration: duration, + ); + } + return MatrixFile( + bytes: null, + name: path.split("/").last, + filePath: path, + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index d991fe40e3..e8cc9af1a9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1550,8 +1550,8 @@ packages: dependency: "direct main" description: path: "." - ref: "feat/file-info-from-mime-type" - resolved-ref: "47cc0a78152ed1c8e429bbb752bd084781737bd4" + ref: "twake-supported-0.22.4" + resolved-ref: d3fd4dbeb9ecfbee641753cd6267e27de9fe91f2 url: "git@github.com:linagora/matrix-dart-sdk.git" source: git version: "0.22.4" diff --git a/pubspec.yaml b/pubspec.yaml index 9d11a428b8..be77f5a1fa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -68,7 +68,7 @@ dependencies: matrix: git: url: git@github.com:linagora/matrix-dart-sdk.git - ref: feat/file-info-from-mime-type + ref: twake-supported-0.22.4 matrix_homeserver_recommendations: ^0.3.0 matrix_link_text: ^2.0.0 native_imaging: ^0.1.0 From 2a2bbc00f1066a86082fa23670928cba9709a593 Mon Sep 17 00:00:00 2001 From: sherlock Date: Tue, 17 Oct 2023 17:16:59 +0700 Subject: [PATCH 4/4] TW-823: share video file with resolution --- .../extensions/send_file_extension.dart | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/presentation/extensions/send_file_extension.dart b/lib/presentation/extensions/send_file_extension.dart index 547fa98dc5..ab76829a21 100644 --- a/lib/presentation/extensions/send_file_extension.dart +++ b/lib/presentation/extensions/send_file_extension.dart @@ -114,6 +114,7 @@ extension SendFileExtension on Room { shrinkImageMaxDimension: shrinkImageMaxDimension, extraContent: extraContent, ); + if (thumbnail != null && fileInfo.fileSize > 0 && fileInfo.fileSize < thumbnail.fileSize) { @@ -126,6 +127,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; @@ -431,9 +455,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, @@ -443,11 +467,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; }