diff --git a/trantor.go b/trantor.go index c5f97e9..21d852d 100644 --- a/trantor.go +++ b/trantor.go @@ -15,7 +15,7 @@ const ( func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { // TODO: when finish devel conver to global: - var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html")) + var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html", "upload.html")) // TODO: use includes err := templates.ExecuteTemplate(w, "head.html", nil) diff --git a/upload.go b/upload.go new file mode 100644 index 0000000..ff4f5f9 --- /dev/null +++ b/upload.go @@ -0,0 +1,38 @@ +package main + +import ( + "os" + "labix.org/v2/mgo" + //"labix.org/v2/mgo/bson" + "net/http" +) + +func storeFile(r *http.Request) { + f, header, err := r.FormFile("epub") + if err != nil { + panic(err) // FIXME + } + defer f.Close() + // FIXME: check the name exist + fw, err := os.Create("new/" + header.Filename) + if err != nil { + panic(err) // FIXME + } + defer fw.Close() + + const size = 1024 + var n int = size + buff := make([]byte, size) + for n == size { + n, err = f.Read(buff) + fw.Write(buff) + } +} + +func uploadHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + storeFile(r) + } + + loadTemplate(w, "upload", nil) +} diff --git a/upload.html b/upload.html new file mode 100644 index 0000000..04edb61 --- /dev/null +++ b/upload.html @@ -0,0 +1,4 @@ +