diff --git a/config.go b/config.go index 3e8daa2..cb5df16 100644 --- a/config.go +++ b/config.go @@ -19,7 +19,6 @@ const ( NEW_ITEMS_PAGE = 50 TEMPLATE_PATH = "templates/" - COVER_PATH = "cover/" CSS_PATH = "css/" JS_PATH = "js/" IMG_PATH = "img/" diff --git a/cover.go b/cover.go index 8fee4d1..47630a6 100644 --- a/cover.go +++ b/cover.go @@ -3,19 +3,59 @@ package main import ( "bytes" "git.gitorious.org/go-pkg/epubgo.git" + "github.com/gorilla/mux" "github.com/nfnt/resize" "image" "image/jpeg" "io" "io/ioutil" + "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" "log" - "os" + "net/http" "regexp" "strings" - "unicode/utf8" ) -func GetCover(e *epubgo.Epub, title string) (string, string) { +func coverHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + id := bson.ObjectIdHex(vars["id"]) + books, _, err := db.GetBooks(bson.M{"_id": id}) + if err != nil || len(books) == 0 { + http.NotFound(w, r) + return + } + book := books[0] + + if !book.Active { + sess := GetSession(r) + if sess.User == "" { + http.NotFound(w, r) + return + } + } + + fs := db.GetFS(FS_IMGS) + var f *mgo.GridFile + if vars["size"] == "small" { + f, err = fs.OpenId(book.CoverSmall) + } else { + f, err = fs.OpenId(book.Cover) + } + if err != nil { + log.Println("Error while opening image:", err) + http.NotFound(w, r) + return + } + defer f.Close() + + headers := w.Header() + headers["Content-Type"] = []string{"image/jpeg"} + + io.Copy(w, f) +} + +func GetCover(e *epubgo.Epub, title string) (bson.ObjectId, bson.ObjectId) { imgPath, smallPath := searchCommonCoverNames(e, title) if imgPath != "" { return imgPath, smallPath @@ -65,7 +105,7 @@ func GetCover(e *epubgo.Epub, title string) (string, string) { return "", "" } -func searchCommonCoverNames(e *epubgo.Epub, title string) (string, string) { +func searchCommonCoverNames(e *epubgo.Epub, title string) (bson.ObjectId, bson.ObjectId) { for _, p := range []string{"cover.jpg", "Images/cover.jpg", "cover.jpeg", "cover1.jpg", "cover1.jpeg"} { img, err := e.OpenFile(p) if err == nil { @@ -76,30 +116,20 @@ func searchCommonCoverNames(e *epubgo.Epub, title string) (string, string) { return "", "" } -func storeImg(img io.Reader, title, extension string) (string, string) { - r, _ := utf8.DecodeRuneInString(title) - folder := string(r) - if _, err := os.Stat(COVER_PATH + folder); err != nil { - err = os.Mkdir(COVER_PATH+folder, os.ModePerm) - if err != nil { - log.Println("Error creating", COVER_PATH+folder, ":", err.Error()) - return "", "" - } - } - +func storeImg(img io.Reader, title, extension string) (bson.ObjectId, bson.ObjectId) { /* open the files */ - imgPath := validFileName(COVER_PATH, title, extension) - fBig, err := os.Create(COVER_PATH + imgPath) + imgPath := title + extension + fBig, err := createCoverFile(imgPath) if err != nil { - log.Println("Error creating", COVER_PATH+imgPath, ":", err.Error()) + log.Println("Error creating", imgPath, ":", err.Error()) return "", "" } defer fBig.Close() - imgPathSmall := validFileName(COVER_PATH, title, "_small"+extension) - fSmall, err := os.Create(COVER_PATH + imgPathSmall) + imgPathSmall := title + "_small" + extension + fSmall, err := createCoverFile(imgPathSmall) if err != nil { - log.Println("Error creating", COVER_PATH+imgPathSmall, ":", err.Error()) + log.Println("Error creating", imgPathSmall, ":", err.Error()) return "", "" } defer fSmall.Close() @@ -129,7 +159,14 @@ func storeImg(img io.Reader, title, extension string) (string, string) { return "", "" } - return imgPath, imgPathSmall + idBig, _ := fBig.Id().(bson.ObjectId) + idSmall, _ := fSmall.Id().(bson.ObjectId) + return idBig, idSmall +} + +func createCoverFile(name string) (*mgo.GridFile, error) { + fs := db.GetFS(FS_IMGS) + return fs.Create(name) } func resizeImg(imgReader io.Reader, width uint) (image.Image, error) { diff --git a/database.go b/database.go index 3d4062c..9dd9127 100644 --- a/database.go +++ b/database.go @@ -32,8 +32,8 @@ type Book struct { Rights string Meta string File bson.ObjectId - Cover string - CoverSmall string + Cover bson.ObjectId + CoverSmall bson.ObjectId Active bool Keywords []string } diff --git a/store.go b/store.go index 43b51b0..f2b42cf 100644 --- a/store.go +++ b/store.go @@ -6,11 +6,8 @@ import ( "io" "io/ioutil" "labix.org/v2/mgo/bson" - "os" "regexp" - "strconv" "strings" - "unicode/utf8" ) func ParseFile(id bson.ObjectId) (string, error) { @@ -94,29 +91,19 @@ func DeleteFile(id bson.ObjectId) error { return fs.RemoveId(id) } -func DeleteBook(book Book) { - if book.Cover != "" { - os.RemoveAll(book.Cover[1:]) - } - if book.CoverSmall != "" { - os.RemoveAll(book.CoverSmall[1:]) - } - DeleteFile(book.File) +func DeleteCover(id bson.ObjectId) error { + fs := db.GetFS(FS_IMGS) + return fs.RemoveId(id) } -func validFileName(path string, title string, extension string) string { - title = strings.Replace(title, "/", "_", -1) - title = strings.Replace(title, "?", "_", -1) - title = strings.Replace(title, "#", "_", -1) - r, _ := utf8.DecodeRuneInString(title) - folder := string(r) - file := folder + "/" + title + extension - _, err := os.Stat(path + file) - for i := 0; err == nil; i++ { - file = folder + "/" + title + "_" + strconv.Itoa(i) + extension - _, err = os.Stat(path + file) +func DeleteBook(book Book) { + if book.Cover != "" { + DeleteCover(book.Cover) } - return file + if book.CoverSmall != "" { + DeleteCover(book.CoverSmall) + } + DeleteFile(book.File) } func cleanStr(str string) string { diff --git a/templates/book.html b/templates/book.html index 725e940..0ce8383 100644 --- a/templates/book.html +++ b/templates/book.html @@ -25,7 +25,7 @@ function delBook(){
{{if .Cover}}
- {{.Title}} + {{.Title}}
{{end}} diff --git a/templates/edit.html b/templates/edit.html index fbad606..90186d7 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -4,7 +4,7 @@
{{if .Cover}}
- {{.Title}} + {{.Title}}
{{end}} diff --git a/templates/index.html b/templates/index.html index cea350c..e2fb587 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,7 +15,7 @@
  • @@ -35,7 +35,7 @@
  • @@ -55,7 +55,7 @@
  • diff --git a/templates/new.html b/templates/new.html index 8e606e4..5a3a5c9 100644 --- a/templates/new.html +++ b/templates/new.html @@ -26,7 +26,7 @@ {{with .B}}
    -

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

    +

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

    {{.Title}} ({{$titleFound}})
    diff --git a/templates/search.html b/templates/search.html index cf905fe..ce0de8b 100644 --- a/templates/search.html +++ b/templates/search.html @@ -19,7 +19,7 @@ {{range .}}

    diff --git a/trantor.go b/trantor.go index c0c35a9..4933e9c 100644 --- a/trantor.go +++ b/trantor.go @@ -6,7 +6,6 @@ import ( "labix.org/v2/mgo/bson" "log" "net/http" - "os" ) type aboutData struct { @@ -122,12 +121,6 @@ func main() { db = initDB() defer db.Close() - /* create the needed folders */ - _, err := os.Stat(COVER_PATH) - if err != nil { - os.Mkdir(COVER_PATH, os.ModePerm) - } - /* set up web handlers */ r := mux.NewRouter() r.HandleFunc("/", indexHandler) @@ -146,11 +139,10 @@ func main() { r.HandleFunc("/save/{id:[0-9a-fA-F]+}", saveHandler).Methods("POST") r.HandleFunc("/about/", aboutHandler) r.HandleFunc("/books/{id:[0-9a-fA-F]+}", downloadHandler) + r.HandleFunc("/cover/{id:[0-9a-fA-F]+}/{size}/{img:.*}", coverHandler) r.HandleFunc("/settings/", settingsHandler) h := http.FileServer(http.Dir(IMG_PATH)) r.Handle("/img/{img}", http.StripPrefix("/img/", h)) - h = http.FileServer(http.Dir(COVER_PATH)) - r.Handle("/cover/{c}/{img}", http.StripPrefix("/cover/", h)) h = http.FileServer(http.Dir(CSS_PATH)) r.Handle("/css/{css}", http.StripPrefix("/css/", h)) h = http.FileServer(http.Dir(JS_PATH))