Check if file exists and increment filename

This commit is contained in:
andreimarcu 2015-09-25 12:47:55 -04:00
parent d98b63e8bd
commit 9b0385bf6f
2 changed files with 21 additions and 2 deletions

View File

@ -24,7 +24,7 @@ func main() {
flag.StringVar(&Config.bind, "b", "127.0.0.1:8080", flag.StringVar(&Config.bind, "b", "127.0.0.1:8080",
"host to bind to (default: 127.0.0.1:8080)") "host to bind to (default: 127.0.0.1:8080)")
flag.StringVar(&Config.filesDir, "filespath", "files/", 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, flag.BoolVar(&Config.noLogs, "nologs", false,
"remove stdout output for each request") "remove stdout output for each request")
flag.StringVar(&Config.siteName, "sitename", "linx", flag.StringVar(&Config.siteName, "sitename", "linx",
@ -37,6 +37,8 @@ func main() {
goji.Abandon(middleware.Logger) goji.Abandon(middleware.Logger)
} }
// check trailing slashes
// Template Globals // Template Globals
pongo2.DefaultSet.Globals["sitename"] = Config.siteName pongo2.DefaultSet.Globals["sitename"] = Config.siteName

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"path" "path"
"regexp" "regexp"
"strconv"
"strings" "strings"
"code.google.com/p/go-uuid/uuid" "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}, ".") 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 { if err != nil {
return return
} }