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"
|
|
|
|
"strings"
|
2012-08-20 14:25:18 +02:00
|
|
|
)
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func InitUpload(database *DB) {
|
2013-05-03 00:43:26 +02:00
|
|
|
uploadChannel = make(chan uploadRequest, CHAN_SIZE)
|
2013-09-23 16:27:31 +02:00
|
|
|
go uploadWorker(database)
|
2013-05-03 00:43:26 +02:00
|
|
|
}
|
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-09-23 16:27:31 +02:00
|
|
|
func uploadWorker(database *DB) {
|
|
|
|
db := database.Copy()
|
|
|
|
defer db.Close()
|
|
|
|
|
2013-05-03 00:43:26 +02:00
|
|
|
for req := range uploadChannel {
|
2013-09-23 16:27:31 +02:00
|
|
|
processFile(req, db)
|
2013-07-16 22:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-03 00:43:26 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func processFile(req uploadRequest, db *DB) {
|
2013-07-16 22:56:48 +02:00
|
|
|
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 {
|
2013-07-18 21:53:35 +02:00
|
|
|
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-09-23 16:27:31 +02:00
|
|
|
book := parseFile(epub, db)
|
2013-07-16 22:56:48 +02:00
|
|
|
title, _ := book["title"].(string)
|
|
|
|
req.file.Seek(0, 0)
|
2013-09-23 16:27:31 +02:00
|
|
|
id, size, err := StoreNewFile(title+".epub", req.file, db)
|
2013-07-16 22:56:48 +02:00
|
|
|
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
|
|
|
|
2013-07-18 21:53:35 +02:00
|
|
|
book["file"] = id
|
2013-09-18 23:56:49 +02:00
|
|
|
book["filesize"] = size
|
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
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func uploadPostHandler(h handler) {
|
2013-07-16 22:56:48 +02:00
|
|
|
problem := false
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
h.r.ParseMultipartForm(20000000)
|
|
|
|
filesForm := h.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)
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("Upload problem!", "There was a problem with book "+f.Filename, "error")
|
2013-07-16 22:56:48 +02:00
|
|
|
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 {
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("Upload successful!", "Thank you for your contribution", "success")
|
2013-07-16 22:56:48 +02:00
|
|
|
} else {
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("Upload problem!", "No books where uploaded.", "error")
|
2013-07-16 22:56:48 +02:00
|
|
|
}
|
2013-05-03 00:43:26 +02:00
|
|
|
}
|
2013-09-23 16:27:31 +02:00
|
|
|
uploadHandler(h)
|
2013-04-16 02:34:15 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func uploadHandler(h handler) {
|
2012-09-12 00:19:19 +02:00
|
|
|
var data uploadData
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
data.S.Upload = true
|
2013-09-23 16:27:31 +02:00
|
|
|
loadTemplate(h.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)))
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func parseFile(epub *epubgo.Epub, db *DB) map[string]interface{} {
|
2013-04-16 02:33:40 +02:00
|
|
|
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
|
2013-09-23 16:27:31 +02:00
|
|
|
cover, coverSmall := GetCover(epub, title, db)
|
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
|
|
|
|
}
|