Skip to content

Commit 0683c54

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

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed
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: 9 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,17 @@ func createBucketViewWithS3Data(s3 S3, templates fs.FS, allowDelete bool, listRe
191192
break
192193
}
193194

195+
var sizeDisplay string
196+
if current != nil && current.HumanReadableSize {
197+
sizeDisplay = FormatFileSize(object.Size)
198+
} else {
199+
sizeDisplay = fmt.Sprintf("%d bytes", object.Size)
200+
}
201+
194202
obj := objectWithIcon{
195203
Key: object.Key,
196204
Size: object.Size,
205+
SizeDisplay: sizeDisplay,
197206
LastModified: object.LastModified,
198207
Owner: object.Owner.DisplayName,
199208
Icon: icon(object.Key),

internal/app/s3manager/multi_s3_manager.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type S3InstanceConfig struct {
3838
UseSSL bool
3939
SkipSSLVerification bool
4040
SignatureType string
41+
HumanReadableSize bool
4142
}
4243

4344
// NewMultiS3Manager creates a new MultiS3Manager with the given configurations
@@ -91,9 +92,10 @@ func NewMultiS3Manager(configs []S3InstanceConfig) (*MultiS3Manager, error) {
9192
}
9293

9394
instance := &S3Instance{
94-
ID: instanceID,
95-
Name: config.Name,
96-
Client: s3Client,
95+
ID: instanceID,
96+
Name: config.Name,
97+
Client: s3Client,
98+
HumanReadableSize: config.HumanReadableSize,
9799
}
98100

99101
manager.instances[instanceID] = instance

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type s3InstanceConfig struct {
3333
UseSSL bool
3434
SkipSSLVerification bool
3535
SignatureType string
36+
HumanReadableSize bool
3637
}
3738

3839
type configuration struct {
@@ -76,6 +77,9 @@ func parseConfiguration() configuration {
7677
viper.SetDefault(prefix+"SIGNATURE_TYPE", "V4")
7778
signatureType := viper.GetString(prefix + "SIGNATURE_TYPE")
7879

80+
viper.SetDefault(prefix+"HUMAN_READABLE_SIZE", false)
81+
humanReadableSize := viper.GetBool(prefix + "HUMAN_READABLE_SIZE")
82+
7983
if !useIam {
8084
if accessKeyID == "" {
8185
log.Fatalf("please provide %sACCESS_KEY_ID for instance %s", prefix, name)
@@ -96,6 +100,7 @@ func parseConfiguration() configuration {
96100
UseSSL: useSSL,
97101
SkipSSLVerification: skipSSLVerification,
98102
SignatureType: signatureType,
103+
HumanReadableSize: humanReadableSize,
99104
})
100105
}
101106

@@ -166,6 +171,7 @@ func main() {
166171
UseSSL: instance.UseSSL,
167172
SkipSSLVerification: instance.SkipSSLVerification,
168173
SignatureType: instance.SignatureType,
174+
HumanReadableSize: instance.HumanReadableSize,
169175
})
170176
}
171177

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)