Vary number of decimals of file sizes

This commit is contained in:
Fedor Indutny
2025-02-11 15:01:27 -08:00
committed by GitHub
parent 8c4a7cb2be
commit 1364ba5784
3 changed files with 13 additions and 11 deletions

View File

@@ -775,10 +775,9 @@ export function Lightbox({
{attachment.totalDownloaded && attachment.size
? i18n('icu:lightBoxDownloading', {
downloaded: formatFileSize(
attachment.totalDownloaded,
2
attachment.totalDownloaded
),
total: formatFileSize(attachment.size, 2),
total: formatFileSize(attachment.size),
})
: undefined}
</Toast>

View File

@@ -126,7 +126,7 @@ export function AttachmentDetailPill({
);
text = (
<div className="AttachmentDetailPill__text-wrapper">
{formatFileSize(totalSize, 2)}
{formatFileSize(totalSize)}
</div>
);
} else if (totalDownloadedSize > 0) {
@@ -148,9 +148,9 @@ export function AttachmentDetailPill({
text = (
<div className="AttachmentDetailPill__text-wrapper">
{totalDownloadedSize > 0 && areAnyPending
? `${formatFileSize(totalDownloadedSize, 2)} / `
? `${formatFileSize(totalDownloadedSize)} / `
: undefined}
{formatFileSize(totalSize, 2)}
{formatFileSize(totalSize)}
</div>
);
} else {
@@ -165,7 +165,7 @@ export function AttachmentDetailPill({
);
text = (
<div className="AttachmentDetailPill__text-wrapper">
{formatFileSize(totalSize, 2)}
{formatFileSize(totalSize)}
</div>
);
}
@@ -191,9 +191,9 @@ export function AttachmentDetailPill({
<div className="AttachmentDetailPill">
<div className="AttachmentDetailPill__text-wrapper">
{totalDownloadedSize > 0 && areAnyPending
? `${formatFileSize(totalDownloadedSize, 2)} / `
? `${formatFileSize(totalDownloadedSize)} / `
: undefined}
{formatFileSize(totalSize, 2)}
{formatFileSize(totalSize)}
{isGif ? ' · GIF' : undefined}
</div>
</div>

View File

@@ -2,6 +2,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
import filesize from 'filesize';
export function formatFileSize(size: number, decimals = 0): string {
return filesize(size, { round: decimals });
// Intentional, `filesize` uses `jedec` standard by default
const MB = 1000 * 1000;
export function formatFileSize(size: number): string {
return filesize(size, { round: size < MB ? 0 : 1 });
}