Skip to content

Commit 20c513b

Browse files
authored
Show download count info in release list (#10124)
* Show download count info in release list * Use go-humanize
1 parent ea50f60 commit 20c513b

File tree

23 files changed

+1207
-40
lines changed

23 files changed

+1207
-40
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
github.com/cznic/strutil v0.0.0-20181122101858-275e90344537 // indirect
3333
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
3434
github.com/dgrijalva/jwt-go v3.2.0+incompatible
35+
github.com/dustin/go-humanize v1.0.0
3536
github.com/editorconfig/editorconfig-core-go/v2 v2.1.1
3637
github.com/emirpasic/gods v1.12.0
3738
github.com/etcd-io/bbolt v1.3.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
133133
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
134134
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
135135
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
136+
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
137+
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
136138
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
137139
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
138140
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=

modules/base/tool.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"encoding/hex"
1414
"fmt"
1515
"io"
16-
"math"
1716
"net/http"
1817
"net/url"
1918
"os"
@@ -29,6 +28,7 @@ import (
2928
"code.gitea.io/gitea/modules/log"
3029
"code.gitea.io/gitea/modules/setting"
3130

31+
"github.com/dustin/go-humanize"
3232
"github.com/unknwon/com"
3333
)
3434

@@ -214,40 +214,15 @@ func AvatarLink(email string) string {
214214
return SizedAvatarLink(email, DefaultAvatarSize)
215215
}
216216

217-
// Storage space size types
218-
const (
219-
Byte = 1
220-
KByte = Byte * 1024
221-
MByte = KByte * 1024
222-
GByte = MByte * 1024
223-
TByte = GByte * 1024
224-
PByte = TByte * 1024
225-
EByte = PByte * 1024
226-
)
227-
228-
func logn(n, b float64) float64 {
229-
return math.Log(n) / math.Log(b)
230-
}
231-
232-
func humanateBytes(s uint64, base float64, sizes []string) string {
233-
if s < 10 {
234-
return fmt.Sprintf("%dB", s)
235-
}
236-
e := math.Floor(logn(float64(s), base))
237-
suffix := sizes[int(e)]
238-
val := float64(s) / math.Pow(base, math.Floor(e))
239-
f := "%.0f"
240-
if val < 10 {
241-
f = "%.1f"
242-
}
243-
244-
return fmt.Sprintf(f+"%s", val, suffix)
245-
}
246-
247217
// FileSize calculates the file size and generate user-friendly string.
248218
func FileSize(s int64) string {
249-
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
250-
return humanateBytes(uint64(s), 1024, sizes)
219+
return humanize.IBytes(uint64(s))
220+
}
221+
222+
// PrettyNumber produces a string form of the given number in base 10 with
223+
// commas after every three orders of magnitud
224+
func PrettyNumber(v int64) string {
225+
return humanize.Comma(v)
251226
}
252227

253228
// Subtract deals with subtraction of all types of number.

modules/base/tool_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,19 @@ func TestAvatarLink(t *testing.T) {
103103

104104
func TestFileSize(t *testing.T) {
105105
var size int64 = 512
106-
assert.Equal(t, "512B", FileSize(size))
106+
assert.Equal(t, "512 B", FileSize(size))
107107
size *= 1024
108-
assert.Equal(t, "512KB", FileSize(size))
108+
assert.Equal(t, "512 KiB", FileSize(size))
109109
size *= 1024
110-
assert.Equal(t, "512MB", FileSize(size))
110+
assert.Equal(t, "512 MiB", FileSize(size))
111111
size *= 1024
112-
assert.Equal(t, "512GB", FileSize(size))
112+
assert.Equal(t, "512 GiB", FileSize(size))
113113
size *= 1024
114-
assert.Equal(t, "512TB", FileSize(size))
114+
assert.Equal(t, "512 TiB", FileSize(size))
115115
size *= 1024
116-
assert.Equal(t, "512PB", FileSize(size))
116+
assert.Equal(t, "512 PiB", FileSize(size))
117117
size *= 4
118-
assert.Equal(t, "2.0EB", FileSize(size))
118+
assert.Equal(t, "2.0 EiB", FileSize(size))
119119
}
120120

121121
func TestSubtract(t *testing.T) {

modules/templates/helper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func NewFuncMap() []template.FuncMap {
9393
"TimeSinceUnix": timeutil.TimeSinceUnix,
9494
"RawTimeSince": timeutil.RawTimeSince,
9595
"FileSize": base.FileSize,
96+
"PrettyNumber": base.PrettyNumber,
9697
"Subtract": base.Subtract,
9798
"EntryIcon": base.EntryIcon,
9899
"MigrationIcon": MigrationIcon,

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,7 @@ release.deletion_success = The release has been deleted.
15651565
release.tag_name_already_exist = A release with this tag name already exists.
15661566
release.tag_name_invalid = The tag name is not valid.
15671567
release.downloads = Downloads
1568+
release.download_count = Downloads: %s
15681569

15691570
branch.name = Branch Name
15701571
branch.search = Search branches

templates/repo/release/list.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
{{if .Attachments}}
8585
{{range .Attachments}}
8686
<li>
87+
<span class="ui text right" data-tooltip="{{$.i18n.Tr "repo.release.download_count" (.DownloadCount | PrettyNumber)}}" data-position="bottom right"><i class="ui octicon octicon-info"></i></span>
8788
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}">
8889
<strong><span class="ui image octicon octicon-package" title='{{.Name}}'></span> {{.Name}}</strong>
8990
<span class="ui text grey right">{{.Size | FileSize}}</span>

vendor/github.com/dustin/go-humanize/.travis.yml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/dustin/go-humanize/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/dustin/go-humanize/README.markdown

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/dustin/go-humanize/big.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)