From d8bfcb2ef2fdaf60795d330c6fd38f9556ff2e0a Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 16 Apr 2013 02:34:15 +0200 Subject: [PATCH] Split post and get handlers for upload --- trantor.go | 3 ++- upload.go | 72 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/trantor.go b/trantor.go index 0c97460..2cdd3c1 100644 --- a/trantor.go +++ b/trantor.go @@ -126,7 +126,8 @@ func main() { r.HandleFunc("/", indexHandler) r.HandleFunc("/book/{id:[0-9a-fA-F]+}", bookHandler) r.HandleFunc("/search/", searchHandler) - r.HandleFunc("/upload/", uploadHandler) + r.HandleFunc("/upload/", uploadHandler).Methods("GET") + r.HandleFunc("/upload/", uploadPostHandler).Methods("POST") r.HandleFunc("/login/", loginHandler).Methods("POST") r.HandleFunc("/logout/", logoutHandler) r.HandleFunc("/new/", newHandler) diff --git a/upload.go b/upload.go index 94a5b92..afa2afd 100644 --- a/upload.go +++ b/upload.go @@ -10,46 +10,48 @@ import ( "strings" ) -func uploadHandler(w http.ResponseWriter, r *http.Request) { - if r.Method == "POST" { - sess := GetSession(r) +func uploadPostHandler(w http.ResponseWriter, r *http.Request) { + sess := GetSession(r) - uploaded := "" - r.ParseMultipartForm(20000000) - filesForm := r.MultipartForm.File["epub"] - for _, f := range filesForm { - log.Println("File uploaded:", f.Filename) - file, err := f.Open() - if err != nil { - sess.Notify("Problem uploading!", "The file '" + f.Filename + "' is not a well formed epub: "+err.Error(), "error") - continue - } - defer file.Close() - - epub, err := openMultipartEpub(file) - if err != nil { - sess.Notify("Problem uploading!", "The file '" + f.Filename + "' is not a well formed epub: "+err.Error(), "error") - continue - } - defer epub.Close() - - book := parseFile(epub) - title, _ := book["title"].(string) - id, err := StoreNewFile(title, file) - if err != nil { - log.Println("Error storing book (", title, "):", err) - continue - } - - book["file"] = id - db.InsertBook(book) - uploaded += " '" + title + "'" + uploaded := "" + r.ParseMultipartForm(20000000) + filesForm := r.MultipartForm.File["epub"] + for _, f := range filesForm { + log.Println("File uploaded:", f.Filename) + file, err := f.Open() + if err != nil { + sess.Notify("Problem uploading!", "The file '"+f.Filename+"' is not a well formed epub: "+err.Error(), "error") + continue } - if uploaded != "" { - sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success") + defer file.Close() + + epub, err := openMultipartEpub(file) + if err != nil { + sess.Notify("Problem uploading!", "The file '"+f.Filename+"' is not a well formed epub: "+err.Error(), "error") + continue } + defer epub.Close() + + book := parseFile(epub) + title, _ := book["title"].(string) + id, err := StoreNewFile(title, file) + if err != nil { + log.Println("Error storing book (", title, "):", err) + continue + } + + book["file"] = id + db.InsertBook(book) + uploaded += " '" + title + "'" + } + if uploaded != "" { + sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success") } + uploadHandler(w, r) +} + +func uploadHandler(w http.ResponseWriter, r *http.Request) { var data uploadData data.S = GetStatus(w, r) data.S.Upload = true