Merge branch 'master' into stats
This commit is contained in:
commit
68254d359c
7 changed files with 86 additions and 39 deletions
29
admin.go
29
admin.go
|
@ -15,7 +15,7 @@ type settingsData struct {
|
||||||
|
|
||||||
func settingsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func settingsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
|
@ -40,7 +40,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
|
|
||||||
func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
var isNew bool
|
var isNew bool
|
||||||
ids := strings.Split(mux.Vars(r)["ids"], "/")
|
ids := strings.Split(mux.Vars(r)["ids"], "/")
|
||||||
for _, idStr := range ids {
|
for _, idStr := range ids {
|
||||||
if idStr == "" {
|
if !bson.IsObjectIdHex(idStr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,14 +79,15 @@ func deleteHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func editHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func editHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if sess.User == "" {
|
idStr := mux.Vars(r)["id"]
|
||||||
http.NotFound(w, r)
|
if sess.User == "" || !bson.IsObjectIdHex(idStr) {
|
||||||
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id := bson.ObjectIdHex(mux.Vars(r)["id"])
|
id := bson.ObjectIdHex(idStr)
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": id})
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,12 +108,12 @@ func cleanEmptyStr(s []string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if sess.User == "" {
|
idStr := mux.Vars(r)["id"]
|
||||||
http.NotFound(w, r)
|
if sess.User == "" || !bson.IsObjectIdHex(idStr) {
|
||||||
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
idStr := mux.Vars(r)["id"]
|
|
||||||
id := bson.ObjectIdHex(idStr)
|
id := bson.ObjectIdHex(idStr)
|
||||||
title := r.FormValue("title")
|
title := r.FormValue("title")
|
||||||
publisher := r.FormValue("publisher")
|
publisher := r.FormValue("publisher")
|
||||||
|
@ -131,7 +132,7 @@ func saveHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
book["keywords"] = keywords(book)
|
book["keywords"] = keywords(book)
|
||||||
err := db.UpdateBook(id, book)
|
err := db.UpdateBook(id, book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,7 +161,7 @@ type newData struct {
|
||||||
|
|
||||||
func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,14 +204,14 @@ func newHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
|
|
||||||
func storeHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func storeHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var titles []string
|
var titles []string
|
||||||
ids := strings.Split(mux.Vars(r)["ids"], "/")
|
ids := strings.Split(mux.Vars(r)["ids"], "/")
|
||||||
for _, idStr := range ids {
|
for _, idStr := range ids {
|
||||||
if idStr == "" {
|
if !bson.IsObjectIdHex(idStr) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
cover.go
10
cover.go
|
@ -23,10 +23,14 @@ import (
|
||||||
|
|
||||||
func coverHandler(w http.ResponseWriter, r *http.Request) {
|
func coverHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
|
if !bson.IsObjectIdHex(vars["id"]) {
|
||||||
|
notFound(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
id := bson.ObjectIdHex(vars["id"])
|
id := bson.ObjectIdHex(vars["id"])
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": id})
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
||||||
if err != nil || len(books) == 0 {
|
if err != nil || len(books) == 0 {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
book := books[0]
|
book := books[0]
|
||||||
|
@ -34,7 +38,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if !book.Active {
|
if !book.Active {
|
||||||
sess := GetSession(r)
|
sess := GetSession(r)
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +52,7 @@ func coverHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error while opening image:", err)
|
log.Println("Error while opening image:", err)
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
25
reader.go
25
reader.go
|
@ -75,7 +75,9 @@ func getNextPrev(e *epubgo.Epub, file string, id string, base string) (string, s
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
prev = genLink(id, base, prev)
|
if prev != "" {
|
||||||
|
prev = genLink(id, base, prev)
|
||||||
|
}
|
||||||
if spine.Next() == nil {
|
if spine.Next() == nil {
|
||||||
next = genLink(id, base, spine.Url())
|
next = genLink(id, base, spine.Url())
|
||||||
}
|
}
|
||||||
|
@ -131,14 +133,14 @@ func readStartHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
id := mux.Vars(r)["id"]
|
id := mux.Vars(r)["id"]
|
||||||
e, _ := openReadEpub(w, r, sess)
|
e, _ := openReadEpub(w, r, sess)
|
||||||
if e == nil {
|
if e == nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer e.Close()
|
defer e.Close()
|
||||||
|
|
||||||
it, err := e.Spine()
|
it, err := e.Spine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/read/"+id+"/"+it.Url(), http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/read/"+id+"/"+it.Url(), http.StatusTemporaryRedirect)
|
||||||
|
@ -149,7 +151,7 @@ func readHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
file := mux.Vars(r)["file"]
|
file := mux.Vars(r)["file"]
|
||||||
e, book := openReadEpub(w, r, sess)
|
e, book := openReadEpub(w, r, sess)
|
||||||
if e == nil {
|
if e == nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer e.Close()
|
defer e.Close()
|
||||||
|
@ -172,6 +174,9 @@ func readHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
func openReadEpub(w http.ResponseWriter, r *http.Request, sess *Session) (*epubgo.Epub, Book) {
|
func openReadEpub(w http.ResponseWriter, r *http.Request, sess *Session) (*epubgo.Epub, Book) {
|
||||||
var book Book
|
var book Book
|
||||||
id := mux.Vars(r)["id"]
|
id := mux.Vars(r)["id"]
|
||||||
|
if !bson.IsObjectIdHex(id) {
|
||||||
|
return nil, book
|
||||||
|
}
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)})
|
books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)})
|
||||||
if err != nil || len(books) == 0 {
|
if err != nil || len(books) == 0 {
|
||||||
return nil, book
|
return nil, book
|
||||||
|
@ -194,33 +199,33 @@ func contentHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
id := vars["id"]
|
id := vars["id"]
|
||||||
file := vars["file"]
|
file := vars["file"]
|
||||||
if file == "" {
|
if file == "" || !bson.IsObjectIdHex(id) {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)})
|
books, _, err := db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)})
|
||||||
if err != nil || len(books) == 0 {
|
if err != nil || len(books) == 0 {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
book := books[0]
|
book := books[0]
|
||||||
if !book.Active {
|
if !book.Active {
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
e, err := OpenBook(book.File)
|
e, err := OpenBook(book.File)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer e.Close()
|
defer e.Close()
|
||||||
|
|
||||||
html, err := e.OpenFile(file)
|
html, err := e.OpenFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer html.Close()
|
defer html.Close()
|
||||||
|
|
15
stats.go
15
stats.go
|
@ -72,18 +72,21 @@ func appendMuxVars(vars map[string]string, stats map[string]interface{}) {
|
||||||
for key, value := range vars {
|
for key, value := range vars {
|
||||||
switch {
|
switch {
|
||||||
case key == "id":
|
case key == "id":
|
||||||
stats["id"] = bson.ObjectIdHex(value)
|
if bson.IsObjectIdHex(value) {
|
||||||
|
stats["id"] = bson.ObjectIdHex(value)
|
||||||
|
}
|
||||||
case key == "ids":
|
case key == "ids":
|
||||||
var objectIds []bson.ObjectId
|
var objectIds []bson.ObjectId
|
||||||
ids := strings.Split(value, "/")
|
ids := strings.Split(value, "/")
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
if id == "" {
|
if bson.IsObjectIdHex(value) {
|
||||||
continue
|
objectIds = append(objectIds, bson.ObjectIdHex(id))
|
||||||
}
|
}
|
||||||
objectIds = append(objectIds, bson.ObjectIdHex(id))
|
|
||||||
}
|
}
|
||||||
stats["ids"] = objectIds
|
if len(objectIds) > 0 {
|
||||||
stats["id"] = objectIds[0]
|
stats["ids"] = objectIds
|
||||||
|
stats["id"] = objectIds[0]
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
stats[key] = value
|
stats[key] = value
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@ func GetStatus(w http.ResponseWriter, r *http.Request) Status {
|
||||||
|
|
||||||
var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
var templates = template.Must(template.ParseFiles(TEMPLATE_PATH+"header.html",
|
||||||
TEMPLATE_PATH+"footer.html",
|
TEMPLATE_PATH+"footer.html",
|
||||||
|
TEMPLATE_PATH+"404.html",
|
||||||
TEMPLATE_PATH+"index.html",
|
TEMPLATE_PATH+"index.html",
|
||||||
TEMPLATE_PATH+"about.html",
|
TEMPLATE_PATH+"about.html",
|
||||||
TEMPLATE_PATH+"book.html",
|
TEMPLATE_PATH+"book.html",
|
||||||
|
|
12
templates/404.html
Normal file
12
templates/404.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{{template "header.html" .}}
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="span10 offset1">
|
||||||
|
<h4>Page not found</h4>
|
||||||
|
<p>
|
||||||
|
The requested page don't exist.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{template "footer.html"}}
|
33
trantor.go
33
trantor.go
|
@ -48,12 +48,18 @@ type bookData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
|
idStr := mux.Vars(r)["id"]
|
||||||
|
if !bson.IsObjectIdHex(idStr) {
|
||||||
|
notFound(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var data bookData
|
var data bookData
|
||||||
data.S = GetStatus(w, r)
|
data.S = GetStatus(w, r)
|
||||||
id := bson.ObjectIdHex(mux.Vars(r)["id"])
|
id := bson.ObjectIdHex(idStr)
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": id})
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
||||||
if err != nil || len(books) == 0 {
|
if err != nil || len(books) == 0 {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
db.IncVisit(id)
|
db.IncVisit(id)
|
||||||
|
@ -62,10 +68,16 @@ func bookHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
id := bson.ObjectIdHex(mux.Vars(r)["id"])
|
idStr := mux.Vars(r)["id"]
|
||||||
|
if !bson.IsObjectIdHex(idStr) {
|
||||||
|
notFound(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := bson.ObjectIdHex(idStr)
|
||||||
books, _, err := db.GetBooks(bson.M{"_id": id})
|
books, _, err := db.GetBooks(bson.M{"_id": id})
|
||||||
if err != nil || len(books) == 0 {
|
if err != nil || len(books) == 0 {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
book := books[0]
|
book := books[0]
|
||||||
|
@ -73,7 +85,7 @@ func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
if !book.Active {
|
if !book.Active {
|
||||||
sess := GetSession(r)
|
sess := GetSession(r)
|
||||||
if sess.User == "" {
|
if sess.User == "" {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +93,7 @@ func downloadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
fs := db.GetFS(FS_BOOKS)
|
fs := db.GetFS(FS_BOOKS)
|
||||||
f, err := fs.OpenId(book.File)
|
f, err := fs.OpenId(book.File)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
notFound(w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
@ -115,6 +127,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||||
loadTemplate(w, "index", data)
|
loadTemplate(w, "index", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func notFound(w http.ResponseWriter) {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
loadTemplate(w, "404", nil)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db = initDB()
|
db = initDB()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
@ -128,6 +145,10 @@ func main() {
|
||||||
|
|
||||||
func setUpRouter() {
|
func setUpRouter() {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
var notFoundHandler http.HandlerFunc
|
||||||
|
notFoundHandler = GatherStats(func(w http.ResponseWriter, r *http.Request, sess *Session) { notFound(w) })
|
||||||
|
r.NotFoundHandler = notFoundHandler
|
||||||
|
|
||||||
r.HandleFunc("/", GatherStats(indexHandler))
|
r.HandleFunc("/", GatherStats(indexHandler))
|
||||||
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", GatherStats(bookHandler))
|
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", GatherStats(bookHandler))
|
||||||
r.HandleFunc("/search/", GatherStats(searchHandler))
|
r.HandleFunc("/search/", GatherStats(searchHandler))
|
||||||
|
|
Reference in a new issue