diff --git a/server.go b/server.go index 9d179a6..5145e0b 100644 --- a/server.go +++ b/server.go @@ -24,7 +24,7 @@ func main() { flag.StringVar(&Config.bind, "b", "127.0.0.1:8080", "host to bind to (default: 127.0.0.1:8080)") flag.StringVar(&Config.filesDir, "filespath", "files/", - "path to files directory (default: files/)") + "path to files directory (including trailing slash)") flag.BoolVar(&Config.noLogs, "nologs", false, "remove stdout output for each request") flag.StringVar(&Config.siteName, "sitename", "linx", @@ -37,6 +37,8 @@ func main() { goji.Abandon(middleware.Logger) } + // check trailing slashes + // Template Globals pongo2.DefaultSet.Globals["sitename"] = Config.siteName diff --git a/upload.go b/upload.go index 6cd9582..eaafa66 100644 --- a/upload.go +++ b/upload.go @@ -8,6 +8,7 @@ import ( "os" "path" "regexp" + "strconv" "strings" "code.google.com/p/go-uuid/uuid" @@ -97,7 +98,23 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { upload.Filename = strings.Join([]string{barename, extension}, ".") - dst, err := os.Create(path.Join("files/", upload.Filename)) + _, err = os.Stat(path.Join(Config.filesDir, upload.Filename)) + + fileexists := err == nil + for fileexists { + counter, err := strconv.Atoi(string(barename[len(barename)-1])) + if err != nil { + barename = barename + "1" + } else { + barename = barename[:len(barename)-1] + strconv.Itoa(counter+1) + } + upload.Filename = strings.Join([]string{barename, extension}, ".") + + _, err = os.Stat(path.Join(Config.filesDir, upload.Filename)) + fileexists = err == nil + } + + dst, err := os.Create(path.Join(Config.filesDir, upload.Filename)) if err != nil { return }