From cef6c20dc703f69bd9ccb7af0b5ec16bdb81da8b Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sun, 29 Mar 2020 20:13:25 +0200 Subject: [PATCH] Fix E2EE files --- lib/components/audio_player.dart | 24 ++++----- lib/components/message_content.dart | 75 ++++++++++------------------ lib/utils/matrix_file_extension.dart | 15 ++++++ 3 files changed, 53 insertions(+), 61 deletions(-) create mode 100644 lib/utils/matrix_file_extension.dart diff --git a/lib/components/audio_player.dart b/lib/components/audio_player.dart index e867083..48f50a3 100644 --- a/lib/components/audio_player.dart +++ b/lib/components/audio_player.dart @@ -12,11 +12,11 @@ import 'matrix.dart'; class AudioPlayer extends StatefulWidget { final Color color; - final MxContent content; + final Event event; - static String currentMxc; + static String currentId; - const AudioPlayer(this.content, {this.color = Colors.black, Key key}) + const AudioPlayer(this.event, {this.color = Colors.black, Key key}) : super(key: key); @override @@ -51,26 +51,24 @@ class _AudioPlayerState extends State { _downloadAction() async { if (status != AudioPlayerStatus.NOT_DOWNLOADED) return; setState(() => status = AudioPlayerStatus.DOWNLOADING); - String url = widget.content.getDownloadLink(Matrix.of(context).client); - var request = await httpClient.getUrl(Uri.parse(url)); - var response = await request.close(); - var bytes = await consolidateHttpClientResponseBytes(response); + final matrixFile = await Matrix.of(context) + .tryRequestWithErrorToast(widget.event.downloadAndDecryptAttachment()); setState(() { - audioFile = bytes; + audioFile = matrixFile.bytes; status = AudioPlayerStatus.DOWNLOADED; }); _playAction(); } _playAction() async { - if (AudioPlayer.currentMxc != widget.content.mxc) { - if (AudioPlayer.currentMxc != null) { + if (AudioPlayer.currentId != widget.event.eventId) { + if (AudioPlayer.currentId != null) { if (flutterSound.audioState != t_AUDIO_STATE.IS_STOPPED) { await flutterSound.stopPlayer(); setState(() => null); } } - AudioPlayer.currentMxc = widget.content.mxc; + AudioPlayer.currentId = widget.event.eventId; } switch (flutterSound.audioState) { case t_AUDIO_STATE.IS_PLAYING: @@ -87,13 +85,13 @@ class _AudioPlayerState extends State { codec: t_CODEC.CODEC_AAC, ); soundSubscription ??= flutterSound.onPlayerStateChanged.listen((e) { - if (AudioPlayer.currentMxc != widget.content.mxc) { + if (AudioPlayer.currentId != widget.event.eventId) { soundSubscription?.cancel()?.then((f) => soundSubscription = null); this.setState(() { currentPosition = 0; statusText = "00:00"; }); - AudioPlayer.currentMxc = null; + AudioPlayer.currentId = null; } else if (e != null) { DateTime date = DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt()); diff --git a/lib/components/message_content.dart b/lib/components/message_content.dart index 18f735c..8d20bf5 100644 --- a/lib/components/message_content.dart +++ b/lib/components/message_content.dart @@ -1,18 +1,13 @@ -import 'dart:io'; - import 'package:bubble/bubble.dart'; -import 'package:cached_network_image/cached_network_image.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/components/audio_player.dart'; import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/utils/event_extension.dart'; -import 'package:fluffychat/views/image_viewer.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:link_text/link_text.dart'; -import 'package:path_provider/path_provider.dart'; import 'package:url_launcher/url_launcher.dart'; -import 'package:open_file/open_file.dart'; +import 'package:fluffychat/utils/matrix_file_extension.dart'; import 'matrix.dart'; @@ -24,58 +19,47 @@ class MessageContent extends StatelessWidget { @override Widget build(BuildContext context) { - var messageType = event.messageType; - if (event.room.encrypted && - [ - MessageTypes.Image, - MessageTypes.Sticker, - MessageTypes.Audio, - MessageTypes.Video, - ].contains(messageType)) { - messageType = MessageTypes.File; - } switch (event.type) { case EventTypes.Message: case EventTypes.Encrypted: case EventTypes.Sticker: - switch (messageType) { + switch (event.messageType) { case MessageTypes.Image: case MessageTypes.Sticker: final int size = 400; - final MxContent content = MxContent(event.content["url"]); - final String src = content.getThumbnail( - Matrix.of(context).client, - width: size * MediaQuery.of(context).devicePixelRatio, - height: size * MediaQuery.of(context).devicePixelRatio, - method: ThumbnailMethod.scale, - ); return Bubble( padding: BubbleEdges.all(0), radius: Radius.circular(10), elevation: 0, - child: InkWell( - onTap: () => ImageViewer.show(context, content), - child: Container( - height: size.toDouble(), - width: size.toDouble(), - child: kIsWeb - ? Image.network( - src, - fit: BoxFit.cover, - ) - : CachedNetworkImage( - imageUrl: src, - fit: BoxFit.cover, - placeholder: (c, s) => Center( - child: CircularProgressIndicator(), - ), + child: Container( + height: size.toDouble(), + width: size.toDouble(), + child: FutureBuilder( + future: event.downloadAndDecryptAttachment(), + builder: (BuildContext context, snapshot) { + if (snapshot.hasError) { + return Center( + child: Text( + snapshot.error.toString(), ), + ); + } + if (snapshot.hasData) { + return InkWell( + onTap: () => snapshot.data.open(), + child: Image.memory(snapshot.data.bytes), + ); + } + return Center( + child: CircularProgressIndicator(), + ); + }, ), ), ); case MessageTypes.Audio: return AudioPlayer( - MxContent(event.content["url"]), + event, color: textColor, ); case MessageTypes.Video: @@ -110,16 +94,11 @@ class MessageContent extends StatelessWidget { ); return; } - final matrixFile = await Matrix.of(context) + final MatrixFile matrixFile = await Matrix.of(context) .tryRequestWithLoadingDialog( event.downloadAndDecryptAttachment(), ); - Directory tempDir = await getTemporaryDirectory(); - final file = File(tempDir.path + - "/" + - matrixFile.path.split("/").last); - file.writeAsBytesSync(matrixFile.bytes); - await OpenFile.open(file.path); + matrixFile.open(); }), Text( "- " + diff --git a/lib/utils/matrix_file_extension.dart b/lib/utils/matrix_file_extension.dart new file mode 100644 index 0000000..29927e2 --- /dev/null +++ b/lib/utils/matrix_file_extension.dart @@ -0,0 +1,15 @@ +import 'dart:io'; + +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:open_file/open_file.dart'; +import 'package:path_provider/path_provider.dart'; + +extension MatrixFileExtension on MatrixFile { + void open() async { + Directory tempDir = await getTemporaryDirectory(); + final file = File(tempDir.path + "/" + path.split("/").last); + file.writeAsBytesSync(bytes); + await OpenFile.open(file.path); + return; + } +}