Skip to content

Commit

Permalink
Show download count info in release list (#10124)
Browse files Browse the repository at this point in the history
* Show download count info in release list

* Use go-humanize
  • Loading branch information
lafriks authored Feb 3, 2020
1 parent ea50f60 commit 20c513b
Show file tree
Hide file tree
Showing 23 changed files with 1,207 additions and 40 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/cznic/strutil v0.0.0-20181122101858-275e90344537 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/editorconfig/editorconfig-core-go/v2 v2.1.1
github.com/emirpasic/gods v1.12.0
github.com/etcd-io/bbolt v1.3.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
Expand Down
41 changes: 8 additions & 33 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"encoding/hex"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,7 @@ release.deletion_success = The release has been deleted.
release.tag_name_already_exist = A release with this tag name already exists.
release.tag_name_invalid = The tag name is not valid.
release.downloads = Downloads
release.download_count = Downloads: %s

branch.name = Branch Name
branch.search = Search branches
Expand Down
1 change: 1 addition & 0 deletions templates/repo/release/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
{{if .Attachments}}
{{range .Attachments}}
<li>
<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>
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}">
<strong><span class="ui image octicon octicon-package" title='{{.Name}}'></span> {{.Name}}</strong>
<span class="ui text grey right">{{.Size | FileSize}}</span>
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/dustin/go-humanize/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/dustin/go-humanize/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions vendor/github.com/dustin/go-humanize/README.markdown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/dustin/go-humanize/big.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 20c513b

Please sign in to comment.