Line data Source code
1 : import 'dart:convert';
2 :
3 : import 'package:cwtch/models/message.dart';
4 : import 'package:cwtch/widgets/filebubble.dart';
5 : import 'package:cwtch/widgets/malformedbubble.dart';
6 : import 'package:cwtch/widgets/messagerow.dart';
7 : import 'package:flutter/widgets.dart';
8 : import 'package:provider/provider.dart';
9 :
10 : import '../../main.dart';
11 : import '../profile.dart';
12 :
13 : class FileMessage extends Message {
14 : final MessageMetadata metadata;
15 : final String content;
16 : final RegExp nonHex = RegExp(r'[^a-f0-9]');
17 :
18 0 : FileMessage(this.metadata, this.content);
19 :
20 0 : @override
21 : Widget getWidget(BuildContext context, Key key, int index) {
22 0 : return ChangeNotifierProvider.value(
23 0 : value: this.metadata,
24 0 : builder: (bcontext, child) {
25 : try {
26 0 : dynamic shareObj = jsonDecode(this.content);
27 : if (shareObj == null) {
28 0 : return MessageRow(MalformedBubble(), index);
29 : }
30 0 : String nameSuggestion = shareObj['f'] as String;
31 0 : String rootHash = shareObj['h'] as String;
32 0 : String nonce = shareObj['n'] as String;
33 0 : int fileSize = shareObj['s'] as int;
34 0 : String fileKey = rootHash + "." + nonce;
35 :
36 0 : if (!Provider.of<ProfileInfoState>(context, listen: false).downloadKnown(fileKey)) {
37 0 : Provider.of<FlwtchState>(context, listen: false).cwtch.CheckDownloadStatus(Provider.of<ProfileInfoState>(context, listen: false).onion, fileKey);
38 : }
39 :
40 0 : if (!validHash(rootHash, nonce)) {
41 0 : return MessageRow(MalformedBubble(), index);
42 : }
43 :
44 0 : return MessageRow(FileBubble(nameSuggestion, rootHash, nonce, fileSize, isAuto: metadata.isAuto), index, key: key);
45 : } catch (e) {
46 0 : return MessageRow(MalformedBubble(), index);
47 : }
48 : },
49 : );
50 : }
51 :
52 0 : @override
53 : Widget getPreviewWidget(BuildContext context, {BoxConstraints? constraints}) {
54 0 : return ChangeNotifierProvider.value(
55 0 : value: this.metadata,
56 0 : builder: (bcontext, child) {
57 0 : dynamic shareObj = jsonDecode(this.content);
58 : if (shareObj == null) {
59 0 : return MalformedBubble();
60 : }
61 0 : String nameSuggestion = shareObj['f'] as String;
62 0 : String rootHash = shareObj['h'] as String;
63 0 : String nonce = shareObj['n'] as String;
64 0 : int fileSize = shareObj['s'] as int;
65 0 : if (!validHash(rootHash, nonce)) {
66 0 : return MalformedBubble();
67 : }
68 0 : return Container(
69 0 : padding: EdgeInsets.all(1.0),
70 0 : decoration: BoxDecoration(),
71 : clipBehavior: Clip.antiAliasWithSaveLayer,
72 0 : constraints: BoxConstraints(minHeight: 50, maxHeight: 50, minWidth: 50, maxWidth: 300),
73 : alignment: Alignment.centerLeft,
74 0 : child: FileBubble(nameSuggestion, rootHash, nonce, fileSize, isAuto: metadata.isAuto, interactive: false, isPreview: true),
75 : );
76 : },
77 : );
78 : }
79 :
80 0 : @override
81 : MessageMetadata getMetadata() {
82 0 : return this.metadata;
83 : }
84 :
85 0 : bool validHash(String hash, String nonce) {
86 0 : return hash.length == 128 && nonce.length == 48 && !hash.contains(nonHex) && !nonce.contains(nonHex);
87 : }
88 : }
|