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(){ <div class="row"> <div class="btn-group pull-right"> <a href="/{{.Path}}" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a> - <a href="/read/{{.Title}}" 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> 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 @@ </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">Save</button> - <a href="/book/{{.Title}}" class="btn">Cancel</a> + <a href="/book/{{.Id}}" class="btn">Cancel</a> </div> </fieldset> </form> 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 .}} <div class="span2"> - <p class="centered down"><a href="/book/{{.Title}}">{{if .CoverSmall}}<img src="{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p> + <p class="centered down"><a href="/book/{{.Id}}">{{if .CoverSmall}}<img src="{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p> </div> {{end}} {{end}} @@ -22,7 +22,7 @@ {{with .Books}} {{range .}} <div class="span2"> - <p class="centered"><a href="/book/{{.Title}}"><strong>{{.Title}}</strong></a></p> + <p class="centered"><a href="/book/{{.Id}}"><strong>{{.Title}}</strong></a></p> </div> {{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 @@ </div> <div class="span2"> <div class="row"> - <p><a href="/readnew/{{.Title}}" class="btn btn-warning pull-right"><i class="icon-eye-open icon-white"></i> read it!</a> + <p><a href="/readnew/{{.Id}}" class="btn btn-warning pull-right"><i class="icon-eye-open icon-white"></i> read it!</a> <a href="/{{.Path}}" class="btn btn-inverse pull-right"><i class="icon-download-alt icon-white"></i> download</a></p> </div> <div class="row"><p></p></div> 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 .}} <div class="row"> <div class="span1"> - <p class="pull-right"><a href="/book/{{.Title}}">{{if .CoverSmall}}<img src="{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p> + <p class="pull-right"><a href="/book/{{.Id}}">{{if .CoverSmall}}<img src="{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p> </div> <div class="span10 well"> <div class="row"> <div class="span7"> - <p><a href="/book/{{.Title}}"><strong>{{.Title}}</strong></a><br /> + <p><a href="/book/{{.Id}}"><strong>{{.Title}}</strong></a><br /> {{range .Author}}{{.}}, {{end}}</p> </div> <div class="span3"> <div class="btn-group pull-right"> <a href="/{{.Path}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a> - <a href="/read/{{.Title}}" 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> 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) } }