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

View file

@ -45,7 +45,7 @@ func ParseFile(path string) (string, error) {
return title, nil return title, nil
} }
func StoreFile(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(path)
if err != nil { if err != nil {
@ -63,7 +63,21 @@ func StoreFile(name string, file io.Reader) (string, error) {
return path, nil 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) 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) { func storeImg(img []byte, title, extension string) (string, string) {
folder := COVER_PATH + title[:1] folder := COVER_PATH + title[:1]
os.Mkdir(folder, os.ModePerm) os.Mkdir(folder, os.ModePerm)
imgPath := ValidFileName(folder, title, extension) imgPath := validFileName(folder, title, extension)
/* store img on disk */ /* store img on disk */
file, err := os.Create(imgPath) 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) resize := append(strings.Split(RESIZE_CMD, " "), imgPath, 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(folder, title, "_small"+extension)
resize = append(strings.Split(RESIZE_THUMB_CMD, " "), imgPath, imgPathSmall) resize = append(strings.Split(RESIZE_THUMB_CMD, " "), imgPath, imgPathSmall)
cmd = exec.Command(resize[0], resize[1:]...) cmd = exec.Command(resize[0], resize[1:]...)
cmd.Run() cmd.Run()

View file

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