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

143 lines
3.1 KiB
Go
Raw Normal View History

2012-08-15 11:40:19 +02:00
package main
import (
2013-04-16 02:33:40 +02:00
"bytes"
"git.gitorious.org/go-pkg/epubgo.git"
"io/ioutil"
2012-10-26 13:44:08 +02:00
"log"
2013-04-16 02:33:40 +02:00
"mime/multipart"
2012-08-15 11:40:19 +02:00
"net/http"
2013-04-16 02:33:40 +02:00
"strings"
2012-08-20 14:25:18 +02:00
)
2013-05-03 00:43:26 +02:00
func InitUpload() {
uploadChannel = make(chan uploadRequest, CHAN_SIZE)
go uploadWorker()
}
2013-04-16 02:33:40 +02:00
2013-05-03 00:43:26 +02:00
var uploadChannel chan uploadRequest
2013-04-16 02:34:15 +02:00
2013-05-03 00:43:26 +02:00
type uploadRequest struct {
2013-07-16 22:56:48 +02:00
file multipart.File
filename string
2013-05-03 00:43:26 +02:00
}
2013-04-16 02:34:15 +02:00
2013-05-03 00:43:26 +02:00
func uploadWorker() {
for req := range uploadChannel {
2013-07-16 22:56:48 +02:00
processFile(req)
}
}
2013-05-03 00:43:26 +02:00
2013-07-16 22:56:48 +02:00
func processFile(req uploadRequest) {
defer req.file.Close()
2013-05-03 00:43:26 +02:00
2013-07-16 22:56:48 +02:00
epub, err := openMultipartEpub(req.file)
if err != nil {
log.Println("Not valid epub uploaded file", req.filename, ":", err)
2013-07-16 22:56:48 +02:00
return
}
defer epub.Close()
2013-05-03 00:43:26 +02:00
2013-07-16 22:56:48 +02:00
book := parseFile(epub)
title, _ := book["title"].(string)
req.file.Seek(0, 0)
id, err := StoreNewFile(title+".epub", req.file)
if err != nil {
log.Println("Error storing book (", title, "):", err)
return
2012-08-15 13:58:16 +02:00
}
2013-07-16 22:56:48 +02:00
book["file"] = id
2013-07-23 22:40:35 +02:00
err = db.InsertBook(book)
if err != nil {
log.Println("Error storing metadata (", title, "):", err)
return
}
2013-07-16 22:56:48 +02:00
log.Println("File uploaded:", req.filename)
2013-05-03 00:43:26 +02:00
}
2013-05-03 00:43:26 +02:00
func uploadPostHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
2013-07-16 22:56:48 +02:00
problem := false
2013-05-03 00:43:26 +02:00
r.ParseMultipartForm(20000000)
filesForm := r.MultipartForm.File["epub"]
2013-07-16 22:56:48 +02:00
for _, f := range filesForm {
file, err := f.Open()
if err != nil {
log.Println("Can not open uploaded file", f.Filename, ":", err)
sess.Notify("Upload problem!", "There was a problem with book "+f.Filename, "error")
problem = true
continue
}
uploadChannel <- uploadRequest{file, f.Filename}
}
2013-05-03 00:43:26 +02:00
2013-07-16 22:56:48 +02:00
if !problem {
if len(filesForm) > 0 {
sess.Notify("Upload successful!", "Thank you for your contribution", "success")
} else {
sess.Notify("Upload problem!", "No books where uploaded.", "error")
}
2013-05-03 00:43:26 +02:00
}
2013-04-22 23:28:00 +02:00
uploadHandler(w, r, sess)
2013-04-16 02:34:15 +02:00
}
2013-04-22 23:28:00 +02:00
func uploadHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
var data uploadData
data.S = GetStatus(w, r)
data.S.Upload = true
loadTemplate(w, "upload", data)
2012-08-15 11:40:19 +02:00
}
2013-04-16 02:33:40 +02:00
type uploadData struct {
S Status
}
func openMultipartEpub(file multipart.File) (*epubgo.Epub, error) {
buff, _ := ioutil.ReadAll(file)
reader := bytes.NewReader(buff)
return epubgo.Load(reader, int64(len(buff)))
}
func parseFile(epub *epubgo.Epub) map[string]interface{} {
book := map[string]interface{}{}
for _, m := range epub.MetadataFields() {
data, err := epub.Metadata(m)
if err != nil {
continue
}
switch m {
case "creator":
book["author"] = parseAuthr(data)
case "description":
book[m] = parseDescription(data)
case "subject":
book[m] = parseSubject(data)
case "date":
book[m] = parseDate(data)
case "language":
book["lang"] = data
case "title", "contributor", "publisher":
book[m] = cleanStr(strings.Join(data, ", "))
case "identifier":
attr, _ := epub.MetadataAttr(m)
for i, d := range data {
if attr[i]["scheme"] == "ISBN" {
book["isbn"] = d
}
}
default:
book[m] = strings.Join(data, ", ")
}
}
title, _ := book["title"].(string)
book["file"] = nil
cover, coverSmall := GetCover(epub, title)
2013-07-23 22:41:04 +02:00
if cover != "" {
book["cover"] = cover
book["coversmall"] = coverSmall
}
2013-04-16 02:33:40 +02:00
book["keywords"] = keywords(book)
return book
}