Skip to content

Commit

Permalink
TW-823: share video file with resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
sherlockvn committed Oct 17, 2023
1 parent 52c1ed8 commit cca6c76
Showing 1 changed file with 56 additions and 3 deletions.
59 changes: 56 additions & 3 deletions lib/presentation/extensions/send_file_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -112,6 +113,7 @@ extension SendFileExtension on Room {
shrinkImageMaxDimension: shrinkImageMaxDimension,
extraContent: extraContent,
);

if (thumbnail != null &&
fileInfo.fileSize > 0 &&
fileInfo.fileSize < thumbnail.fileSize) {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -475,4 +512,20 @@ extension SendFileExtension on Room {
);
return completer.future;
}

Future<Size> _calculateImageBytesDimension(Uint8List bytes) {
final completer = Completer<Size>();
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;
}
}

0 comments on commit cca6c76

Please sign in to comment.