aboutsummaryrefslogtreecommitdiffhomepage
path: root/template/functions.go
diff options
context:
space:
mode:
Diffstat (limited to 'template/functions.go')
-rw-r--r--template/functions.go23
1 files changed, 19 insertions, 4 deletions
diff --git a/template/functions.go b/template/functions.go
index 0f5b180..157935d 100644
--- a/template/functions.go
+++ b/template/functions.go
@@ -31,10 +31,11 @@ type funcMap struct {
// Map returns a map of template functions that are compiled during template parsing.
func (f *funcMap) Map() template.FuncMap {
return template.FuncMap{
- "dict": dict,
- "hasKey": hasKey,
- "truncate": truncate,
- "isEmail": isEmail,
+ "formatFileSize": formatFileSize,
+ "dict": dict,
+ "hasKey": hasKey,
+ "truncate": truncate,
+ "isEmail": isEmail,
"baseURL": func() string {
return config.Opts.BaseURL()
},
@@ -200,3 +201,17 @@ func proxify(router *mux.Router, link string) string {
// We use base64 url encoding to avoid slash in the URL.
return route.Path(router, "proxy", "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
}
+
+func formatFileSize(b int64) string {
+ const unit = 1024
+ if b < unit {
+ return fmt.Sprintf("%d B", b)
+ }
+ div, exp := int64(unit), 0
+ for n := b / unit; n >= unit; n /= unit {
+ div *= unit
+ exp++
+ }
+ return fmt.Sprintf("%.1f %ciB",
+ float64(b)/float64(div), "KMGTPE"[exp])
+}