2016-05-02 21:36:49 -04:00
|
|
|
package trantor
|
2012-08-15 11:51:25 +02:00
|
|
|
|
2014-08-30 03:56:36 -05:00
|
|
|
import (
|
2016-05-03 01:03:23 -04:00
|
|
|
html_tmpl "html/template"
|
2014-08-30 03:56:36 -05:00
|
|
|
txt_tmpl "text/template"
|
2013-08-31 00:38:32 +02:00
|
|
|
|
2014-08-30 03:56:36 -05:00
|
|
|
log "github.com/cihub/seelog"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2016-05-03 01:03:23 -04:00
|
|
|
"path"
|
2015-01-17 02:19:14 -06:00
|
|
|
"time"
|
2014-08-30 13:17:50 -05:00
|
|
|
|
2016-05-02 21:36:49 -04:00
|
|
|
"gitlab.com/trantor/trantor/lib/database"
|
2014-08-30 03:56:36 -05:00
|
|
|
)
|
2014-08-30 01:25:16 -05:00
|
|
|
|
2012-08-18 02:06:43 +02:00
|
|
|
type Status struct {
|
2014-02-19 11:59:16 +01:00
|
|
|
BaseURL string
|
|
|
|
FullURL string
|
2016-01-28 18:13:26 -05:00
|
|
|
Title string
|
2014-02-19 11:59:16 +01:00
|
|
|
Search string
|
|
|
|
User string
|
|
|
|
IsAdmin bool
|
|
|
|
Notif []Notification
|
2015-01-17 02:19:14 -06:00
|
|
|
Updated string
|
2014-02-19 11:59:16 +01:00
|
|
|
Home bool
|
|
|
|
About bool
|
|
|
|
News bool
|
|
|
|
Upload bool
|
|
|
|
Stats bool
|
|
|
|
Help bool
|
|
|
|
Dasboard bool
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func GetStatus(h handler) Status {
|
2012-08-19 02:29:34 +02:00
|
|
|
var s Status
|
2016-05-03 01:03:23 -04:00
|
|
|
s.BaseURL = "http://" + h.hostname
|
2013-09-23 16:27:31 +02:00
|
|
|
s.FullURL = s.BaseURL + h.r.RequestURI
|
2016-01-28 18:13:26 -05:00
|
|
|
s.Title = "Imperial Library of Trantor"
|
2013-09-23 16:27:31 +02:00
|
|
|
s.User = h.sess.User
|
|
|
|
s.IsAdmin = h.sess.IsAdmin()
|
|
|
|
s.Notif = h.sess.GetNotif()
|
2015-01-17 02:19:14 -06:00
|
|
|
s.Updated = time.Now().UTC().Format("2006-01-02T15:04:05Z")
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Save(h.w, h.r)
|
2012-08-19 02:29:34 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-05-03 01:03:23 -04:00
|
|
|
type Template struct {
|
|
|
|
tmpl_html *html_tmpl.Template
|
|
|
|
tmpl_rss *txt_tmpl.Template
|
|
|
|
tmpl_opds *txt_tmpl.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitTemplate(assetsPath string) *Template {
|
|
|
|
var t Template
|
|
|
|
templatePath := path.Join(assetsPath, "templates")
|
|
|
|
|
|
|
|
t.tmpl_html = html_tmpl.Must(html_tmpl.ParseFiles(
|
|
|
|
path.Join(templatePath, "header.html"),
|
|
|
|
path.Join(templatePath, "footer.html"),
|
|
|
|
path.Join(templatePath, "404.html"),
|
|
|
|
path.Join(templatePath, "index.html"),
|
|
|
|
path.Join(templatePath, "about.html"),
|
|
|
|
path.Join(templatePath, "news.html"),
|
|
|
|
path.Join(templatePath, "edit_news.html"),
|
|
|
|
path.Join(templatePath, "book.html"),
|
|
|
|
path.Join(templatePath, "search.html"),
|
|
|
|
path.Join(templatePath, "upload.html"),
|
|
|
|
path.Join(templatePath, "login.html"),
|
|
|
|
path.Join(templatePath, "new.html"),
|
|
|
|
path.Join(templatePath, "read.html"),
|
|
|
|
path.Join(templatePath, "edit.html"),
|
|
|
|
path.Join(templatePath, "dashboard.html"),
|
|
|
|
path.Join(templatePath, "settings.html"),
|
|
|
|
path.Join(templatePath, "stats.html"),
|
|
|
|
path.Join(templatePath, "help.html"),
|
|
|
|
))
|
|
|
|
|
|
|
|
t.tmpl_rss = txt_tmpl.Must(txt_tmpl.ParseFiles(
|
|
|
|
path.Join(templatePath, "search.rss"),
|
|
|
|
path.Join(templatePath, "news.rss"),
|
|
|
|
))
|
|
|
|
|
|
|
|
t.tmpl_opds = txt_tmpl.Must(txt_tmpl.ParseFiles(
|
|
|
|
path.Join(templatePath, "index.opds"),
|
|
|
|
path.Join(templatePath, "search.opds"),
|
|
|
|
))
|
|
|
|
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Template) load(h handler, tmpl string, data interface{}) {
|
2014-08-30 01:25:16 -05:00
|
|
|
var err error
|
|
|
|
fmt := h.r.FormValue("fmt")
|
2014-08-30 03:56:36 -05:00
|
|
|
switch fmt {
|
|
|
|
case "rss":
|
2016-05-03 01:03:23 -04:00
|
|
|
err = t.tmpl_rss.ExecuteTemplate(h.w, tmpl+".rss", data)
|
2015-01-17 02:19:14 -06:00
|
|
|
case "opds":
|
2016-05-03 01:03:23 -04:00
|
|
|
err = t.tmpl_opds.ExecuteTemplate(h.w, tmpl+".opds", data)
|
2014-08-30 03:56:36 -05:00
|
|
|
case "json":
|
|
|
|
err = loadJson(h.w, tmpl, data)
|
|
|
|
default:
|
2016-05-03 01:03:23 -04:00
|
|
|
err = t.tmpl_html.ExecuteTemplate(h.w, tmpl+".html", data)
|
2014-08-30 01:25:16 -05:00
|
|
|
}
|
2013-08-31 00:38:32 +02:00
|
|
|
if err != nil {
|
2016-05-03 01:03:23 -04:00
|
|
|
t.tmpl_html.ExecuteTemplate(h.w, "404.html", data)
|
2014-08-30 01:25:16 -05:00
|
|
|
log.Warn("An error ocurred loading the template ", tmpl, ".", fmt, ": ", err)
|
2013-08-31 00:38:32 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-30 03:56:36 -05:00
|
|
|
|
|
|
|
func loadJson(w http.ResponseWriter, tmpl string, data interface{}) error {
|
|
|
|
var res []byte
|
|
|
|
var err error
|
|
|
|
switch tmpl {
|
|
|
|
case "index":
|
|
|
|
res, err = indexJson(data)
|
|
|
|
case "book":
|
|
|
|
res, err = bookJson(data)
|
|
|
|
case "news":
|
|
|
|
res, err = newsJson(data)
|
|
|
|
case "search":
|
|
|
|
res, err = searchJson(data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(res)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexJson(data interface{}) ([]byte, error) {
|
|
|
|
index, ok := data.(indexData)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Data is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
books := make([]map[string]interface{}, len(index.Books))
|
|
|
|
for i, book := range index.Books {
|
|
|
|
books[i] = bookJsonRaw(book)
|
|
|
|
}
|
|
|
|
news := newsJsonRaw(index.News)
|
|
|
|
|
|
|
|
return json.Marshal(map[string]interface{}{
|
2016-01-28 18:13:26 -05:00
|
|
|
"title": index.S.Title,
|
2014-08-30 03:56:36 -05:00
|
|
|
"url": index.S.BaseURL,
|
|
|
|
"count": index.Count,
|
|
|
|
"news": news,
|
|
|
|
"tags": index.Tags,
|
2014-08-30 15:02:51 -05:00
|
|
|
"last_added": books,
|
2014-08-30 03:56:36 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func bookJson(data interface{}) ([]byte, error) {
|
|
|
|
book, ok := data.(bookData)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Data is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(bookJsonRaw(book.Book))
|
|
|
|
}
|
|
|
|
|
|
|
|
func newsJson(data interface{}) ([]byte, error) {
|
|
|
|
news, ok := data.(newsData)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Data is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(newsJsonRaw(news.News))
|
|
|
|
}
|
|
|
|
|
|
|
|
func newsJsonRaw(news []newsEntry) []map[string]string {
|
|
|
|
list := make([]map[string]string, len(news))
|
|
|
|
for i, n := range news {
|
|
|
|
list[i] = map[string]string{
|
|
|
|
"date": n.Date,
|
|
|
|
"text": n.Text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func searchJson(data interface{}) ([]byte, error) {
|
|
|
|
search, ok := data.(searchData)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Data is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
books := make([]map[string]interface{}, len(search.Books))
|
|
|
|
for i, book := range search.Books {
|
|
|
|
books[i] = bookJsonRaw(book)
|
|
|
|
}
|
2014-08-30 18:22:48 -05:00
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"found": search.Found,
|
|
|
|
"page": search.Page - 1,
|
|
|
|
"items": search.ItemsPage,
|
|
|
|
"books": books,
|
|
|
|
})
|
2014-08-30 03:56:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func bookJsonRaw(book database.Book) map[string]interface{} {
|
|
|
|
cover := ""
|
|
|
|
coverSmall := ""
|
|
|
|
if book.Cover {
|
|
|
|
cover = "/cover/" + book.Id + "/big/" + book.Title + ".jpg"
|
|
|
|
coverSmall = "/cover/" + book.Id + "/small/" + book.Title + ".jpg"
|
|
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
|
|
"id": book.Id,
|
|
|
|
"title": book.Title,
|
|
|
|
"author": book.Author,
|
|
|
|
"contributor": book.Contributor,
|
|
|
|
"publisher": book.Publisher,
|
|
|
|
"description": book.Description,
|
|
|
|
"subject": book.Subject,
|
|
|
|
"date": book.Date,
|
|
|
|
"lang": book.Lang,
|
|
|
|
"isbn": book.Isbn,
|
|
|
|
"size": book.FileSize,
|
|
|
|
"cover": cover,
|
|
|
|
"cover_small": coverSmall,
|
|
|
|
"download": "/download/" + book.Id + "/" + book.Title + ".epub",
|
|
|
|
"read": "/read/" + book.Id,
|
|
|
|
}
|
|
|
|
}
|