Factor out the download url into a function
This commit is contained in:
parent
138eb64e52
commit
32b546a212
6 changed files with 26 additions and 14 deletions
|
@ -8,8 +8,10 @@ import (
|
||||||
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -17,6 +19,11 @@ import (
|
||||||
"gitlab.com/trantor/trantor/lib/database"
|
"gitlab.com/trantor/trantor/lib/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var tmpl_funcs = map[string]interface{}{
|
||||||
|
"strings_join": stringsJoin,
|
||||||
|
"download_url": downloadUrl,
|
||||||
|
}
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
FullURL string
|
FullURL string
|
||||||
|
@ -68,14 +75,12 @@ func InitTemplate(assetsPath string) *Template {
|
||||||
var t Template
|
var t Template
|
||||||
templatePath := path.Join(assetsPath, "templates")
|
templatePath := path.Join(assetsPath, "templates")
|
||||||
|
|
||||||
t.html, err = html_tmpl.New("html").Funcs(html_tmpl.FuncMap{
|
t.html, err = html_tmpl.New("html").Funcs(tmpl_funcs).ParseGlob(path.Join(templatePath, "*.html"))
|
||||||
"strings_join": strings.Join,
|
|
||||||
}).ParseGlob(path.Join(templatePath, "*.html"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Critical("Error loading html templates: ", err)
|
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 {
|
if err != nil {
|
||||||
log.Critical("Error loading opds templates: ", err)
|
log.Critical("Error loading opds templates: ", err)
|
||||||
}
|
}
|
||||||
|
@ -83,6 +88,15 @@ func InitTemplate(assetsPath string) *Template {
|
||||||
return &t
|
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 {
|
type DevTemplateExecutor struct {
|
||||||
assetsPath string
|
assetsPath string
|
||||||
tpe 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"} {
|
for _, f := range []string{"header.html", "footer.html", "book_list.html"} {
|
||||||
included_files = append(included_files, path.Join(templatePath, f))
|
included_files = append(included_files, path.Join(templatePath, f))
|
||||||
}
|
}
|
||||||
t = html_tmpl.Must(html_tmpl.New("html").Funcs(html_tmpl.FuncMap{
|
t = html_tmpl.Must(html_tmpl.New("html").Funcs(tmpl_funcs).ParseFiles(included_files...))
|
||||||
"strings_join": strings.Join,
|
|
||||||
}).ParseFiles(included_files...))
|
|
||||||
case "txt":
|
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)
|
return t.ExecuteTemplate(wr, name, data)
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ function delBook(){
|
||||||
<div class="span3">
|
<div class="span3">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="btn-group pull-right">
|
<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>
|
<a href="/read/{{.ID}}" class="btn btn-large btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="pull-right">
|
<div class="pull-right">
|
||||||
|
@ -104,7 +104,7 @@ function delBook(){
|
||||||
<form class="form-inline" method="POST" action="/list/">
|
<form class="form-inline" method="POST" action="/list/">
|
||||||
<input type="hidden" id="book_id" name="book_id" value="{{.Book.ID}}">
|
<input type="hidden" id="book_id" name="book_id" value="{{.Book.ID}}">
|
||||||
<div class="input-append">
|
<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>
|
<button type="submit" class="btn btn-primary">Add</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="span3">
|
<div class="span3">
|
||||||
<div class="btn-group pull-right">
|
<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>
|
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -58,7 +58,7 @@
|
||||||
<div class="row"><p></p></div>
|
<div class="row"><p></p></div>
|
||||||
{{end}}
|
{{end}}
|
||||||
<div class="row btn-group pull-right">
|
<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>
|
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -51,7 +51,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="row"><p></p></div>
|
<div class="row"><p></p></div>
|
||||||
<div class="row btn-group pull-right">
|
<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>
|
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -56,7 +56,7 @@
|
||||||
<div class="row"><p></p></div>
|
<div class="row"><p></p></div>
|
||||||
{{end}}
|
{{end}}
|
||||||
<div class="row btn-group pull-right">
|
<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>
|
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Reference in a new issue