Upload multiple files

This commit is contained in:
Las Zenow 2012-08-22 10:33:57 +02:00
parent 1ed4de177f
commit f35947c644
3 changed files with 46 additions and 35 deletions

View file

@ -1,8 +1,8 @@
package main package main
import ( import (
"html/template"
"git.gitorious.org/go-pkg/epub.git" "git.gitorious.org/go-pkg/epub.git"
"html/template"
"labix.org/v2/mgo" "labix.org/v2/mgo"
"labix.org/v2/mgo/bson" "labix.org/v2/mgo/bson"
"net/http" "net/http"
@ -14,8 +14,8 @@ type readData struct {
S Status S Status
Book Book Book Book
Txt template.HTML Txt template.HTML
Next string Next string
Prev string Prev string
Back string Back string
} }
@ -96,7 +96,7 @@ func readHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
if file == "" { if file == "" {
it := e.Iterator(epub.EITERATOR_LINEAR) it := e.Iterator(epub.EITERATOR_LINEAR)
defer it.Close() defer it.Close()
http.Redirect(w, r, base + id + "/" + it.CurrUrl(), 307) http.Redirect(w, r, base+id+"/"+it.CurrUrl(), 307)
return return
} }

View file

@ -3,7 +3,7 @@
<p>Upload your epubs to help the library.</p> <p>Upload your epubs to help the library.</p>
<form class="well form-inline" method="POST" enctype="multipart/form-data"> <form class="well form-inline" method="POST" enctype="multipart/form-data">
<input accept="application/epub+zip" type="file" name="epub" /> <input accept="application/epub+zip" type="file" name="epub" multiple="multiple" />
<button type="submit" class="btn">Submit</button> <button type="submit" class="btn">Submit</button>
</form> </form>

View file

@ -27,29 +27,34 @@ func storePath(name string) string {
return path return path
} }
func storeFile(r *http.Request) (string, error) { func storeFiles(r *http.Request) ([]string, error) {
f, header, err := r.FormFile("epub") r.ParseMultipartForm(20000000)
if err != nil { filesForm := r.MultipartForm.File["epub"]
return "", err paths := make([]string, 0, len(filesForm))
} for _, f := range filesForm {
defer f.Close() file, err := f.Open()
if err != nil {
return paths, err
}
defer file.Close()
path := storePath(header.Filename) path := storePath(f.Filename)
fw, err := os.Create(path) fw, err := os.Create(path)
if err != nil { if err != nil {
return "", err return paths, err
} }
defer fw.Close() defer fw.Close()
const size = 1024 const size = 1024
var n int = size var n int = size
buff := make([]byte, size) buff := make([]byte, size)
for n == size { for n == size {
n, err = f.Read(buff) n, err = file.Read(buff)
fw.Write(buff) fw.Write(buff)
}
paths = append(paths, path)
} }
return paths, nil
return path, nil
} }
func cleanStr(str string) string { func cleanStr(str string) string {
@ -243,18 +248,24 @@ type uploadData struct {
func uploadHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { func uploadHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
path, err := storeFile(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
title, err := parseFile(coll, path)
sess := GetSession(r) sess := GetSession(r)
paths, err := storeFiles(r)
if err != nil { if err != nil {
os.Remove(path) sess.Notify("Problem uploading!", "Some files were not stored. Try again or contact us if it keeps happening", "error")
sess.Notify("Problem uploading!", "The file is not a well formed epub", "error") }
} else {
sess.Notify("Upload successful!", "Added '"+title+"'. Thank you for your contribution", "success") uploaded := ""
for _, path := range paths {
title, err := parseFile(coll, path)
if err != nil {
os.Remove(path)
sess.Notify("Problem uploading!", "The file '"+path[len("new/"):]+"' is not a well formed epub", "error")
} else {
uploaded = uploaded + " '" + title + "'"
}
}
if uploaded != "" {
sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success")
} }
} }