cwtch-ui/lib/models/messages/filemessage.dart

94 lines
3.2 KiB
Dart
Raw Permalink Normal View History

2021-09-21 21:57:40 +00:00
import 'dart:convert';
import 'package:cwtch/models/message.dart';
import 'package:cwtch/widgets/filebubble.dart';
import 'package:cwtch/widgets/malformedbubble.dart';
import 'package:cwtch/widgets/messagerow.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import '../../main.dart';
import '../profile.dart';
2021-09-21 21:57:40 +00:00
class FileMessage extends Message {
final MessageMetadata metadata;
final String content;
2021-11-04 22:31:50 +00:00
final RegExp nonHex = RegExp(r'[^a-f0-9]');
2021-09-21 21:57:40 +00:00
FileMessage(this.metadata, this.content);
@override
Widget getWidget(BuildContext context, Key key, int index) {
2021-09-21 21:57:40 +00:00
return ChangeNotifierProvider.value(
value: this.metadata,
builder: (bcontext, child) {
try {
dynamic shareObj = jsonDecode(this.content);
if (shareObj == null) {
return MessageRow(MalformedBubble(), index);
}
String nameSuggestion = shareObj['f'] as String;
String rootHash = shareObj['h'] as String;
String nonce = shareObj['n'] as String;
int fileSize = shareObj['s'] as int;
String fileKey = rootHash + "." + nonce;
if (!Provider.of<ProfileInfoState>(context, listen: false).downloadKnown(fileKey)) {
Provider.of<FlwtchState>(context, listen: false).cwtch.CheckDownloadStatus(Provider.of<ProfileInfoState>(context, listen: false).onion, fileKey);
}
if (!validHash(rootHash, nonce)) {
return MessageRow(MalformedBubble(), index);
}
2021-09-21 21:57:40 +00:00
return MessageRow(FileBubble(nameSuggestion, rootHash, nonce, fileSize, isAuto: metadata.isAuto), index, key: key);
} catch (e) {
return MessageRow(MalformedBubble(), index);
2021-11-04 22:31:50 +00:00
}
2021-09-21 21:57:40 +00:00
});
}
@override
2024-02-08 21:51:49 +00:00
Widget getPreviewWidget(BuildContext context, {BoxConstraints? constraints}) {
2021-09-21 21:57:40 +00:00
return ChangeNotifierProvider.value(
value: this.metadata,
builder: (bcontext, child) {
dynamic shareObj = jsonDecode(this.content);
if (shareObj == null) {
return MalformedBubble();
2021-09-21 21:57:40 +00:00
}
String nameSuggestion = shareObj['f'] as String;
2021-09-21 21:57:40 +00:00
String rootHash = shareObj['h'] as String;
String nonce = shareObj['n'] as String;
2021-09-30 00:20:35 +00:00
int fileSize = shareObj['s'] as int;
2021-11-04 22:31:50 +00:00
if (!validHash(rootHash, nonce)) {
return MalformedBubble();
2021-11-04 22:31:50 +00:00
}
2021-12-18 00:54:30 +00:00
return Container(
2024-02-09 23:25:39 +00:00
padding: EdgeInsets.all(1.0),
decoration: BoxDecoration(),
clipBehavior: Clip.antiAliasWithSaveLayer,
2024-02-09 23:54:19 +00:00
constraints: BoxConstraints(minHeight: 50, maxHeight: 50, minWidth: 50, maxWidth: 300),
alignment: Alignment.centerLeft,
2021-12-18 00:54:30 +00:00
child: FileBubble(
nameSuggestion,
rootHash,
nonce,
fileSize,
isAuto: metadata.isAuto,
interactive: false,
2022-06-13 17:06:06 +00:00
isPreview: true,
2021-12-18 00:54:30 +00:00
));
2021-09-21 21:57:40 +00:00
});
}
@override
MessageMetadata getMetadata() {
return this.metadata;
}
2021-11-04 22:31:50 +00:00
bool validHash(String hash, String nonce) {
return hash.length == 128 && nonce.length == 48 && !hash.contains(nonHex) && !nonce.contains(nonHex);
}
2021-09-21 21:57:40 +00:00
}