Check if the filename exists
This commit is contained in:
parent
6a656eca31
commit
beb032eddb
1 changed files with 13 additions and 2 deletions
15
upload.go
15
upload.go
|
@ -2,19 +2,30 @@ package main
|
|||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"labix.org/v2/mgo"
|
||||
//"labix.org/v2/mgo/bson"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func storePath(name string) string {
|
||||
path := "new/" + name
|
||||
_, err := os.Stat(path)
|
||||
for i := 0; err == nil; i++ {
|
||||
path = "new/" + strconv.Itoa(i) + "_" + name
|
||||
_, err = os.Stat(path)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func storeFile(r *http.Request) error {
|
||||
f, header, err := r.FormFile("epub")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
// FIXME: check the name exist
|
||||
fw, err := os.Create("new/" + header.Filename)
|
||||
|
||||
fw, err := os.Create(storePath(header.Filename))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
Reference in a new issue