LCOV - code coverage report
Current view: top level - lib/models/messages - filemessage.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 40 0.0 %
Date: 2024-09-24 21:50:54 Functions: 0 0 -

          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           0 :   @override
      52             :   Widget getPreviewWidget(BuildContext context, {BoxConstraints? constraints}) {
      53           0 :     return ChangeNotifierProvider.value(
      54           0 :         value: this.metadata,
      55           0 :         builder: (bcontext, child) {
      56           0 :           dynamic shareObj = jsonDecode(this.content);
      57             :           if (shareObj == null) {
      58           0 :             return MalformedBubble();
      59             :           }
      60           0 :           String nameSuggestion = shareObj['f'] as String;
      61           0 :           String rootHash = shareObj['h'] as String;
      62           0 :           String nonce = shareObj['n'] as String;
      63           0 :           int fileSize = shareObj['s'] as int;
      64           0 :           if (!validHash(rootHash, nonce)) {
      65           0 :             return MalformedBubble();
      66             :           }
      67           0 :           return Container(
      68           0 :               padding: EdgeInsets.all(1.0),
      69           0 :               decoration: BoxDecoration(),
      70             :               clipBehavior: Clip.antiAliasWithSaveLayer,
      71           0 :               constraints: BoxConstraints(minHeight: 50, maxHeight: 50, minWidth: 50, maxWidth: 300),
      72             :               alignment: Alignment.centerLeft,
      73           0 :               child: FileBubble(
      74             :                 nameSuggestion,
      75             :                 rootHash,
      76             :                 nonce,
      77             :                 fileSize,
      78           0 :                 isAuto: metadata.isAuto,
      79             :                 interactive: false,
      80             :                 isPreview: true,
      81             :               ));
      82             :         });
      83             :   }
      84             : 
      85           0 :   @override
      86             :   MessageMetadata getMetadata() {
      87           0 :     return this.metadata;
      88             :   }
      89             : 
      90           0 :   bool validHash(String hash, String nonce) {
      91           0 :     return hash.length == 128 && nonce.length == 48 && !hash.contains(nonHex) && !nonce.contains(nonHex);
      92             :   }
      93             : }

Generated by: LCOV version 1.14