From 137357cd12acd42d0705fed42e02f05533e99051 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Fri, 3 May 2013 00:43:26 +0200 Subject: [PATCH] Upload files on a separate goroutine --- config.go | 2 +- stats.go | 2 +- trantor.go | 1 + upload.go | 81 +++++++++++++++++++++++++++++++++--------------------- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/config.go b/config.go index 8ed9110..c144607 100644 --- a/config.go +++ b/config.go @@ -28,5 +28,5 @@ const ( IMG_WIDTH_SMALL = 60 IMG_QUALITY = 80 - STATS_CHAN_SIZE = 100 + CHAN_SIZE = 100 ) diff --git a/stats.go b/stats.go index c12a192..4751f46 100644 --- a/stats.go +++ b/stats.go @@ -9,7 +9,7 @@ import ( ) func InitStats() { - statsChannel = make(chan statsRequest, STATS_CHAN_SIZE) + statsChannel = make(chan statsRequest, CHAN_SIZE) go statsWorker() } diff --git a/trantor.go b/trantor.go index 7371ea0..7e917cf 100644 --- a/trantor.go +++ b/trantor.go @@ -120,6 +120,7 @@ func main() { defer db.Close() InitStats() + InitUpload() setUpRouter() panic(http.ListenAndServe(":"+PORT, nil)) diff --git a/upload.go b/upload.go index 7e7fc88..2b90d76 100644 --- a/upload.go +++ b/upload.go @@ -10,43 +10,60 @@ import ( "strings" ) +func InitUpload() { + uploadChannel = make(chan uploadRequest, CHAN_SIZE) + go uploadWorker() +} + +var uploadChannel chan uploadRequest + +type uploadRequest struct { + epubs []*multipart.FileHeader +} + +func uploadWorker() { + for req := range uploadChannel { + for _, f := range req.epubs { + file, err := f.Open() + if err != nil { + log.Println("Can not open uploaded file", f.Filename, ":", err) + continue + } + defer file.Close() + + epub, err := openMultipartEpub(file) + if err != nil { + log.Println("Not valid epub uploaded file", f.Filename, ":", err) + continue + } + defer epub.Close() + + book := parseFile(epub) + title, _ := book["title"].(string) + file.Seek(0, 0) + id, err := StoreNewFile(title+".epub", file) + if err != nil { + log.Println("Error storing book (", title, "):", err) + continue + } + + book["file"] = id + db.InsertBook(book) + log.Println("File uploaded:", f.Filename) + } + } +} + func uploadPostHandler(w http.ResponseWriter, r *http.Request, sess *Session) { - 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() + uploadChannel <- uploadRequest{filesForm} - 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) - file.Seek(0, 0) - id, err := StoreNewFile(title+".epub", file) - if err != nil { - log.Println("Error storing book (", title, "):", err) - continue - } - - book["file"] = id - db.InsertBook(book) - uploaded += " '" + title + "'" + if len(filesForm) > 0 { + sess.Notify("Upload successful!", "Thank you for your contribution", "success") + } else { + sess.Notify("Upload problem!", "No books where uploaded.", "error") } - if uploaded != "" { - sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success") - } - uploadHandler(w, r, sess) }