Browse Source

feat: show human-readable size in error message

pull/9697/head
Pranav C 1 month ago
parent
commit
723aad7968
  1. 11
      packages/nc-gui/components/cell/attachment/utils.ts

11
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 MdiFileExcelOutline from '~icons/mdi/file-excel-outline'
import IcOutlineInsertDriveFile from '~icons/ic/outline-insert-drive-file' 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( export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
(updateModelValue: (data: string | Record<string, any>[]) => void) => { (updateModelValue: (data: string | Record<string, any>[]) => void) => {
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
@ -137,9 +142,9 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
// verify file size // verify file size
if (file?.size && file.size > attachmentMeta.maxAttachmentSize) { if (file?.size && file.size > attachmentMeta.maxAttachmentSize) {
message.error( message.error(
`The size of ${(file as File)?.name || (file as AttachmentReqType)?.fileName} exceeds the maximum file size ${ `The size of ${
attachmentMeta.maxAttachmentSize (file as File)?.name || (file as AttachmentReqType)?.fileName
} MB.`, } exceeds the maximum file size ${getReadableFileSize(attachmentMeta.maxAttachmentSize)}.`,
) )
continue continue
} }

Loading…
Cancel
Save