Remove '/' from titles for use them on paths

This commit is contained in:
Las Zenow 2012-09-02 23:07:21 +02:00
parent f72f06c9a3
commit de0fd4227c
2 changed files with 20 additions and 17 deletions

View file

@ -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)

View file

@ -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()