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

146 lines
3.8 KiB
Go
Raw Permalink 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"
2014-08-21 19:24:23 -05:00
"crypto/rand"
"crypto/sha256"
2014-08-21 19:24:23 -05:00
"encoding/base64"
"io"
2013-04-16 02:33:40 +02:00
"mime/multipart"
"net/http"
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
)
2017-05-21 10:16:16 +00: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 {
file multipart.File
header *multipart.FileHeader
id int
2013-05-03 00:43:26 +02:00
}
2013-04-16 02:34:15 +02:00
2017-05-21 10:16:16 +00:00
func uploadWorker(database database.DB, store storage.Store) {
2013-05-03 00:43:26 +02:00
for req := range uploadChannel {
req.processFile(database, store)
2013-07-16 22:56:48 +02:00
}
}
2013-05-03 00:43:26 +02:00
func (req uploadRequest) processFile(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
h := sha256.New()
if _, err := io.Copy(h, req.file); err != nil {
log.Warn("Can't read uploaded file ", req.header.Filename, ": ", err)
db.UpdateSubmission(req.id, "There was a problem with the uploaded book, try again later.", nil)
return
}
fileHash := h.Sum(nil)
if db.ExistsBookHash(fileHash) {
log.Info("Uploaded file ", req.header.Filename, " already in the library.")
db.UpdateSubmission(req.id, "The uploaded book is already in the library (it could be in the moderation queue)", nil)
return
}
epub, err := epubgo.Load(req.file, req.header.Size)
2013-07-16 22:56:48 +02:00
if err != nil {
log.Warn("Not valid epub uploaded file ", req.header.Filename, ": ", err)
db.UpdateSubmission(req.id, "It is not a valid epub file", nil)
return
2013-07-16 22:56:48 +02:00
}
defer epub.Close()
2013-05-03 00:43:26 +02:00
book := parser.EpubMetadata(epub)
2018-07-01 14:35:09 +00:00
book.ID = GenID()
2013-07-16 22:56:48 +02:00
req.file.Seek(0, 0)
2016-07-30 07:59:30 -04:00
size, err := store.Store(book.ID, req.file, epubFile)
2013-07-16 22:56:48 +02:00
if err != nil {
2016-07-30 07:59:30 -04:00
log.Error("Error storing book (", book.ID, "): ", err)
db.UpdateSubmission(req.id, "There was a problem in our server", nil)
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
book.FileSize = int(size)
book.FileHash = fileHash
2016-07-30 07:59:30 -04:00
book.Cover = GetCover(epub, book.ID, store)
err = db.AddBook(book)
2013-07-23 22:40:35 +02:00
if err != nil {
2016-07-30 07:59:30 -04:00
log.Error("Error storing metadata (", book.ID, "): ", err)
db.UpdateSubmission(req.id, "There was a problem in our server", nil)
2013-07-23 22:40:35 +02:00
return
}
log.Info("File uploaded: ", req.header.Filename)
db.UpdateSubmission(req.id, "Waiting for moderation", &book)
2013-05-03 00:43:26 +02:00
}
func uploadPostHandler(h handler) {
const _2M int64 = (1 << 20) * 2
2017-05-21 10:16:16 +00:00
if h.ro {
h.sess.Notify("Upload failed!", "The library is in Read Only mode, no books can be uploaded", "danger")
2017-05-21 10:16:16 +00:00
uploadHandler(h)
return
}
if err := h.r.ParseMultipartForm(_2M); nil != err {
log.Error("Can't parse form: ", err)
return
}
defer h.r.MultipartForm.RemoveAll()
filesForm := h.r.MultipartForm.File["epub"]
2018-07-01 14:35:09 +00:00
submissionID := GenID()
2013-07-16 22:56:48 +02:00
for _, f := range filesForm {
submission := database.Submission{
SubmissionID: submissionID,
Filename: f.Filename,
Status: "Waiting to be processed, reload the page in few minutes",
}
2013-07-16 22:56:48 +02:00
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, "danger")
submission.Status = "It was not possible to read the book"
h.db.AddSubmission(submission, h.sess.User)
2013-07-16 22:56:48 +02:00
continue
}
2013-05-03 00:43:26 +02:00
id, err := h.db.AddSubmission(submission, h.sess.User)
if err != nil {
log.Error("Can add submission to db for ", f.Filename, ": ", err)
2013-07-16 22:56:48 +02:00
}
uploadChannel <- uploadRequest{file, f, id}
2013-05-03 00:43:26 +02:00
}
http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound)
2013-04-16 02:34:15 +02:00
}
func uploadHandler(h handler) {
var data struct{ S Status }
data.S = GetStatus(h)
data.S.Title = "Upload -- " + data.S.Title
data.S.Upload = true
h.load("upload", data)
2012-08-15 11:40:19 +02:00
}
2013-04-16 02:33:40 +02:00
2018-07-01 14:35:09 +00:00
func GenID() string {
2014-08-21 19:24:23 -05:00
b := make([]byte, 12)
rand.Read(b)
return base64.URLEncoding.EncodeToString(b)
}