1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

Show attachment size on entry page

This commit is contained in:
Frédéric Guillot 2019-11-29 10:27:25 -08:00
parent 912a98788e
commit b3869a7833
4 changed files with 43 additions and 9 deletions

View file

@ -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])
}