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

62 lines
1.4 KiB
Go
Raw Normal View History

2012-08-15 11:40:19 +02:00
package main
import (
2012-10-26 13:44:08 +02:00
"log"
2012-08-15 11:40:19 +02:00
"net/http"
2012-08-20 14:25:18 +02:00
"os"
)
2012-08-22 10:33:57 +02:00
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 {
2012-10-24 22:36:20 +02:00
log.Println("File uploaded:", f.Filename)
2012-08-22 10:33:57 +02:00
file, err := f.Open()
if err != nil {
return paths, err
}
defer file.Close()
2012-08-15 12:29:54 +02:00
2012-10-28 17:22:23 +01:00
path, err := StoreNewFile(f.Filename, file)
2012-08-22 10:33:57 +02:00
if err != nil {
return paths, err
}
paths = append(paths, path)
2012-08-15 11:40:19 +02:00
}
2012-08-22 10:33:57 +02:00
return paths, nil
2012-08-20 14:25:18 +02:00
}
2012-08-18 02:06:43 +02:00
type uploadData struct {
2012-08-20 14:25:18 +02:00
S Status
2012-08-18 02:06:43 +02:00
}
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")
}
2012-08-22 10:33:57 +02:00
uploaded := ""
for _, path := range paths {
title, err := ParseFile(path)
if err != nil {
os.Remove(NEW_PATH + path)
sess.Notify("Problem uploading!", "The file '"+path[len("new/"):]+"' is not a well formed epub", "error")
} else {
uploaded = uploaded + " '" + title + "'"
2012-08-20 14:25:18 +02:00
}
}
if uploaded != "" {
sess.Notify("Upload successful!", "Added the books:"+uploaded+". Thank you for your contribution", "success")
}
2012-08-15 13:58:16 +02:00
}
var data uploadData
data.S = GetStatus(w, r)
data.S.Upload = true
loadTemplate(w, "upload", data)
2012-08-15 11:40:19 +02:00
}