This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/upload.go
Las Zenow 85111946ed Refactoring code
Take all the storing stuff out of upload file
2012-10-28 17:04:38 +01:00

61 lines
1.4 KiB
Go

package main
import (
"log"
"net/http"
"os"
)
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 {
log.Println("File uploaded:", f.Filename)
file, err := f.Open()
if err != nil {
return paths, err
}
defer file.Close()
path, err := StoreFile(f.Filename, file)
if err != nil {
return paths, err
}
paths = append(paths, path)
}
return paths, nil
}
type uploadData struct {
S Status
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
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(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")
}
}
var data uploadData
data.S = GetStatus(w, r)
data.S.Upload = true
loadTemplate(w, "upload", data)
}