Fix E2EE files
This commit is contained in:
parent
1d3bfbafda
commit
cef6c20dc7
|
@ -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<AudioPlayer> {
|
|||
_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<AudioPlayer> {
|
|||
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());
|
||||
|
|
|
@ -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<MatrixFile>(
|
||||
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(
|
||||
"- " +
|
||||
|
|
15
lib/utils/matrix_file_extension.dart
Normal file
15
lib/utils/matrix_file_extension.dart
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue