Put the covers on the database
This commit is contained in:
parent
402dbabdbd
commit
986f834f46
10 changed files with 79 additions and 64 deletions
|
@ -19,7 +19,6 @@ const (
|
|||
NEW_ITEMS_PAGE = 50
|
||||
|
||||
TEMPLATE_PATH = "templates/"
|
||||
COVER_PATH = "cover/"
|
||||
CSS_PATH = "css/"
|
||||
JS_PATH = "js/"
|
||||
IMG_PATH = "img/"
|
||||
|
|
81
cover.go
81
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) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
33
store.go
33
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 {
|
||||
|
|
|
@ -25,7 +25,7 @@ function delBook(){
|
|||
<div class="row">
|
||||
{{if .Cover}}
|
||||
<div class="span4">
|
||||
<img src="/cover/{{.Cover}}" alt="{{.Title}}" class="pull-right" />
|
||||
<img src="/cover/{{.Id}}/big/{{.Title}}.jpg" alt="{{.Title}}" class="pull-right" />
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<div class="row">
|
||||
{{if .Cover}}
|
||||
<div class="span4">
|
||||
<img src="/cover/{{.Cover}}" alt="{{.Title}}" class="pull-right" />
|
||||
<img src="/cover/{{.Id}}/big/{{.Title}}.jpg" alt="{{.Title}}" class="pull-right" />
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
<li class="span2">
|
||||
<div class="thumbnail centered" style="border:none;">
|
||||
<a href="/book/{{.Id}}">
|
||||
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
|
||||
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
|
||||
<p><strong>{{.Title}}</strong></p>
|
||||
</a>
|
||||
</div>
|
||||
|
@ -35,7 +35,7 @@
|
|||
<li class="span2">
|
||||
<div class="thumbnail centered" style="border:none;">
|
||||
<a href="/book/{{.Id}}">
|
||||
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
|
||||
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
|
||||
<p><strong>{{.Title}}</strong></p>
|
||||
</a>
|
||||
</div>
|
||||
|
@ -55,7 +55,7 @@
|
|||
<li class="span2">
|
||||
<div class="thumbnail centered" style="border:none;">
|
||||
<a href="/book/{{.Id}}">
|
||||
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
|
||||
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" /></div>{{end}}
|
||||
<p><strong>{{.Title}}</strong></p>
|
||||
</a>
|
||||
</div>
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
{{with .B}}
|
||||
<div class="row">
|
||||
<div class="span1">
|
||||
<p class="pull-right">{{if .CoverSmall}}<img src="/cover/{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</p>
|
||||
<p class="pull-right">{{if .CoverSmall}}<img src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</p>
|
||||
</div>
|
||||
<div class="span9">
|
||||
<p><a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a> <small>({{$titleFound}})</small><br />
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
{{range .}}
|
||||
<div class="row">
|
||||
<div class="span1">
|
||||
<p class="pull-right"><a href="/book/{{.Id}}">{{if .CoverSmall}}<img class="img-rounded" src="/cover/{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p>
|
||||
<p class="pull-right"><a href="/book/{{.Id}}">{{if .CoverSmall}}<img class="img-rounded" src="/cover/{{.Id}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</a></p>
|
||||
</div>
|
||||
<div class="span10 well">
|
||||
<div class="row">
|
||||
|
|
10
trantor.go
10
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))
|
||||
|
|
Reference in a new issue