From a4b11142b2a18675a5d9783a8069c81f741d2717 Mon Sep 17 00:00:00 2001
From: Las Zenow <zenow@riseup.net>
Date: Tue, 9 Feb 2021 17:32:13 +0000
Subject: [PATCH] Publish the size of the book before download

- Closes: #62
---
 lib/template.go     | 5 +++++
 templates/book.html | 1 +
 2 files changed, 6 insertions(+)

diff --git a/lib/template.go b/lib/template.go
index ae8e311..059b624 100644
--- a/lib/template.go
+++ b/lib/template.go
@@ -22,6 +22,7 @@ import (
 var tmpl_funcs = map[string]interface{}{
 	"strings_join": stringsJoin,
 	"download_url": downloadUrl,
+	"size2mb":      size2mb,
 }
 
 type Status struct {
@@ -97,6 +98,10 @@ func downloadUrl(book database.Book) string {
 	return fmt.Sprintf("/download/%s/%s", book.ID, fileName)
 }
 
+func size2mb(size int) float32 {
+	return float32(size) / (1024.0 * 1024.0)
+}
+
 type DevTemplateExecutor struct {
 	assetsPath string
 	tpe        string
diff --git a/templates/book.html b/templates/book.html
index 83f7dd0..3911e55 100644
--- a/templates/book.html
+++ b/templates/book.html
@@ -44,6 +44,7 @@ function delBook(){
 		{{if .Tags}}<dt>Tags</dt> <dd>{{range .Tags}}<a href="/search/?q=tag:{{.}}">{{.}}</a>, {{end}}</dd>{{end}}
 		{{if .Isbn}}<dt>ISBN</dt> <dd>{{.Isbn}}</dd>{{end}}
 		{{if .Date}}<dt>Date</dt> <dd>{{.Date}}</dd>{{end}}
+		{{if .FileSize}}<dt>Size</dt> <dd>{{printf "%.2f" (size2mb .FileSize)}} MB</dd>{{end}}
 		{{if .Lang}}<dt>Lang</dt> <dd><a href="/search/?q=lang:{{.Lang}}">{{.Lang}}</a> </dd>{{end}}
 		</dl>
 	</div>