From de0fd4227c2202727911e6709712d94d561df8ee Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sun, 2 Sep 2012 23:07:21 +0200 Subject: [PATCH] Remove '/' from titles for use them on paths --- admin.go | 19 +++++++++++++------ upload.go | 18 +++++++----------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/admin.go b/admin.go index 9950069..0262cca 100644 --- a/admin.go +++ b/admin.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "strconv" + "strings" ) func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, *http.Request) { @@ -137,6 +138,17 @@ func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { } } +func ValidFileName(path string, title string, extension string) string { + title = strings.Replace(title, "/", "_", -1) + file := path + "/" + title + extension + _, err := os.Stat(file) + for i := 0; err == nil; i++ { + file := path + "/" + title + "_" + strconv.Itoa(i) + extension + _, err = os.Stat(file) + } + return file +} + func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { sess := GetSession(r) @@ -154,12 +166,7 @@ func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http } title, _ := book["title"].(string) - path := BOOKS_PATH + title[:1] + "/" + title + ".epub" - _, err = os.Stat(path) - for i := 0; err == nil; i++ { - path := BOOKS_PATH + title[:1] + "/" + title + "_" + strconv.Itoa(i) + ".epub" - _, err = os.Stat(path) - } + path := ValidFileName(BOOKS_PATH + title[:1], title, ".epub") oldPath, _ := book["path"].(string) os.Mkdir(BOOKS_PATH+title[:1], os.ModePerm) diff --git a/upload.go b/upload.go index fae530d..a799df9 100644 --- a/upload.go +++ b/upload.go @@ -61,19 +61,15 @@ func cleanStr(str string) string { } func storeImg(img []byte, title, extension string) (string, string) { - name := title - folder := COVER_PATH + name[:1] + "/" + folder := COVER_PATH + title[:1] os.Mkdir(folder, os.ModePerm) - imgPath := folder + name + extension - _, err := os.Stat(imgPath) - for i := 0; err == nil; i++ { - name = title + "_" + strconv.Itoa(i) - imgPath = folder + name + extension - _, err = os.Stat(imgPath) - } + imgPath := ValidFileName(folder, title, extension) /* store img on disk */ - file, _ := os.Create(imgPath) + file, err := os.Create(imgPath) + if err != nil { + return "", "" + } defer file.Close() file.Write(img) @@ -81,7 +77,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 := folder + name + "_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()