diff --git a/.gitignore b/.gitignore index 7adb2e4..2e02529 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ books/ +cover/ img/ trantor .*.swp +upload/upload +upload/books +upload/cover diff --git a/book.html b/book.html new file mode 100644 index 0000000..eaffd3b --- /dev/null +++ b/book.html @@ -0,0 +1,10 @@ +

{{.Title}}

+

{{.Description}}

+{{.Title}} + diff --git a/database.go b/database.go index 0ec10f8..ce41a3d 100644 --- a/database.go +++ b/database.go @@ -1,9 +1,23 @@ package main type Book struct { - Title []string - Creator []string - Subject []string - Lang []string - Path string + Title string + Author []string + Contributor string + Publisher string + Description string + Subject []string + Date string + Lang []string + Type string + Format string + Source string + Relation string + Coverage string + Rights string + Meta string + Path string + Cover string + CoverSmall string + Keywords []string } diff --git a/foot.html b/foot.html new file mode 100644 index 0000000..301b859 --- /dev/null +++ b/foot.html @@ -0,0 +1,5 @@ + + + diff --git a/front.html b/front.html new file mode 100644 index 0000000..72fff32 --- /dev/null +++ b/front.html @@ -0,0 +1,6 @@ +

Imperial Library of Trantor

+
+ + +
+

library

diff --git a/head.html b/head.html new file mode 100644 index 0000000..c786647 --- /dev/null +++ b/head.html @@ -0,0 +1,5 @@ + + + Imperial Library of Trantor + + diff --git a/search.go b/search.go new file mode 100644 index 0000000..0453c1c --- /dev/null +++ b/search.go @@ -0,0 +1,35 @@ +package main + +import ( + "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" + "net/http" + "strings" +) + +func buildQuery(q string) bson.M { + words := strings.Split(q, " ") + reg := make([]bson.RegEx, len(words)) + for i, w := range words { + reg[i].Pattern = w + reg[i].Options = "i" + } + return bson.M{"keywords": bson.M{"$all": reg}} +} + +type searchData struct { + Search string + Books []Book +} + +func searchHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + req := strings.Join(r.Form["q"], " ") + var res []Book + coll.Find(buildQuery(req)).All(&res) + loadTemplate(w, "search", searchData{req, res}) +} diff --git a/search.html b/search.html new file mode 100644 index 0000000..60c4c89 --- /dev/null +++ b/search.html @@ -0,0 +1,13 @@ +

Search

+
+ + +
+ + diff --git a/trantor.go b/trantor.go new file mode 100644 index 0000000..318f166 --- /dev/null +++ b/trantor.go @@ -0,0 +1,63 @@ +package main + +import ( + "html/template" + "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" + "net/http" +) + +const ( + IP = "127.0.0.1" + DB_NAME = "trantor" + BOOKS_COLL = "books" +) + +func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { + // TODO: when finish devel conver to global: + var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html")) + + err := templates.ExecuteTemplate(w, "head.html", nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = templates.ExecuteTemplate(w, tmpl+".html", data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = templates.ExecuteTemplate(w, "foot.html", nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + +func bookHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) { + var book Book + coll.Find(bson.M{"title": r.URL.Path[len("/book/"):]}).One(&book) + loadTemplate(w, "book", book) +} + + +func sendFile(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path[1:] + http.ServeFile(w, r, path) +} + +func main() { + session, err := mgo.Dial(IP) + if err != nil { + panic(err) + } + defer session.Close() + coll := session.DB(DB_NAME).C(BOOKS_COLL) + + http.HandleFunc("/book/", func(w http.ResponseWriter, r *http.Request) { bookHandler(coll, w, r) }) + http.HandleFunc("/search/", func(w http.ResponseWriter, r *http.Request) { searchHandler(coll, w, r) }) + http.HandleFunc("/img/", sendFile) + http.HandleFunc("/cover/", sendFile) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { loadTemplate(w, "front", nil) }) + http.ListenAndServe(":8080", nil) +} diff --git a/upload.go b/upload.go deleted file mode 100644 index dc1d594..0000000 --- a/upload.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "fmt" - "os" - "git.gitorious.org/go-pkg/epub.git" - "labix.org/v2/mgo" -) - -const ( - IP = "127.0.0.1" - DB_NAME = "trantor" - BOOKS_COLL = "books" - PATH = "./books/" -) - - -func store(coll *mgo.Collection, path string) { - var book Book - - e, err := epub.Open(path, 0) - if err != nil { - fmt.Println(path) - panic(err) // TODO: do something - } - defer e.Close() - - // TODO: do it for all metadata - book.Title = e.Metadata(epub.EPUB_TITLE) - book.Creator = e.Metadata(epub.EPUB_CREATOR) - book.Subject = e.Metadata(epub.EPUB_SUBJECT) - book.Lang = e.Metadata(epub.EPUB_LANG) - book.Path = path - - coll.Insert(book) -} - - -func main() { - session, err := mgo.Dial(IP) - if err != nil { - panic(err) // TODO: do something - } - defer session.Close() - coll := session.DB(DB_NAME).C(BOOKS_COLL) - - f, err := os.Open(PATH) - if err != nil { - fmt.Println(PATH) - panic(err) // TODO: do something - } - names, err := f.Readdirnames(0) - if err != nil { - panic(err) // TODO: do something - } - - for _, name := range names { - store(coll, PATH + name) - } -} diff --git a/upload/database.go b/upload/database.go new file mode 100644 index 0000000..ce41a3d --- /dev/null +++ b/upload/database.go @@ -0,0 +1,23 @@ +package main + +type Book struct { + Title string + Author []string + Contributor string + Publisher string + Description string + Subject []string + Date string + Lang []string + Type string + Format string + Source string + Relation string + Coverage string + Rights string + Meta string + Path string + Cover string + CoverSmall string + Keywords []string +} diff --git a/upload/upload.go b/upload/upload.go new file mode 100644 index 0000000..c82c35f --- /dev/null +++ b/upload/upload.go @@ -0,0 +1,142 @@ +package main + +import ( + "fmt" + "git.gitorious.org/go-pkg/epub.git" + "labix.org/v2/mgo" + "os" + "os/exec" + "regexp" + "strings" +) + +const ( + IP = "127.0.0.1" + DB_NAME = "trantor" + BOOKS_COLL = "books" + PATH = "books/" + COVER_PATH = "cover/" + RESIZE = "/usr/bin/convert -resize 300 -quality 60 " + RESIZE_THUMB = "/usr/bin/convert -resize 60 -quality 60 " +) + +func getCover(e *epub.Epub, path string) (string, string) { + exp, _ := regexp.Compile("