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/") { 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 return
} }

View file

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

View file

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

View file

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

View file

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

View file

@ -15,7 +15,7 @@
<li class="span2"> <li class="span2">
<div class="thumbnail centered" style="border:none;"> <div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}"> <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> <p><strong>{{.Title}}</strong></p>
</a> </a>
</div> </div>
@ -35,7 +35,7 @@
<li class="span2"> <li class="span2">
<div class="thumbnail centered" style="border:none;"> <div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}"> <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> <p><strong>{{.Title}}</strong></p>
</a> </a>
</div> </div>
@ -55,7 +55,7 @@
<li class="span2"> <li class="span2">
<div class="thumbnail centered" style="border:none;"> <div class="thumbnail centered" style="border:none;">
<a href="/book/{{.Id}}"> <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> <p><strong>{{.Title}}</strong></p>
</a> </a>
</div> </div>

View file

@ -14,7 +14,7 @@
{{with .B}} {{with .B}}
<div class="row"> <div class="row">
<div class="span1"> <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>
<div class="span9"> <div class="span9">
<p><a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a> <small>({{$titleFound}})</small><br /> <p><a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a> <small>({{$titleFound}})</small><br />
@ -34,7 +34,7 @@
</div> </div>
<div class="row"><p></p></div> <div class="row"><p></p></div>
<div class="row btn-group pull-right"> <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> <a href="/read/{{.Id}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div> </div>
</div> </div>

View file

@ -19,7 +19,7 @@
{{range .}} {{range .}}
<div class="row"> <div class="row">
<div class="span1"> <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>
<div class="span10 well"> <div class="span10 well">
<div class="row"> <div class="row">
@ -33,7 +33,7 @@
</div> </div>
<div class="span3"> <div class="span3">
<div class="btn-group pull-right"> <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> <a href="/read/{{.Id}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
</div> </div>
</div> </div>

View file

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