From 56f59d132b49c362490ff4f4c5325b23d243e97c Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sat, 9 Apr 2022 19:02:10 +0100 Subject: [PATCH] fix: don't paste file anyways if too large --- src/context/revoltjs/FileUploads.tsx | 64 +++++++++++++++++----------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/context/revoltjs/FileUploads.tsx b/src/context/revoltjs/FileUploads.tsx index ca17b3f3..26d1b863 100644 --- a/src/context/revoltjs/FileUploads.tsx +++ b/src/context/revoltjs/FileUploads.tsx @@ -20,33 +20,39 @@ type BehaviourType = | { behaviour: "ask"; onChange: (file: File) => void } | { behaviour: "upload"; onUpload: (id: string) => Promise } | { - behaviour: "multi"; - onChange: (files: File[]) => void; - append?: (files: File[]) => void; - } + behaviour: "multi"; + onChange: (files: File[]) => void; + append?: (files: File[]) => void; + }; type StyleType = | { - style: "icon" | "banner"; - width?: number; - height?: number; - previewURL?: string; - defaultPreview?: string; - desaturateDefault?: boolean - } + style: "icon" | "banner"; + width?: number; + height?: number; + previewURL?: string; + defaultPreview?: string; + desaturateDefault?: boolean; + } | { - style: "attachment"; - attached: boolean; - uploading: boolean; - cancel: () => void; - size?: number; - } + style: "attachment"; + attached: boolean; + uploading: boolean; + cancel: () => void; + size?: number; + }; -type Props = BehaviourType & StyleType & { - fileType: "backgrounds" | "icons" | "avatars" | "attachments" | "banners"; - maxFileSize: number; - remove: () => Promise; -} +type Props = BehaviourType & + StyleType & { + fileType: + | "backgrounds" + | "icons" + | "avatars" + | "attachments" + | "banners"; + maxFileSize: number; + remove: () => Promise; + }; export async function uploadFile( autumnURL: string, @@ -80,6 +86,7 @@ export function grabFiles( input.onchange = async (e) => { const files = (e.currentTarget as HTMLInputElement)?.files; if (!files) return; + for (const file of files) { if (file.size > maxFileSize) { return tooLarge(); @@ -167,6 +174,7 @@ export function FileUploader(props: Props) { id: "error", error: "FileTooLarge", }); + continue; } files.push(blob); @@ -195,6 +203,7 @@ export function FileUploader(props: Props) { for (const item of dropped) { if (item.size > props.maxFileSize) { openScreen({ id: "error", error: "FileTooLarge" }); + continue; } files.push(item); @@ -226,14 +235,19 @@ export function FileUploader(props: Props) { })} data-uploading={uploading}>