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

View file

@ -3,7 +3,7 @@
<p>Upload your epubs to help the library.</p>
<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>
</form>

View file

@ -27,29 +27,34 @@ func storePath(name string) string {
return path
}
func storeFile(r *http.Request) (string, error) {
f, header, err := r.FormFile("epub")
if err != nil {
return "", err
}
defer f.Close()
func storeFiles(r *http.Request) ([]string, error) {
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 {
return paths, err
}
defer file.Close()
path := storePath(header.Filename)
fw, err := os.Create(path)
if err != nil {
return "", err
}
defer fw.Close()
path := storePath(f.Filename)
fw, err := os.Create(path)
if err != nil {
return paths, err
}
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)
const size = 1024
var n int = size
buff := make([]byte, size)
for n == size {
n, err = file.Read(buff)
fw.Write(buff)
}
paths = append(paths, path)
}
return path, nil
return paths, nil
}
func cleanStr(str string) string {
@ -243,18 +248,24 @@ type uploadData struct {
func uploadHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
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)
paths, err := storeFiles(r)
if err != nil {
os.Remove(path)
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")
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 {
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")
}
}