aboutsummaryrefslogtreecommitdiffhomepage
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
parent912a98788e66b836125fe8ef37672a4de20c169c (diff)
Show attachment size on entry page
-rw-r--r--template/functions.go23
-rw-r--r--template/functions_test.go19
-rw-r--r--template/html/entry.html4
-rw-r--r--template/views.go6
4 files changed, 43 insertions, 9 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])
+}
diff --git a/template/functions_test.go b/template/functions_test.go
index 5e37319..4f1ec08 100644
--- a/template/functions_test.go
+++ b/template/functions_test.go
@@ -314,3 +314,22 @@ func TestProxyFilterWithHttpsInvalid(t *testing.T) {
t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected)
}
}
+
+func TestFormatFileSize(t *testing.T) {
+ scenarios := []struct {
+ input int64
+ expected string
+ }{
+ {500, "500 B"},
+ {1024, "1.0 KiB"},
+ {43520, "42.5 KiB"},
+ {5000 * 1024 * 1024, "4.9 GiB"},
+ }
+
+ for _, scenario := range scenarios {
+ result := formatFileSize(scenario.input)
+ if result != scenario.expected {
+ t.Errorf(`Unexpected result, got %q instead of %q for %d`, result, scenario.expected, scenario.input)
+ }
+ }
+}
diff --git a/template/html/entry.html b/template/html/entry.html
index 4dcd3ca..c7eea4a 100644
--- a/template/html/entry.html
+++ b/template/html/entry.html
@@ -115,8 +115,8 @@
{{ end }}
<div class="entry-enclosure-download">
- <a href="{{ .URL }}" title="{{ .URL }} ({{ .MimeType }})" target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer">{{ t "action.download" }}</a>
- <small>({{ .URL }})</small>
+ <a href="{{ .URL }}" title="{{ t "action.download" }}{{ if gt .Size 0 }} - {{ formatFileSize .Size }}{{ end }} ({{ .MimeType }})" target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer">{{ .URL }}</a>
+ <small>{{ if gt .Size 0 }} - <strong>{{ formatFileSize .Size }}</strong>{{ end }}</small>
</div>
</div>
{{ end }}
diff --git a/template/views.go b/template/views.go
index c4fadb4..21b29fb 100644
--- a/template/views.go
+++ b/template/views.go
@@ -680,8 +680,8 @@ var templateViewsMap = map[string]string{
{{ end }}
<div class="entry-enclosure-download">
- <a href="{{ .URL }}" title="{{ .URL }} ({{ .MimeType }})" target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer">{{ t "action.download" }}</a>
- <small>({{ .URL }})</small>
+ <a href="{{ .URL }}" title="{{ t "action.download" }}{{ if gt .Size 0 }} - {{ formatFileSize .Size }}{{ end }} ({{ .MimeType }})" target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer">{{ .URL }}</a>
+ <small>{{ if gt .Size 0 }} - <strong>{{ formatFileSize .Size }}</strong>{{ end }}</small>
</div>
</div>
{{ end }}
@@ -1354,7 +1354,7 @@ var templateViewsMapChecksums = map[string]string{
"edit_category": "b1c0b38f1b714c5d884edcd61e5b5295a5f1c8b71c469b35391e4dcc97cc6d36",
"edit_feed": "34aa0d668b3ea1a1b5fa480c20cebeae729b37010af3bb915d2a9eed73d3b996",
"edit_user": "c692db9de1a084c57b93e95a14b041d39bf489846cbb91fc982a62b72b77062a",
- "entry": "24aeba26ef9a51ce585ca5c4af090f1de7d7bfd7f1e3ff1b63af520e2afa76bd",
+ "entry": "36c75170b4831e96ba7d7726f8c80e5401366c626a1283629712a47f5e1e8caf",
"feed_entries": "9c70b82f55e4b311eff20be1641733612e3c1b406ce8010861e4c417d97b6dcc",
"feeds": "fa06cd1e1e3fec79132386972c640a2fe91237f5dba572389d5f45be74545f25",
"history_entries": "87e17d39de70eb3fdbc4000326283be610928758eae7924e4b08dcb446f3b6a9",