From b049ae269b2283913f73cbecda4eecbfc20bf9a7 Mon Sep 17 00:00:00 2001 From: Clivia <132346501+Yanyutin753@users.noreply.github.com> Date: Fri, 2 Aug 2024 09:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84Feat=20show=20the=20size=20of=20fil?= =?UTF-8?q?e=20and=20prepare=20for=20subsequent=20upload=20restrictions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/chat/Controls/Controls.svelte | 1 + src/lib/components/chat/MessageInput.svelte | 1 + .../chat/Messages/UserMessage.svelte | 1 + src/lib/components/common/FileItem.svelte | 22 +++++++++++++++++-- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/lib/components/chat/Controls/Controls.svelte b/src/lib/components/chat/Controls/Controls.svelte index 4e4198e53..4e3968f49 100644 --- a/src/lib/components/chat/Controls/Controls.svelte +++ b/src/lib/components/chat/Controls/Controls.svelte @@ -39,6 +39,7 @@ url={`${file?.url}`} name={file.name} type={file.type} + size={file?.size} dismissible={true} on:dismiss={() => { // Remove the file from the chatFiles array diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 2fa835acd..2e1e4b98e 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -554,6 +554,7 @@ { diff --git a/src/lib/components/chat/Messages/UserMessage.svelte b/src/lib/components/chat/Messages/UserMessage.svelte index 748fcd61b..22b7081d7 100644 --- a/src/lib/components/chat/Messages/UserMessage.svelte +++ b/src/lib/components/chat/Messages/UserMessage.svelte @@ -104,6 +104,7 @@ url={file.url} name={file.name} type={file.type} + size={file?.size} colorClassName="bg-white dark:bg-gray-850 " /> {/if} diff --git a/src/lib/components/common/FileItem.svelte b/src/lib/components/common/FileItem.svelte index 6ae011cae..f4dfd27e7 100644 --- a/src/lib/components/common/FileItem.svelte +++ b/src/lib/components/common/FileItem.svelte @@ -15,6 +15,21 @@ export let name: string; export let type: string; + export let size: number; + + function formatSize(size) { + if (size == null) return 'Unknown size'; + if (typeof size !== 'number' || size < 0) return 'Invalid size'; + if (size === 0) return '0 B'; + const units = ['B', 'KB', 'MB', 'GB', 'TB']; + let unitIndex = 0; + + while (size >= 1024 && unitIndex < units.length - 1) { + size /= 1024; + unitIndex++; + } + return `${size.toFixed(1)} ${units[unitIndex]}`; + }
@@ -93,11 +108,11 @@
-
+
{name}
-
+
{#if type === 'file'} {$i18n.t('File')} {:else if type === 'doc'} @@ -107,6 +122,9 @@ {:else} {type} {/if} + {#if size} + {formatSize(size)} + {/if}