diff --git a/upload.go b/upload.go index ff4f5f9..332d10f 100644 --- a/upload.go +++ b/upload.go @@ -7,16 +7,16 @@ import ( "net/http" ) -func storeFile(r *http.Request) { +func storeFile(r *http.Request) error { f, header, err := r.FormFile("epub") if err != nil { - panic(err) // FIXME + return err } defer f.Close() // FIXME: check the name exist fw, err := os.Create("new/" + header.Filename) if err != nil { - panic(err) // FIXME + return err } defer fw.Close() @@ -27,12 +27,20 @@ func storeFile(r *http.Request) { n, err = f.Read(buff) fw.Write(buff) } + + return nil } func uploadHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) { + status := "" if r.Method == "POST" { - storeFile(r) + err := storeFile(r) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + status = "Upload successful." } - loadTemplate(w, "upload", nil) + loadTemplate(w, "upload", status) } diff --git a/upload.html b/upload.html index 469deb5..7344369 100644 --- a/upload.html +++ b/upload.html @@ -1,5 +1,7 @@ {{template "header.html"}} +{{if .}}
{{.}}
{{end}} +