diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts index 9dff8fef2..8d9ce1014 100644 --- a/apps/client/src/services/utils.ts +++ b/apps/client/src/services/utils.ts @@ -187,13 +187,15 @@ export function formatSize(size: number | null | undefined) { return ""; } - size = Math.max(Math.round(size / 1024), 1); - - if (size < 1024) { - return `${size} KiB`; + if (size === 0) { + return "0 B"; } - return `${Math.round(size / 102.4) / 10} MiB`; + const k = 1024; + const sizes = ["B", "KB", "MB", "GB"]; + const i = Math.floor(Math.log(size) / Math.log(k)); + + return `${Math.round((size / Math.pow(k, i)) * 100) / 100} ${sizes[i]}`; } function toObject(array: T[], fn: (arg0: T) => [key: string, value: R]) { diff --git a/apps/client/src/widgets/sidebar/PdfAttachments.tsx b/apps/client/src/widgets/sidebar/PdfAttachments.tsx index 1627814af..3269db627 100644 --- a/apps/client/src/widgets/sidebar/PdfAttachments.tsx +++ b/apps/client/src/widgets/sidebar/PdfAttachments.tsx @@ -1,5 +1,6 @@ import "./PdfAttachments.css"; +import { formatSize } from "../../services/utils"; import { useActiveNoteContext, useGetContextData, useNoteProperty } from "../react/hooks"; import Icon from "../react/Icon"; import RightPanelWidget from "./RightPanelWidget"; @@ -52,7 +53,7 @@ function PdfAttachmentItem({ attachment: AttachmentInfo; onDownload: (filename: string) => void; }) { - const sizeText = formatFileSize(attachment.size); + const sizeText = formatSize(attachment.size); return (
onDownload(attachment.filename)}> @@ -65,11 +66,3 @@ function PdfAttachmentItem({
); } - -function formatFileSize(bytes: number): string { - if (bytes === 0) return "0 B"; - const k = 1024; - const sizes = ["B", "KB", "MB", "GB"]; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - return `${Math.round((bytes / Math.pow(k, i)) * 100) / 100 } ${ sizes[i]}`; -}