From 88b4809e99bc3d2dfcdb2be7d62c997f8b1a6b76 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 21 Aug 2012 21:22:56 +0200 Subject: [PATCH] Use Ids for address no titles --- reader.go | 20 ++++++++++---------- templates/book.html | 2 +- templates/edit.html | 2 +- templates/index.html | 4 ++-- templates/new.html | 2 +- templates/search.html | 6 +++--- trantor.go | 6 +++++- 7 files changed, 23 insertions(+), 19 deletions(-) diff --git a/reader.go b/reader.go index 82ef247..6e3bb54 100644 --- a/reader.go +++ b/reader.go @@ -23,14 +23,14 @@ func parseUrl(url string) (string, string, string, string) { exp, _ := regexp.Compile("^(\\/read[^\\/]*\\/)([^\\/]*)\\/?(.*\\.([^\\.]*))?$") res := exp.FindStringSubmatch(url) base := res[1] - title := res[2] + id := res[2] file := "" ext := "" if len(res) == 5 { file = res[3] ext = res[4] } - return base, title, file, ext + return base, id, file, ext } func cleanHtml(html string) string { @@ -47,7 +47,7 @@ func cleanHtml(html string) string { } /* return next and prev urls from document */ -func nextPrev(e *epub.Epub, file string, title string, base string) (string, string) { +func nextPrev(e *epub.Epub, file string, id string, base string) (string, string) { it := e.Iterator(epub.EITERATOR_LINEAR) defer it.Close() @@ -67,18 +67,18 @@ func nextPrev(e *epub.Epub, file string, title string, base string) (string, str } } if prev != "" { - prev = base + title + "/" + prev + prev = base + id + "/" + prev } if next != "" { - next = base + title + "/" + next + next = base + id + "/" + next } return next, prev } func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - base, title, file, ext := parseUrl(r.URL.Path) - books, _, err := GetBook(coll, bson.M{"title": title}) + base, id, file, ext := parseUrl(r.URL.Path) + books, _, err := GetBook(coll, bson.M{"_id": bson.ObjectIdHex(id)}) if err != nil || len(books) == 0 { http.NotFound(w, r) return @@ -89,7 +89,7 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) if file == "" { it := e.Iterator(epub.EITERATOR_LINEAR) defer it.Close() - http.Redirect(w, r, base + title + "/" + it.CurrUrl(), 307) + http.Redirect(w, r, base + id + "/" + it.CurrUrl(), 307) return } @@ -97,11 +97,11 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) var data readData data.S = GetStatus(w, r) data.Book = book - data.Next, data.Prev = nextPrev(e, file, title, base) + data.Next, data.Prev = nextPrev(e, file, id, base) if base == "/readnew/" { data.Back = "/new/" } else { - data.Back = "/book/" + title + data.Back = "/book/" + id } page := string(e.Data(file)) data.Txt = template.HTML(cleanHtml(page)) diff --git a/templates/book.html b/templates/book.html index 7fdb540..5da151d 100644 --- a/templates/book.html +++ b/templates/book.html @@ -54,7 +54,7 @@ function delBook(){
diff --git a/templates/edit.html b/templates/edit.html index 8b7a49f..6600dca 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -68,7 +68,7 @@
- Cancel + Cancel
diff --git a/templates/index.html b/templates/index.html index 6d0c987..95ff344 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,7 +13,7 @@ {{with .Books}} {{range .}}
-

{{if .CoverSmall}}{{.Title}}{{end}}

+

{{if .CoverSmall}}{{.Title}}{{end}}

{{end}} {{end}} @@ -22,7 +22,7 @@ {{with .Books}} {{range .}}
-

{{.Title}}

+

{{.Title}}

{{end}} {{end}} diff --git a/templates/new.html b/templates/new.html index ca5797d..981c622 100644 --- a/templates/new.html +++ b/templates/new.html @@ -20,7 +20,7 @@

diff --git a/templates/search.html b/templates/search.html index dfe7c2c..3e0b0c7 100644 --- a/templates/search.html +++ b/templates/search.html @@ -19,18 +19,18 @@ {{range .}}
-

{{.Title}}
+

{{.Title}}
{{range .Author}}{{.}}, {{end}}

diff --git a/trantor.go b/trantor.go index b2fab9e..9344618 100644 --- a/trantor.go +++ b/trantor.go @@ -66,7 +66,8 @@ func bookHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) return func(w http.ResponseWriter, r *http.Request) { var data bookData data.S = GetStatus(w, r) - books, _, err := GetBook(coll, bson.M{"title": r.URL.Path[len("/book/"):]}) + id := bson.ObjectIdHex(r.URL.Path[len("/book/"):]) + books, _, err := GetBook(coll, bson.M{"_id": id}) if err != nil || len(books) == 0 { http.NotFound(w, r) return @@ -114,6 +115,9 @@ func indexHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) data.S.Home = true data.Count, _ = coll.Count() coll.Find(bson.M{}).Sort("-_id").Limit(6).All(&data.Books) + for i, b := range data.Books { + data.Books[i].Id = bson.ObjectId(b.Id).Hex() + } loadTemplate(w, "index", data) } }