Browse Source

Check if file exists and increment filename

pull/4/head
andreimarcu 9 years ago
parent
commit
9b0385bf6f
  1. 4
      server.go
  2. 19
      upload.go

4
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

19
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
}

Loading…
Cancel
Save