Factor out the download url into a function

This commit is contained in:
Las Zenow 2020-12-10 12:44:36 +00:00
parent 138eb64e52
commit 32b546a212
6 changed files with 26 additions and 14 deletions

View file

@ -8,8 +8,10 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
@ -17,6 +19,11 @@ import (
"gitlab.com/trantor/trantor/lib/database"
)
var tmpl_funcs = map[string]interface{}{
"strings_join": stringsJoin,
"download_url": downloadUrl,
}
type Status struct {
BaseURL string
FullURL string
@ -68,14 +75,12 @@ func InitTemplate(assetsPath string) *Template {
var t Template
templatePath := path.Join(assetsPath, "templates")
t.html, err = html_tmpl.New("html").Funcs(html_tmpl.FuncMap{
"strings_join": strings.Join,
}).ParseGlob(path.Join(templatePath, "*.html"))
t.html, err = html_tmpl.New("html").Funcs(tmpl_funcs).ParseGlob(path.Join(templatePath, "*.html"))
if err != nil {
log.Critical("Error loading html templates: ", err)
}
t.opds, err = txt_tmpl.ParseGlob(path.Join(templatePath, "*.opds"))
t.opds, err = txt_tmpl.New("opds").Funcs(tmpl_funcs).ParseGlob(path.Join(templatePath, "*.opds"))
if err != nil {
log.Critical("Error loading opds templates: ", err)
}
@ -83,6 +88,15 @@ func InitTemplate(assetsPath string) *Template {
return &t
}
func stringsJoin(strs []string) string {
return strings.Join(strs, ", ")
}
func downloadUrl(book database.Book) string {
fileName := url.PathEscape(fmt.Sprintf("%s - %s.epub", strings.Join(book.Authors, ", "), book.Title))
return fmt.Sprintf("/download/%s/%s", book.ID, fileName)
}
type DevTemplateExecutor struct {
assetsPath string
tpe string
@ -106,11 +120,9 @@ func (e DevTemplateExecutor) ExecuteTemplate(wr io.Writer, name string, data int
for _, f := range []string{"header.html", "footer.html", "book_list.html"} {
included_files = append(included_files, path.Join(templatePath, f))
}
t = html_tmpl.Must(html_tmpl.New("html").Funcs(html_tmpl.FuncMap{
"strings_join": strings.Join,
}).ParseFiles(included_files...))
t = html_tmpl.Must(html_tmpl.New("html").Funcs(tmpl_funcs).ParseFiles(included_files...))
case "txt":
t = txt_tmpl.Must(txt_tmpl.ParseFiles(file))
t = txt_tmpl.Must(txt_tmpl.New("txt").Funcs(tmpl_funcs).ParseFiles(file))
}
return t.ExecuteTemplate(wr, name, data)
}

View file

@ -50,7 +50,7 @@ function delBook(){
<div class="span3">
<div class="row">
<div class="btn-group pull-right">
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="{{download_url .}}" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-large btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
<div class="pull-right">
@ -104,7 +104,7 @@ function delBook(){
<form class="form-inline" method="POST" action="/list/">
<input type="hidden" id="book_id" name="book_id" value="{{.Book.ID}}">
<div class="input-append">
<input type="text" data-provide="typeahead" data-source='["{{strings_join .UserLists "\", \""}}"]' data-items="4" id="list" name="list" autocomplete="off">
<input type="text" data-provide="typeahead" data-source='["{{strings_join .UserLists}}"]' data-items="4" id="list" name="list" autocomplete="off">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>

View file

@ -15,7 +15,7 @@
</div>
<div class="span3">
<div class="btn-group pull-right">
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="{{download_url .}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>

View file

@ -58,7 +58,7 @@
<div class="row"><p></p></div>
{{end}}
<div class="row btn-group pull-right">
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="{{download_url .}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>

View file

@ -51,7 +51,7 @@
</div>
<div class="row"><p></p></div>
<div class="row btn-group pull-right">
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="{{download_url .}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>

View file

@ -56,7 +56,7 @@
<div class="row"><p></p></div>
{{end}}
<div class="row btn-group pull-right">
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="{{download_url .}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>