From 449b2e31179fdd8d183251a2c4bca49ff921abae Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 11 Aug 2025 18:45:12 +0000 Subject: [PATCH] Fix attachment size (#787) Fix `tea releases assets ` displayed wrong attachment size. Reviewed-on: https://gitea.com/gitea/tea/pulls/787 Reviewed-by: techknowlogick Co-authored-by: Lunny Xiao Co-committed-by: Lunny Xiao --- modules/print/attachment.go | 11 ++++++++++- modules/print/formatters.go | 8 ++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/print/attachment.go b/modules/print/attachment.go index 91fe5f3..cdfbb5d 100644 --- a/modules/print/attachment.go +++ b/modules/print/attachment.go @@ -4,9 +4,18 @@ package print import ( + "fmt" + "code.gitea.io/sdk/gitea" ) +func formatByteSize(size int64) string { + if size < 1024 { + return fmt.Sprintf("%d B", size) + } + return formatSize(size / 1024) +} + // ReleaseAttachmentsList prints a listing of release attachments func ReleaseAttachmentsList(attachments []*gitea.Attachment, output string) { t := tableWithHeader( @@ -17,7 +26,7 @@ func ReleaseAttachmentsList(attachments []*gitea.Attachment, output string) { for _, attachment := range attachments { t.addRow( attachment.Name, - formatSize(attachment.Size), + formatByteSize(attachment.Size), ) } diff --git a/modules/print/formatters.go b/modules/print/formatters.go index 2b935b0..512cf53 100644 --- a/modules/print/formatters.go +++ b/modules/print/formatters.go @@ -29,17 +29,17 @@ func getRepoURL(resourceURL string) string { // formatSize get kb in int and return string func formatSize(kb int64) string { if kb < 1024 { - return fmt.Sprintf("%d Kb", kb) + return fmt.Sprintf("%d KB", kb) } mb := kb / 1024 if mb < 1024 { - return fmt.Sprintf("%d Mb", mb) + return fmt.Sprintf("%d MB", mb) } gb := mb / 1024 if gb < 1024 { - return fmt.Sprintf("%d Gb", gb) + return fmt.Sprintf("%d GB", gb) } - return fmt.Sprintf("%d Tb", gb/1024) + return fmt.Sprintf("%d TB", gb/1024) } // FormatTime provides a string for the given time value.