mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Show download count info in release list (#10124)
* Show download count info in release list * Use go-humanize
This commit is contained in:
		| @@ -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. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user