Refactor the admin/store code

This commit is contained in:
Las Zenow 2012-10-28 17:22:23 +01:00
parent 85111946ed
commit 2e043af31a
3 changed files with 24 additions and 14 deletions

View file

@ -4,7 +4,6 @@ import (
"labix.org/v2/mgo/bson"
"net/http"
"os"
"os/exec"
"strings"
)
@ -182,14 +181,11 @@ func storeHandler(w http.ResponseWriter, r *http.Request) {
return
}
book := books[0]
title := book.Title
path := ValidFileName(BOOKS_PATH+title[:1], title, ".epub")
oldPath := book.Path
os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm)
cmd := exec.Command("mv", oldPath, path)
cmd.Run()
path, err := StoreBook(book)
if err != nil {
sess.Notify("An error ocurred!", err.Error(), "error")
return
}
db.UpdateBook(id, bson.M{"active": true, "path": path})
titles = append(titles, book.Title)
}

View file

@ -45,7 +45,7 @@ func ParseFile(path string) (string, error) {
return title, nil
}
func StoreFile(name string, file io.Reader) (string, error) {
func StoreNewFile(name string, file io.Reader) (string, error) {
path := storePath(name)
fw, err := os.Create(path)
if err != nil {
@ -63,7 +63,21 @@ func StoreFile(name string, file io.Reader) (string, error) {
return path, nil
}
func ValidFileName(path string, title string, extension string) string {
func StoreBook(book Book) (path string, err error) {
title := book.Title
path = validFileName(BOOKS_PATH+title[:1], title, ".epub")
oldPath := book.Path
err = os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm)
if err != nil {
return
}
cmd := exec.Command("mv", oldPath, path)
err = cmd.Run()
return
}
func validFileName(path string, title string, extension string) string {
title = strings.Replace(title, "/", "_", -1)
title = strings.Replace(title, "?", "_", -1)
title = strings.Replace(title, "#", "_", -1)
@ -98,7 +112,7 @@ 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)
imgPath := validFileName(folder, title, extension)
/* store img on disk */
file, err := os.Create(imgPath)
@ -112,7 +126,7 @@ func storeImg(img []byte, title, extension string) (string, string) {
resize := append(strings.Split(RESIZE_CMD, " "), imgPath, imgPath)
cmd := exec.Command(resize[0], resize[1:]...)
cmd.Run()
imgPathSmall := ValidFileName(folder, title, "_small"+extension)
imgPathSmall := validFileName(folder, title, "_small"+extension)
resize = append(strings.Split(RESIZE_THUMB_CMD, " "), imgPath, imgPathSmall)
cmd = exec.Command(resize[0], resize[1:]...)
cmd.Run()

View file

@ -18,7 +18,7 @@ func storeFiles(r *http.Request) ([]string, error) {
}
defer file.Close()
path, err := StoreFile(f.Filename, file)
path, err := StoreNewFile(f.Filename, file)
if err != nil {
return paths, err
}