Skip to content

Commit f457f70

Browse files
kplgrcloudlena
authored andcommitted
Add human-readable file size formatting and update related components
1 parent 326220b commit f457f70

File tree

7 files changed

+75
-14
lines changed

7 files changed

+75
-14
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
with:
1616
go-version: "1.25"
1717
- name: Checkout repo
18-
uses: actions/checkout@v5
18+
uses: actions/checkout@v6
1919
- name: Lint code
2020
uses: golangci/golangci-lint-action@v9
2121
- name: Run tests

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ require (
3131
github.com/subosito/gotenv v1.6.0 // indirect
3232
github.com/tinylib/msgp v1.6.1 // indirect
3333
go.yaml.in/yaml/v3 v3.0.4 // indirect
34-
golang.org/x/crypto v0.45.0 // indirect
35-
golang.org/x/net v0.47.0 // indirect
36-
golang.org/x/sys v0.38.0 // indirect
37-
golang.org/x/text v0.31.0 // indirect
34+
golang.org/x/crypto v0.46.0 // indirect
35+
golang.org/x/net v0.48.0 // indirect
36+
golang.org/x/sys v0.39.0 // indirect
37+
golang.org/x/text v0.32.0 // indirect
3838
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
3939
gopkg.in/yaml.v3 v3.0.1 // indirect
4040
)

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ github.com/tinylib/msgp v1.6.1 h1:ESRv8eL3u+DNHUoSAAQRE50Hm162zqAnBoGv9PzScPY=
6565
github.com/tinylib/msgp v1.6.1/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
6666
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
6767
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
68-
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
69-
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
70-
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
71-
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
72-
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
73-
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
74-
golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM=
75-
golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM=
68+
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
69+
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
70+
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
71+
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
72+
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
73+
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
74+
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
75+
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
7676
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7777
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
7878
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package s3manager
2+
3+
import "fmt"
4+
5+
// FormatFileSize converts a size in bytes to a human-readable string using the largest appropriate unit.
6+
func FormatFileSize(size int64) string {
7+
const (
8+
KB = 1024.0
9+
MB = 1024.0 * KB
10+
GB = 1024.0 * MB
11+
TB = 1024.0 * GB
12+
)
13+
14+
sizeF := float64(size)
15+
switch {
16+
case sizeF >= TB:
17+
return fmt.Sprintf("%.2f TB", sizeF/TB)
18+
case sizeF >= GB:
19+
return fmt.Sprintf("%.2f GB", sizeF/GB)
20+
case sizeF >= MB:
21+
return fmt.Sprintf("%.2f MB", sizeF/MB)
22+
case sizeF >= KB:
23+
return fmt.Sprintf("%.2f KB", sizeF/KB)
24+
default:
25+
return fmt.Sprintf("%d bytes", size)
26+
}
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package s3manager
2+
3+
import "testing"
4+
5+
func TestFormatFileSize(t *testing.T) {
6+
tests := []struct {
7+
bytes int64
8+
expected string
9+
}{
10+
{0, "0 bytes"},
11+
{1, "1 bytes"},
12+
{512, "512 bytes"},
13+
{1023, "1023 bytes"},
14+
{1024, "1.00 KB"},
15+
{1536, "1.50 KB"},
16+
{1048576, "1.00 MB"},
17+
{1572864, "1.50 MB"},
18+
{1073741824, "1.00 GB"},
19+
{1610612736, "1.50 GB"},
20+
{1099511627776, "1.00 TB"},
21+
{1649267441664, "1.50 TB"},
22+
}
23+
24+
for _, test := range tests {
25+
result := FormatFileSize(test.bytes)
26+
if result != test.expected {
27+
t.Errorf("FormatFileSize(%d) = %q; want %q", test.bytes, result, test.expected)
28+
}
29+
}
30+
}

internal/app/s3manager/manager_handlers.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
142142
type objectWithIcon struct {
143143
Key string
144144
Size int64
145+
SizeDisplay string
145146
LastModified time.Time
146147
Owner string
147148
Icon string
@@ -191,9 +192,12 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
191192
break
192193
}
193194

195+
sizeDisplay := FormatFileSize(object.Size)
196+
194197
obj := objectWithIcon{
195198
Key: object.Key,
196199
Size: object.Size,
200+
SizeDisplay: sizeDisplay,
197201
LastModified: object.LastModified,
198202
Owner: object.Owner.DisplayName,
199203
Icon: icon(object.Key),

web/template/bucket.html.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
{{ end }}>
9393
<i class="material-icons">{{ $object.Icon }}</i> {{ $object.DisplayName }}
9494
</td>
95-
<td>{{ $object.Size }} bytes</td>
95+
<td>{{ $object.SizeDisplay }}</td>
9696
<td>{{ $object.Owner }}</td>
9797
<td>{{ $object.LastModified }}</td>
9898
<td>

0 commit comments

Comments
 (0)