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';
|
|
|
|
|
2021-12-21 18:29:20 +00:00
|
|
|
import '../../main.dart';
|
2022-01-18 21:26:52 +00:00
|
|
|
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
|
2022-04-29 23:00:52 +00:00
|
|
|
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) {
|
2022-12-07 23:02:53 +00:00
|
|
|
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;
|
|
|
|
|
2023-04-20 22:48:00 +00:00
|
|
|
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);
|
2023-04-17 17:36:45 +00:00
|
|
|
}
|
2022-12-07 23:02:53 +00:00
|
|
|
|
|
|
|
if (!validHash(rootHash, nonce)) {
|
|
|
|
return MessageRow(MalformedBubble(), index);
|
2021-12-21 18:29:20 +00:00
|
|
|
}
|
2021-09-21 21:57:40 +00:00
|
|
|
|
2022-12-07 23:02:53 +00:00
|
|
|
return MessageRow(FileBubble(nameSuggestion, rootHash, nonce, fileSize, isAuto: metadata.isAuto), index, key: key);
|
|
|
|
} catch (e) {
|
2022-04-29 23:00:52 +00:00
|
|
|
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) {
|
2024-02-08 22:10:43 +00:00
|
|
|
return MalformedBubble();
|
2021-09-21 21:57:40 +00:00
|
|
|
}
|
2022-12-30 04:27:25 +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)) {
|
2024-02-08 22:10:43 +00:00
|
|
|
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
|
|
|
}
|