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"

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,17 +27,21 @@ 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)
filesForm := r.MultipartForm.File["epub"]
paths := make([]string, 0, len(filesForm))
for _, f := range filesForm {
file, err := f.Open()
if err != nil { if err != nil {
return "", err return paths, err
} }
defer f.Close() 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()
@ -45,11 +49,12 @@ func storeFile(r *http.Request) (string, error) {
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 path, nil }
return paths, 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 {
sess.Notify("Problem uploading!", "Some files were not stored. Try again or contact us if it keeps happening", "error")
}
uploaded := ""
for _, path := range paths {
title, err := parseFile(coll, path)
if err != nil { if err != nil {
os.Remove(path) os.Remove(path)
sess.Notify("Problem uploading!", "The file is not a well formed epub", "error") sess.Notify("Problem uploading!", "The file '"+path[len("new/"):]+"' is not a well formed epub", "error")
} else { } else {
sess.Notify("Upload successful!", "Added '"+title+"'. Thank you for your contribution", "success") uploaded = uploaded + " '" + title + "'"
}
}
if uploaded != "" {
sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success")
} }
} }