From e4db6eb5d73341558a8293ce0ec222a64457a0d5 Mon Sep 17 00:00:00 2001 From: Rexogamer Date: Sat, 28 Oct 2023 08:02:46 +0100 Subject: [PATCH] fix: ignore attachments that are too big --- src/MessageBox.tsx | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/MessageBox.tsx b/src/MessageBox.tsx index 8363afc..23391d6 100644 --- a/src/MessageBox.tsx +++ b/src/MessageBox.tsx @@ -5,7 +5,6 @@ import {observer} from 'mobx-react-lite'; import DocumentPicker, { DocumentPickerResponse, } from 'react-native-document-picker'; -import FA5Icon from 'react-native-vector-icons/FontAwesome5'; import MaterialIcon from 'react-native-vector-icons/MaterialIcons'; import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons'; @@ -18,7 +17,7 @@ import {styles, currentTheme} from './Theme'; import {Text, Username} from './components/common/atoms'; import {USER_IDS} from './lib/consts'; import {ReplyingMessage} from './lib/types'; -import {getReadableFileSize} from './lib/utils'; +import {getReadableFileSize, showToast} from './lib/utils'; let typing = false; @@ -190,25 +189,33 @@ export const MessageBox = observer((props: MessageBoxProps) => { let res = await DocumentPicker.pickSingle({ type: [DocumentPicker.types.allFiles], }); - let isDuplicate = false; - for (const a of attachments) { - if (a.uri === res.uri) { + let tooBig = false; + if (res.size && res.size > 20000000) { + showToast('Attachments must be less than 20MB!'); + tooBig = true; + } + if (!tooBig) { + let isDuplicate = false; + for (const a of attachments) { + if (a.uri === res.uri) { + console.log( + `[MESSAGEBOX] Not pushing duplicate attachment ${res.name} (${res.uri})`, + ); + isDuplicate = true; + } + } + + if (res.uri && !isDuplicate) { console.log( - `[MESSAGEBOX] Not pushing duplicate attachment ${res.name} (${res.uri})`, + `[MESSAGEBOX] Pushing attachment ${res.name} (${res.uri})`, ); - isDuplicate = true; + setAttachments(existingAttachments => [ + ...existingAttachments, + res, + ]); + console.log(attachments); } } - if (res.uri && !isDuplicate) { - console.log( - `[MESSAGEBOX] Pushing attachment ${res.name} (${res.uri})`, - ); - setAttachments(existingAttachments => [ - ...existingAttachments, - res, - ]); - console.log(attachments); - } } catch (error) { console.log(`[MESSAGEBOX] Error: ${error}`); }