Show download count info in release list (#10124)

* Show download count info in release list

* Use go-humanize
This commit is contained in:
Lauris BH 2020-02-03 21:50:37 +02:00 committed by GitHub
parent ea50f60df2
commit 20c513be6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 1207 additions and 40 deletions

View file

@ -13,7 +13,6 @@ import (
"encoding/hex"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
@ -29,6 +28,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"github.com/dustin/go-humanize"
"github.com/unknwon/com"
)
@ -214,40 +214,15 @@ func AvatarLink(email string) string {
return SizedAvatarLink(email, DefaultAvatarSize)
}
// Storage space size types
const (
Byte = 1
KByte = Byte * 1024
MByte = KByte * 1024
GByte = MByte * 1024
TByte = GByte * 1024
PByte = TByte * 1024
EByte = PByte * 1024
)
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
func humanateBytes(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%dB", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := float64(s) / math.Pow(base, math.Floor(e))
f := "%.0f"
if val < 10 {
f = "%.1f"
}
return fmt.Sprintf(f+"%s", val, suffix)
}
// FileSize calculates the file size and generate user-friendly string.
func FileSize(s int64) string {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(uint64(s), 1024, sizes)
return humanize.IBytes(uint64(s))
}
// PrettyNumber produces a string form of the given number in base 10 with
// commas after every three orders of magnitud
func PrettyNumber(v int64) string {
return humanize.Comma(v)
}
// Subtract deals with subtraction of all types of number.

View file

@ -103,19 +103,19 @@ func TestAvatarLink(t *testing.T) {
func TestFileSize(t *testing.T) {
var size int64 = 512
assert.Equal(t, "512B", FileSize(size))
assert.Equal(t, "512 B", FileSize(size))
size *= 1024
assert.Equal(t, "512KB", FileSize(size))
assert.Equal(t, "512 KiB", FileSize(size))
size *= 1024
assert.Equal(t, "512MB", FileSize(size))
assert.Equal(t, "512 MiB", FileSize(size))
size *= 1024
assert.Equal(t, "512GB", FileSize(size))
assert.Equal(t, "512 GiB", FileSize(size))
size *= 1024
assert.Equal(t, "512TB", FileSize(size))
assert.Equal(t, "512 TiB", FileSize(size))
size *= 1024
assert.Equal(t, "512PB", FileSize(size))
assert.Equal(t, "512 PiB", FileSize(size))
size *= 4
assert.Equal(t, "2.0EB", FileSize(size))
assert.Equal(t, "2.0 EiB", FileSize(size))
}
func TestSubtract(t *testing.T) {

View file

@ -93,6 +93,7 @@ func NewFuncMap() []template.FuncMap {
"TimeSinceUnix": timeutil.TimeSinceUnix,
"RawTimeSince": timeutil.RawTimeSince,
"FileSize": base.FileSize,
"PrettyNumber": base.PrettyNumber,
"Subtract": base.Subtract,
"EntryIcon": base.EntryIcon,
"MigrationIcon": MigrationIcon,