Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tw 2001: No previews for video #2004

Merged
merged 8 commits into from
Sep 4, 2024
21 changes: 14 additions & 7 deletions lib/presentation/extensions/send_file_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,15 @@ extension SendFileExtension on Room {
}

final formattedDateTime = DateTime.now().getFormattedCurrentDateTime();
final targetPath =
await File('${tempDir.path}/$formattedDateTime${fileInfo.fileName}')
.create();
fileInfo = fileInfo as ImageFileInfo;
final fileName = _generateThumbnailFileName(formattedDateTime, fileInfo);
final targetPath = await _createThumbnailFile(tempDir, fileName);
await _generateThumbnail(
fileInfo,
fileInfo as ImageFileInfo,
targetPath: targetPath.path,
uploadStreamController: uploadStreamController,
);
thumbnail = ImageFileInfo(
fileInfo.fileName,
fileName,
targetPath.path,
await targetPath.length(),
width: fileInfo.width,
Expand Down Expand Up @@ -427,6 +425,15 @@ extension SendFileExtension on Room {
return eventId;
}

Future<File> _createThumbnailFile(Directory tempDir, String fileName) async =>
await File('${tempDir.path}/$fileName').create();

String _generateThumbnailFileName(
String formattedDateTime,
FileInfo fileInfo,
) =>
'$formattedDateTime${fileInfo.fileName}.${AppConfig.imageCompressFormmat.name}';

Future<ImageFileInfo> convertHeicToJpgImage(ImageFileInfo fileInfo) async {
final convertedFilePath =
StorageDirectoryManager.instance.convertFileExtension(
Expand Down Expand Up @@ -629,7 +636,7 @@ extension SendFileExtension on Room {
}
uploadStreamController?.add(const Right(GenerateThumbnailSuccess()));
return ImageFileInfo(
'${result.name}.${AppConfig.imageCompressFormmat.name}',
result.name,
result.path,
size,
width: width,
Expand Down
13 changes: 9 additions & 4 deletions lib/presentation/extensions/send_file_web_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dar
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:matrix/matrix.dart';
import 'package:image/image.dart';
import 'package:mime/mime.dart';
import 'package:video_player/video_player.dart';
import 'package:video_thumbnail/video_thumbnail.dart';

Expand Down Expand Up @@ -169,7 +170,7 @@ extension SendFileWebExtension on Room {
: null;
if (uploadThumbnail != null && uploadThumbnail.bytes != null) {
final uploadThumbnailResponse = await mediaApi.uploadFileWeb(
file: file,
file: uploadThumbnail,
cancelToken: cancelToken,
onSendProgress: (receive, total) {
uploadStreamController?.add(
Expand Down Expand Up @@ -442,11 +443,12 @@ extension SendFileWebExtension on Room {
const Right(GenerateThumbnailSuccess()),
);

final thumbnailFileName = _getVideoThumbnailFileName(originalFile);

return MatrixImageFile(
bytes: result,
name:
'${originalFile.name}.${AppConfig.videoThumbnailFormat.name.toLowerCase()}',
mimeType: originalFile.mimeType,
name: thumbnailFileName,
mimeType: lookupMimeType(thumbnailFileName) ?? 'image/jpeg',
width: thumbnailBitmap?.width,
height: thumbnailBitmap?.height,
blurhash: blurHash,
Expand All @@ -464,6 +466,9 @@ extension SendFileWebExtension on Room {
}
}

String _getVideoThumbnailFileName(MatrixVideoFile originalFile) =>
'${originalFile.name}.${AppConfig.videoThumbnailFormat.name.toLowerCase()}';

Future<int?> _getVideoDuration(
MatrixVideoFile originalFile,
) async {
Expand Down