fix: use binary prefixes (1024) for file size display

Change HumanBytes to use binary prefixes (1024) instead of decimal
prefixes (1000) for file size calculations. This aligns with standard
computing conventions used by Hugging Face and most file systems.

- 3072 bytes now displays as "3 KB" (3 * 1024) instead of "3.1 KB"
- 4096 bytes now displays as "4 KB" (4 * 1024) instead of "4.1 KB"

Fixes #13405
This commit is contained in:
Nathan Nguyen 2025-12-13 03:50:55 -05:00
parent 4ff8a691bc
commit 932dbb54c5
3 changed files with 36 additions and 33 deletions

View File

@ -948,9 +948,9 @@ func TestListHandler(t *testing.T) {
{Name: "model1", Digest: "sha256:abc123", Size: 1024, ModifiedAt: time.Now().Add(-24 * time.Hour)},
{Name: "model2", Digest: "sha256:def456", Size: 2048, ModifiedAt: time.Now().Add(-48 * time.Hour)},
},
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1.0 KB 24 hours ago \n" +
"model2 sha256:def45 2.0 KB 2 days ago \n",
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1 KB 24 hours ago \n" +
"model2 sha256:def45 2 KB 2 days ago \n",
},
{
name: "filter models by prefix",
@ -959,8 +959,8 @@ func TestListHandler(t *testing.T) {
{Name: "model1", Digest: "sha256:abc123", Size: 1024, ModifiedAt: time.Now().Add(-24 * time.Hour)},
{Name: "model2", Digest: "sha256:def456", Size: 2048, ModifiedAt: time.Now().Add(-24 * time.Hour)},
},
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1.0 KB 24 hours ago \n",
expectedOutput: "NAME ID SIZE MODIFIED \n" +
"model1 sha256:abc12 1 KB 24 hours ago \n",
},
{
name: "server error",

View File

@ -22,18 +22,19 @@ func HumanBytes(b int64) string {
var value float64
var unit string
// Use binary prefixes (1024) for file sizes as is standard in computing
switch {
case b >= TeraByte:
value = float64(b) / TeraByte
case b >= GibiByte*1024:
value = float64(b) / (GibiByte * 1024)
unit = "TB"
case b >= GigaByte:
value = float64(b) / GigaByte
case b >= GibiByte:
value = float64(b) / GibiByte
unit = "GB"
case b >= MegaByte:
value = float64(b) / MegaByte
case b >= MebiByte:
value = float64(b) / MebiByte
unit = "MB"
case b >= KiloByte:
value = float64(b) / KiloByte
case b >= KibiByte:
value = float64(b) / KibiByte
unit = "KB"
default:
return fmt.Sprintf("%d B", b)

View File

@ -14,32 +14,34 @@ func TestHumanBytes(t *testing.T) {
// Test bytes (B)
{0, "0 B"},
{1, "1 B"},
{999, "999 B"},
{1023, "1023 B"},
// Test kilobytes (KB)
{1000, "1 KB"},
{1500, "1.5 KB"},
{999999, "999 KB"},
// Test kilobytes (KB) - using binary (1024)
{1024, "1 KB"},
{1536, "1.5 KB"},
{3072, "3 KB"},
{4096, "4 KB"},
{1048575, "1023 KB"},
// Test megabytes (MB)
{1000000, "1 MB"},
{1500000, "1.5 MB"},
{999999999, "999 MB"},
// Test megabytes (MB) - using binary (1024*1024)
{1048576, "1 MB"},
{1572864, "1.5 MB"},
{1073741823, "1023 MB"},
// Test gigabytes (GB)
{1000000000, "1 GB"},
{1500000000, "1.5 GB"},
{999999999999, "999 GB"},
// Test gigabytes (GB) - using binary (1024*1024*1024)
{1073741824, "1 GB"},
{1610612736, "1.5 GB"},
{1099511627775, "1023 GB"},
// Test terabytes (TB)
{1000000000000, "1 TB"},
{1500000000000, "1.5 TB"},
{1999999999999, "2.0 TB"},
// Test terabytes (TB) - using binary (1024^4)
{1099511627776, "1 TB"},
{1649267441664, "1.5 TB"},
{2199023255551, "2.0 TB"},
// Test fractional values
{1234, "1.2 KB"},
{1234567, "1.2 MB"},
{1234567890, "1.2 GB"},
{1280, "1.2 KB"},
{1310720, "1.2 MB"},
{1288490189, "1.2 GB"},
}
for _, tc := range tests {