From 723aad796853d352dd94acea12f0e7502b0faf4c Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 22 Oct 2024 07:59:20 +0000 Subject: [PATCH] feat: show human-readable size in error message --- packages/nc-gui/components/cell/attachment/utils.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/nc-gui/components/cell/attachment/utils.ts b/packages/nc-gui/components/cell/attachment/utils.ts index 12215fb270..31bd735a48 100644 --- a/packages/nc-gui/components/cell/attachment/utils.ts +++ b/packages/nc-gui/components/cell/attachment/utils.ts @@ -8,6 +8,11 @@ import MdiFilePowerpointBox from '~icons/mdi/file-powerpoint-box' import MdiFileExcelOutline from '~icons/mdi/file-excel-outline' import IcOutlineInsertDriveFile from '~icons/ic/outline-insert-drive-file' +export const getReadableFileSize = (sizeInBytes: number) => { + const i = Math.min(Math.floor(Math.log(sizeInBytes) / Math.log(1024)), 4) + return `${(sizeInBytes / 1024 ** i).toFixed(2) * 1} ${['B', 'kB', 'MB', 'GB', 'TB'][i]}` +} + export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState( (updateModelValue: (data: string | Record[]) => void) => { const { $api } = useNuxtApp() @@ -137,9 +142,9 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState( // verify file size if (file?.size && file.size > attachmentMeta.maxAttachmentSize) { message.error( - `The size of ${(file as File)?.name || (file as AttachmentReqType)?.fileName} exceeds the maximum file size ${ - attachmentMeta.maxAttachmentSize - } MB.`, + `The size of ${ + (file as File)?.name || (file as AttachmentReqType)?.fileName + } exceeds the maximum file size ${getReadableFileSize(attachmentMeta.maxAttachmentSize)}.`, ) continue }