Change the paths stored on the database

The paths in the database now are relative of the config BOOKS_PATH and COVER_PATH

For updating the database this query should be use:
for (var i = db.books.find(); i.hasNext(); ) {
        var book = i.next();
        db.books.update({_id: book["_id"]}, {$set: {path: book["path"].slice(6), cover: book["cover"].slice(7), coversmall: book["coversmall"].slice(7)}});
}
This commit is contained in:
Las Zenow 2012-10-28 20:21:46 +01:00
parent 34b48f411c
commit 93bd567f8d
9 changed files with 41 additions and 35 deletions

View file

@ -166,7 +166,7 @@ func newHandler(w http.ResponseWriter, r *http.Request) {
}
if len(r.URL.Path) > len("/new/") {
http.ServeFile(w, r, r.URL.Path[1:])
http.ServeFile(w, r, NEW_PATH + r.URL.Path[len("/new/"):])
return
}

View file

@ -125,6 +125,7 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
var data readData
data.Book = books[0]
var bookPath string
if !data.Book.Active {
sess := GetSession(r)
if sess.User == "" {
@ -132,10 +133,12 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
return
}
data.Back = "/new/"
bookPath = NEW_PATH + data.Book.Path
} else {
data.Back = "/book/" + id
bookPath = BOOKS_PATH + data.Book.Path
}
e, _ := epub.Open(data.Book.Path, 0)
e, _ := epub.Open(bookPath, 0)
defer e.Close()
if file == "" {
it := e.Iterator(epub.EITERATOR_LINEAR)
@ -158,14 +161,18 @@ func contentHandler(w http.ResponseWriter, r *http.Request) {
return
}
book := books[0]
var bookPath string
if !book.Active {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
bookPath = NEW_PATH + book.Path
} else {
bookPath = BOOKS_PATH + book.Path
}
e, _ := epub.Open(book.Path, 0)
e, _ := epub.Open(bookPath, 0)
defer e.Close()
if file == "" {
http.NotFound(w, r)

View file

@ -13,7 +13,7 @@ import (
func ParseFile(path string) (string, error) {
book := map[string]interface{}{}
e, err := epub.Open(path, 0)
e, err := epub.Open(NEW_PATH + path, 0)
if err != nil {
return "", err
}
@ -47,7 +47,7 @@ func ParseFile(path string) (string, error) {
func StoreNewFile(name string, file io.Reader) (string, error) {
path := storePath(name)
fw, err := os.Create(path)
fw, err := os.Create(NEW_PATH + path)
if err != nil {
return "", err
}
@ -65,14 +65,14 @@ func StoreNewFile(name string, file io.Reader) (string, error) {
func StoreBook(book Book) (path string, err error) {
title := book.Title
path = validFileName(BOOKS_PATH+title[:1], title, ".epub")
path = validFileName(BOOKS_PATH, title, ".epub")
oldPath := book.Path
oldPath := NEW_PATH+book.Path
err = os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm)
if err != nil {
return
}
cmd := exec.Command("mv", oldPath, path)
cmd := exec.Command("mv", oldPath, BOOKS_PATH+path)
err = cmd.Run()
return
}
@ -91,21 +91,21 @@ func validFileName(path string, title string, extension string) string {
title = strings.Replace(title, "/", "_", -1)
title = strings.Replace(title, "?", "_", -1)
title = strings.Replace(title, "#", "_", -1)
file := path + "/" + title + extension
_, err := os.Stat(file)
file := title[:1] + "/" + title + extension
_, err := os.Stat(path + file)
for i := 0; err == nil; i++ {
file = path + "/" + title + "_" + strconv.Itoa(i) + extension
_, err = os.Stat(file)
file = title[:1] + "/" + title + "_" + strconv.Itoa(i) + extension
_, err = os.Stat(path + file)
}
return file
}
func storePath(name string) string {
path := NEW_PATH + name
_, err := os.Stat(path)
path := name
_, err := os.Stat(NEW_PATH + path)
for i := 0; err == nil; i++ {
path = NEW_PATH + strconv.Itoa(i) + "_" + name
_, err = os.Stat(path)
path = strconv.Itoa(i) + "_" + name
_, err = os.Stat(NEW_PATH + path)
}
return path
}
@ -120,12 +120,11 @@ func cleanStr(str string) string {
}
func storeImg(img []byte, title, extension string) (string, string) {
folder := COVER_PATH + title[:1]
os.Mkdir(folder, os.ModePerm)
imgPath := validFileName(folder, title, extension)
os.Mkdir(COVER_PATH + title[:1], os.ModePerm)
imgPath := validFileName(COVER_PATH, title, extension)
/* store img on disk */
file, err := os.Create(imgPath)
file, err := os.Create(COVER_PATH + imgPath)
if err != nil {
return "", ""
}
@ -133,14 +132,14 @@ func storeImg(img []byte, title, extension string) (string, string) {
file.Write(img)
/* resize img */
resize := append(strings.Split(RESIZE_CMD, " "), imgPath, imgPath)
resize := append(strings.Split(RESIZE_CMD, " "), COVER_PATH + imgPath, COVER_PATH + imgPath)
cmd := exec.Command(resize[0], resize[1:]...)
cmd.Run()
imgPathSmall := validFileName(folder, title, "_small"+extension)
resize = append(strings.Split(RESIZE_THUMB_CMD, " "), imgPath, imgPathSmall)
imgPathSmall := validFileName(COVER_PATH, title, "_small"+extension)
resize = append(strings.Split(RESIZE_THUMB_CMD, " "), COVER_PATH + imgPath, COVER_PATH + imgPathSmall)
cmd = exec.Command(resize[0], resize[1:]...)
cmd.Run()
return "/" + imgPath, "/" + imgPathSmall
return imgPath, imgPathSmall
}
func getCover(e *epub.Epub, title string) (string, string) {

View file

@ -25,7 +25,7 @@ function delBook(){
<div class="row">
{{if .Cover}}
<div class="span4">
<img src="{{.Cover}}" alt="{{.Title}}" class="pull-right" />
<img src="/cover/{{.Cover}}" alt="{{.Title}}" class="pull-right" />
</div>
{{end}}
@ -53,7 +53,7 @@ function delBook(){
{{end}}
<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="/books/{{.Path}}" class="btn btn-large btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.Id}}" class="btn btn-large btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>

View file

@ -4,7 +4,7 @@
<div class="row">
{{if .Cover}}
<div class="span4">
<img src="{{.Cover}}" alt="{{.Title}}" class="pull-right" />
<img src="/cover/{{.Cover}}" alt="{{.Title}}" class="pull-right" />
</div>
{{end}}

View file

@ -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="{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.CoverSmall}}" 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="{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.CoverSmall}}" 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="{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
{{if .CoverSmall}}<div class="down"><img class="img-rounded" src="/cover/{{.CoverSmall}}" alt="{{.Title}}" /></div>{{end}}
<p><strong>{{.Title}}</strong></p>
</a>
</div>

View file

@ -14,7 +14,7 @@
{{with .B}}
<div class="row">
<div class="span1">
<p class="pull-right">{{if .CoverSmall}}<img src="{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</p>
<p class="pull-right">{{if .CoverSmall}}<img src="/cover/{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</p>
</div>
<div class="span9">
<p><a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a> <small>({{$titleFound}})</small><br />
@ -34,7 +34,7 @@
</div>
<div class="row"><p></p></div>
<div class="row btn-group pull-right">
<a href="/{{.Path}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/new/{{.Path}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.Id}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>

View file

@ -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="{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p>
<p class="pull-right"><a href="/book/{{.Id}}">{{if .CoverSmall}}<img class="img-rounded" src="/cover/{{.CoverSmall}}" alt="{{.Title}}" />{{end}}</a></p>
</div>
<div class="span10 well">
<div class="row">
@ -33,7 +33,7 @@
</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="/books/{{.Path}}" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
<a href="/read/{{.Id}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div>
</div>

View file

@ -43,7 +43,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
for _, path := range paths {
title, err := ParseFile(path)
if err != nil {
os.Remove(path)
os.Remove(NEW_PATH + path)
sess.Notify("Problem uploading!", "The file '"+path[len("new/"):]+"' is not a well formed epub", "error")
} else {
uploaded = uploaded + " '" + title + "'"