Store files on GridFS
This commit is contained in:
parent
64375b6de5
commit
05b641201c
8 changed files with 94 additions and 75 deletions
26
upload.go
26
upload.go
|
@ -1,30 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"labix.org/v2/mgo/bson"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func storeFiles(r *http.Request) ([]string, error) {
|
||||
func storeFiles(r *http.Request) ([]bson.ObjectId, error) {
|
||||
r.ParseMultipartForm(20000000)
|
||||
filesForm := r.MultipartForm.File["epub"]
|
||||
paths := make([]string, 0, len(filesForm))
|
||||
ids := make([]bson.ObjectId, 0, len(filesForm))
|
||||
for _, f := range filesForm {
|
||||
log.Println("File uploaded:", f.Filename)
|
||||
file, err := f.Open()
|
||||
if err != nil {
|
||||
return paths, err
|
||||
return ids, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
path, err := StoreNewFile(f.Filename, file)
|
||||
id, err := StoreNewFile(f.Filename, file)
|
||||
if err != nil {
|
||||
return paths, err
|
||||
return ids, err
|
||||
}
|
||||
paths = append(paths, path)
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return paths, nil
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
type uploadData struct {
|
||||
|
@ -34,17 +34,17 @@ type uploadData struct {
|
|||
func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
sess := GetSession(r)
|
||||
paths, err := storeFiles(r)
|
||||
ids, 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(path)
|
||||
for _, id := range ids {
|
||||
title, err := ParseFile(id)
|
||||
if err != nil {
|
||||
os.Remove(NEW_PATH + path)
|
||||
sess.Notify("Problem uploading!", "The file '"+path+"' is not a well formed epub: "+err.Error(), "error")
|
||||
DeleteFile(id)
|
||||
sess.Notify("Problem uploading!", "The file is not a well formed epub: "+err.Error(), "error")
|
||||
} else {
|
||||
uploaded = uploaded + " '" + title + "'"
|
||||
}
|
||||
|
|
Reference in a new issue