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/lib/upload.go

123 lines
2.6 KiB
Go
Raw Normal View History

2016-05-02 21:36:49 -04:00
package trantor
2012-08-15 11:40:19 +02:00
import (
2014-08-30 13:17:50 -05:00
log "github.com/cihub/seelog"
2013-04-16 02:33:40 +02:00
"bytes"
2014-08-21 19:24:23 -05:00
"crypto/rand"
"encoding/base64"
2013-04-16 02:33:40 +02:00
"io/ioutil"
"mime/multipart"
2015-04-21 21:32:01 -04:00
"github.com/meskio/epubgo"
2016-05-02 21:36:49 -04:00
"gitlab.com/trantor/trantor/lib/database"
"gitlab.com/trantor/trantor/lib/parser"
"gitlab.com/trantor/trantor/lib/storage"
2012-08-20 14:25:18 +02:00
)
const (
uploadChanSize = 100
)
2014-08-21 19:24:23 -05:00
func InitUpload(database *database.DB, store *storage.Store) {
uploadChannel = make(chan uploadRequest, uploadChanSize)
2014-08-21 19:24:23 -05:00
go uploadWorker(database, store)
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
2014-08-21 19:24:23 -05:00
func uploadWorker(database *database.DB, store *storage.Store) {
db := database.Copy()
defer db.Close()
2013-05-03 00:43:26 +02:00
for req := range uploadChannel {
2014-08-21 19:24:23 -05:00
processFile(req, db, store)
2013-07-16 22:56:48 +02:00
}
}
2013-05-03 00:43:26 +02:00
2014-08-21 19:24:23 -05:00
func processFile(req uploadRequest, db *database.DB, store *storage.Store) {
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 {
2014-02-11 13:13:43 +01:00
log.Warn("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
id := genId()
metadata := parser.EpubMetadata(epub)
metadata["id"] = id
metadata["cover"] = GetCover(epub, id, store)
2013-07-16 22:56:48 +02:00
req.file.Seek(0, 0)
size, err := store.Store(id, req.file, epubFile)
2013-07-16 22:56:48 +02:00
if err != nil {
2014-08-21 19:24:23 -05:00
log.Error("Error storing book (", id, "): ", err)
2013-07-16 22:56:48 +02:00
return
2012-08-15 13:58:16 +02:00
}
2013-07-16 22:56:48 +02:00
metadata["filesize"] = size
err = db.AddBook(metadata)
2013-07-23 22:40:35 +02:00
if err != nil {
2014-08-21 19:24:23 -05:00
log.Error("Error storing metadata (", id, "): ", err)
2013-07-23 22:40:35 +02:00
return
}
2014-02-11 13:13:43 +01:00
log.Info("File uploaded: ", req.filename)
2013-05-03 00:43:26 +02:00
}
func uploadPostHandler(h handler) {
2013-07-16 22:56:48 +02:00
problem := false
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 {
2014-02-11 13:13:43 +01:00
log.Error("Can not open uploaded file ", f.Filename, ": ", err)
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 {
h.sess.Notify("Upload successful!", "Thank you for your contribution", "success")
2013-07-16 22:56:48 +02:00
} else {
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
}
uploadHandler(h)
2013-04-16 02:34:15 +02:00
}
func uploadHandler(h handler) {
var data uploadData
data.S = GetStatus(h)
data.S.Title = "Upload -- " + data.S.Title
data.S.Upload = true
h.template.load(h, "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)))
}
2014-08-21 19:24:23 -05:00
func genId() string {
b := make([]byte, 12)
rand.Read(b)
return base64.URLEncoding.EncodeToString(b)
}