Fix E2EE files

This commit is contained in:
Christian Pauly 2020-03-29 20:13:25 +02:00
parent 1d3bfbafda
commit cef6c20dc7
3 changed files with 53 additions and 61 deletions

View file

@ -12,11 +12,11 @@ import 'matrix.dart';
class AudioPlayer extends StatefulWidget { class AudioPlayer extends StatefulWidget {
final Color color; 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); : super(key: key);
@override @override
@ -51,26 +51,24 @@ class _AudioPlayerState extends State<AudioPlayer> {
_downloadAction() async { _downloadAction() async {
if (status != AudioPlayerStatus.NOT_DOWNLOADED) return; if (status != AudioPlayerStatus.NOT_DOWNLOADED) return;
setState(() => status = AudioPlayerStatus.DOWNLOADING); setState(() => status = AudioPlayerStatus.DOWNLOADING);
String url = widget.content.getDownloadLink(Matrix.of(context).client); final matrixFile = await Matrix.of(context)
var request = await httpClient.getUrl(Uri.parse(url)); .tryRequestWithErrorToast(widget.event.downloadAndDecryptAttachment());
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
setState(() { setState(() {
audioFile = bytes; audioFile = matrixFile.bytes;
status = AudioPlayerStatus.DOWNLOADED; status = AudioPlayerStatus.DOWNLOADED;
}); });
_playAction(); _playAction();
} }
_playAction() async { _playAction() async {
if (AudioPlayer.currentMxc != widget.content.mxc) { if (AudioPlayer.currentId != widget.event.eventId) {
if (AudioPlayer.currentMxc != null) { if (AudioPlayer.currentId != null) {
if (flutterSound.audioState != t_AUDIO_STATE.IS_STOPPED) { if (flutterSound.audioState != t_AUDIO_STATE.IS_STOPPED) {
await flutterSound.stopPlayer(); await flutterSound.stopPlayer();
setState(() => null); setState(() => null);
} }
} }
AudioPlayer.currentMxc = widget.content.mxc; AudioPlayer.currentId = widget.event.eventId;
} }
switch (flutterSound.audioState) { switch (flutterSound.audioState) {
case t_AUDIO_STATE.IS_PLAYING: case t_AUDIO_STATE.IS_PLAYING:
@ -87,13 +85,13 @@ class _AudioPlayerState extends State<AudioPlayer> {
codec: t_CODEC.CODEC_AAC, codec: t_CODEC.CODEC_AAC,
); );
soundSubscription ??= flutterSound.onPlayerStateChanged.listen((e) { soundSubscription ??= flutterSound.onPlayerStateChanged.listen((e) {
if (AudioPlayer.currentMxc != widget.content.mxc) { if (AudioPlayer.currentId != widget.event.eventId) {
soundSubscription?.cancel()?.then((f) => soundSubscription = null); soundSubscription?.cancel()?.then((f) => soundSubscription = null);
this.setState(() { this.setState(() {
currentPosition = 0; currentPosition = 0;
statusText = "00:00"; statusText = "00:00";
}); });
AudioPlayer.currentMxc = null; AudioPlayer.currentId = null;
} else if (e != null) { } else if (e != null) {
DateTime date = DateTime date =
DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt()); DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());

View file

@ -1,18 +1,13 @@
import 'dart:io';
import 'package:bubble/bubble.dart'; import 'package:bubble/bubble.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/audio_player.dart'; import 'package:fluffychat/components/audio_player.dart';
import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/event_extension.dart'; import 'package:fluffychat/utils/event_extension.dart';
import 'package:fluffychat/views/image_viewer.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:link_text/link_text.dart'; import 'package:link_text/link_text.dart';
import 'package:path_provider/path_provider.dart';
import 'package:url_launcher/url_launcher.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'; import 'matrix.dart';
@ -24,58 +19,47 @@ class MessageContent extends StatelessWidget {
@override @override
Widget build(BuildContext context) { 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) { switch (event.type) {
case EventTypes.Message: case EventTypes.Message:
case EventTypes.Encrypted: case EventTypes.Encrypted:
case EventTypes.Sticker: case EventTypes.Sticker:
switch (messageType) { switch (event.messageType) {
case MessageTypes.Image: case MessageTypes.Image:
case MessageTypes.Sticker: case MessageTypes.Sticker:
final int size = 400; 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( return Bubble(
padding: BubbleEdges.all(0), padding: BubbleEdges.all(0),
radius: Radius.circular(10), radius: Radius.circular(10),
elevation: 0, elevation: 0,
child: InkWell(
onTap: () => ImageViewer.show(context, content),
child: Container( child: Container(
height: size.toDouble(), height: size.toDouble(),
width: size.toDouble(), width: size.toDouble(),
child: kIsWeb child: FutureBuilder<MatrixFile>(
? Image.network( future: event.downloadAndDecryptAttachment(),
src, builder: (BuildContext context, snapshot) {
fit: BoxFit.cover, if (snapshot.hasError) {
) return Center(
: CachedNetworkImage( child: Text(
imageUrl: src, snapshot.error.toString(),
fit: BoxFit.cover, ),
placeholder: (c, s) => Center( );
}
if (snapshot.hasData) {
return InkWell(
onTap: () => snapshot.data.open(),
child: Image.memory(snapshot.data.bytes),
);
}
return Center(
child: CircularProgressIndicator(), child: CircularProgressIndicator(),
), );
), },
), ),
), ),
); );
case MessageTypes.Audio: case MessageTypes.Audio:
return AudioPlayer( return AudioPlayer(
MxContent(event.content["url"]), event,
color: textColor, color: textColor,
); );
case MessageTypes.Video: case MessageTypes.Video:
@ -110,16 +94,11 @@ class MessageContent extends StatelessWidget {
); );
return; return;
} }
final matrixFile = await Matrix.of(context) final MatrixFile matrixFile = await Matrix.of(context)
.tryRequestWithLoadingDialog( .tryRequestWithLoadingDialog(
event.downloadAndDecryptAttachment(), event.downloadAndDecryptAttachment(),
); );
Directory tempDir = await getTemporaryDirectory(); matrixFile.open();
final file = File(tempDir.path +
"/" +
matrixFile.path.split("/").last);
file.writeAsBytesSync(matrixFile.bytes);
await OpenFile.open(file.path);
}), }),
Text( Text(
"- " + "- " +

View file

@ -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;
}
}