aboutsummaryrefslogtreecommitdiffhomepage
path: root/template/functions.go
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2019-11-29 10:27:25 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2019-11-29 10:36:35 -0800
commitb3869a783323fc424f8321d1a9a6379ef90b32c0 (patch)
treec7273372f684d1ab997238c32cb172288fdea9e7 /template/functions.go
parent912a98788e66b836125fe8ef37672a4de20c169c (diff)
Show attachment size on entry page
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])
+}