Check if the filename exists

This commit is contained in:
Las Zenow 2012-08-15 12:29:54 +02:00
parent 6a656eca31
commit beb032eddb

View file

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