Fix attachment size (#787)

Fix `tea releases assets <tag_name>` displayed wrong attachment size.

Reviewed-on: https://gitea.com/gitea/tea/pulls/787
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-committed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
Lunny Xiao
2025-08-11 18:45:12 +00:00
committed by techknowlogick
parent 9e8c71e13e
commit 449b2e3117
2 changed files with 14 additions and 5 deletions

View File

@ -4,9 +4,18 @@
package print package print
import ( import (
"fmt"
"code.gitea.io/sdk/gitea" "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 // ReleaseAttachmentsList prints a listing of release attachments
func ReleaseAttachmentsList(attachments []*gitea.Attachment, output string) { func ReleaseAttachmentsList(attachments []*gitea.Attachment, output string) {
t := tableWithHeader( t := tableWithHeader(
@ -17,7 +26,7 @@ func ReleaseAttachmentsList(attachments []*gitea.Attachment, output string) {
for _, attachment := range attachments { for _, attachment := range attachments {
t.addRow( t.addRow(
attachment.Name, attachment.Name,
formatSize(attachment.Size), formatByteSize(attachment.Size),
) )
} }

View File

@ -29,17 +29,17 @@ func getRepoURL(resourceURL string) string {
// formatSize get kb in int and return string // formatSize get kb in int and return string
func formatSize(kb int64) string { func formatSize(kb int64) string {
if kb < 1024 { if kb < 1024 {
return fmt.Sprintf("%d Kb", kb) return fmt.Sprintf("%d KB", kb)
} }
mb := kb / 1024 mb := kb / 1024
if mb < 1024 { if mb < 1024 {
return fmt.Sprintf("%d Mb", mb) return fmt.Sprintf("%d MB", mb)
} }
gb := mb / 1024 gb := mb / 1024
if gb < 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. // FormatTime provides a string for the given time value.