From 4224ef142e9b7d7f34ae7f24799d62ea787787c5 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 15 Mar 2014 12:29:49 -0400 Subject: [PATCH 1/2] Add template func FileSize --- modules/base/template.go | 1 + modules/base/tool.go | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/modules/base/template.go b/modules/base/template.go index 620bf4a18..b38ab140c 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -21,6 +21,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ }, "str2html": Str2html, "TimeSince": TimeSince, + "FileSize": FileSize, "Subtract": Subtract, "ActionIcon": ActionIcon, "ActionDesc": ActionDesc, diff --git a/modules/base/tool.go b/modules/base/tool.go index 4802c413e..8576b941c 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -8,6 +8,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" + "math" "strings" "time" ) @@ -80,6 +81,51 @@ func TimeSince(then time.Time) string { return then.String() } +const ( + Byte = 1 + KByte = Byte * 1024 + MByte = KByte * 1024 + GByte = MByte * 1024 + TByte = GByte * 1024 + PByte = TByte * 1024 + EByte = PByte * 1024 +) + +var bytesSizeTable = map[string]uint64{ + "b": Byte, + "kb": KByte, + "mb": MByte, + "gb": GByte, + "tb": TByte, + "pb": PByte, + "eb": EByte, +} + +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 uint64) string { + sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} + return humanateBytes(uint64(s), 1024, sizes) +} + // Subtract deals with subtraction of all types of number. func Subtract(left interface{}, right interface{}) interface{} { var rleft, rright int64 From 8fb4b3afadaea5a335b0e13a02e44377d8714523 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 15 Mar 2014 12:31:12 -0400 Subject: [PATCH 2/2] Add template func FileSize --- modules/base/tool.go | 2 +- templates/repo/single.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/base/tool.go b/modules/base/tool.go index 8576b941c..3f8b8ffa8 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -121,7 +121,7 @@ func humanateBytes(s uint64, base float64, sizes []string) string { } // FileSize calculates the file size and generate user-friendly string. -func FileSize(s uint64) string { +func FileSize(s int64) string { sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"} return humanateBytes(uint64(s), 1024, sizes) } diff --git a/templates/repo/single.tmpl b/templates/repo/single.tmpl index b99950ef5..5774c0051 100644 --- a/templates/repo/single.tmpl +++ b/templates/repo/single.tmpl @@ -47,7 +47,7 @@ {{if .IsDir}} {{.Name}} {{else}} - {{.Name}} - {{.Size}} + {{.Name}} - {{FileSize .Size}} {{end}} {{.Message}}