From 6a656eca31ebc0bfd311cd5b0e8e63323e9150c7 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 15 Aug 2012 12:11:39 +0200 Subject: [PATCH] Inform the user if the upload was success --- upload.go | 18 +++++++++++++----- upload.html | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) 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}} +