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 (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
//"labix.org/v2/mgo/bson"
|
//"labix.org/v2/mgo/bson"
|
||||||
"net/http"
|
"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 {
|
func storeFile(r *http.Request) error {
|
||||||
f, header, err := r.FormFile("epub")
|
f, header, err := r.FormFile("epub")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue