Split post and get handlers for upload
This commit is contained in:
parent
fc50ba5fd6
commit
d8bfcb2ef2
2 changed files with 39 additions and 36 deletions
|
@ -126,7 +126,8 @@ func main() {
|
||||||
r.HandleFunc("/", indexHandler)
|
r.HandleFunc("/", indexHandler)
|
||||||
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", bookHandler)
|
r.HandleFunc("/book/{id:[0-9a-fA-F]+}", bookHandler)
|
||||||
r.HandleFunc("/search/", searchHandler)
|
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("/login/", loginHandler).Methods("POST")
|
||||||
r.HandleFunc("/logout/", logoutHandler)
|
r.HandleFunc("/logout/", logoutHandler)
|
||||||
r.HandleFunc("/new/", newHandler)
|
r.HandleFunc("/new/", newHandler)
|
||||||
|
|
72
upload.go
72
upload.go
|
@ -10,46 +10,48 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
func uploadPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
sess := GetSession(r)
|
||||||
sess := GetSession(r)
|
|
||||||
|
|
||||||
uploaded := ""
|
uploaded := ""
|
||||||
r.ParseMultipartForm(20000000)
|
r.ParseMultipartForm(20000000)
|
||||||
filesForm := r.MultipartForm.File["epub"]
|
filesForm := r.MultipartForm.File["epub"]
|
||||||
for _, f := range filesForm {
|
for _, f := range filesForm {
|
||||||
log.Println("File uploaded:", f.Filename)
|
log.Println("File uploaded:", f.Filename)
|
||||||
file, err := f.Open()
|
file, err := f.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sess.Notify("Problem uploading!", "The file '" + f.Filename + "' is not a well formed epub: "+err.Error(), "error")
|
sess.Notify("Problem uploading!", "The file '"+f.Filename+"' is not a well formed epub: "+err.Error(), "error")
|
||||||
continue
|
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 + "'"
|
|
||||||
}
|
}
|
||||||
if uploaded != "" {
|
defer file.Close()
|
||||||
sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success")
|
|
||||||
|
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
|
var data uploadData
|
||||||
data.S = GetStatus(w, r)
|
data.S = GetStatus(w, r)
|
||||||
data.S.Upload = true
|
data.S.Upload = true
|
||||||
|
|
Reference in a new issue