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 93bd567f8d Change the paths stored on the database
The paths in the database now are relative of the config BOOKS_PATH and COVER_PATH

For updating the database this query should be use:
for (var i = db.books.find(); i.hasNext(); ) {
        var book = i.next();
        db.books.update({_id: book["_id"]}, {$set: {path: book["path"].slice(6), cover: book["cover"].slice(7), coversmall: book["coversmall"].slice(7)}});
}
2012-10-28 20:21:46 +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 := StoreNewFile(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(NEW_PATH + 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)
}