parent
26894f1438
commit
cc12981a50
12 changed files with 216 additions and 13 deletions
|
@ -9,6 +9,7 @@ import (
|
|||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/meskio/epubgo"
|
||||
"gitlab.com/trantor/trantor/lib/database"
|
||||
"gitlab.com/trantor/trantor/lib/parser"
|
||||
|
@ -29,6 +30,7 @@ var uploadChannel chan uploadRequest
|
|||
type uploadRequest struct {
|
||||
file multipart.File
|
||||
filename string
|
||||
id int
|
||||
}
|
||||
|
||||
func uploadWorker(database database.DB, store storage.Store) {
|
||||
|
@ -43,6 +45,7 @@ func processFile(req uploadRequest, db database.DB, store storage.Store) {
|
|||
epub, err := openMultipartEpub(req.file)
|
||||
if err != nil {
|
||||
log.Warn("Not valid epub uploaded file ", req.filename, ": ", err)
|
||||
db.UpdateSubmission(req.id, "It is not a valid epub file", nil)
|
||||
return
|
||||
}
|
||||
defer epub.Close()
|
||||
|
@ -54,6 +57,7 @@ func processFile(req uploadRequest, db database.DB, store storage.Store) {
|
|||
size, err := store.Store(book.ID, req.file, epubFile)
|
||||
if err != nil {
|
||||
log.Error("Error storing book (", book.ID, "): ", err)
|
||||
db.UpdateSubmission(req.id, "There was a problem in our server", nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -63,9 +67,11 @@ func processFile(req uploadRequest, db database.DB, store storage.Store) {
|
|||
err = db.AddBook(book)
|
||||
if err != nil {
|
||||
log.Error("Error storing metadata (", book.ID, "): ", err)
|
||||
db.UpdateSubmission(req.id, "There was a problem in our server", nil)
|
||||
return
|
||||
}
|
||||
log.Info("File uploaded: ", req.filename)
|
||||
db.UpdateSubmission(req.id, "Waiting for moderation", &book)
|
||||
}
|
||||
|
||||
func uploadPostHandler(h handler) {
|
||||
|
@ -75,33 +81,41 @@ func uploadPostHandler(h handler) {
|
|||
return
|
||||
}
|
||||
|
||||
problem := false
|
||||
|
||||
h.r.ParseMultipartForm(20000000)
|
||||
filesForm := h.r.MultipartForm.File["epub"]
|
||||
submissionID := genID()
|
||||
for _, f := range filesForm {
|
||||
submission := database.Submission{
|
||||
SubmissionID: submissionID,
|
||||
Filename: f.Filename,
|
||||
Status: "Waiting to be processed",
|
||||
}
|
||||
|
||||
file, err := f.Open()
|
||||
if err != nil {
|
||||
log.Error("Can not open uploaded file ", f.Filename, ": ", err)
|
||||
h.sess.Notify("Upload problem!", "There was a problem with book "+f.Filename, "error")
|
||||
problem = true
|
||||
submission.Status = "It was not possible to read the book"
|
||||
h.db.AddSubmission(submission)
|
||||
continue
|
||||
}
|
||||
uploadChannel <- uploadRequest{file, f.Filename}
|
||||
}
|
||||
|
||||
if !problem {
|
||||
if len(filesForm) > 0 {
|
||||
h.sess.Notify("Upload successful!", "Thank you for your contribution", "success")
|
||||
} else {
|
||||
h.sess.Notify("Upload problem!", "No books where uploaded.", "error")
|
||||
id, err := h.db.AddSubmission(submission)
|
||||
if err != nil {
|
||||
log.Error("Can add submission to db for ", f.Filename, ": ", err)
|
||||
}
|
||||
uploadChannel <- uploadRequest{file, f.Filename, id}
|
||||
}
|
||||
uploadHandler(h)
|
||||
_uploadHandler(h, submissionID)
|
||||
}
|
||||
|
||||
func uploadHandler(h handler) {
|
||||
_uploadHandler(h, "")
|
||||
}
|
||||
|
||||
func _uploadHandler(h handler, submissionID string) {
|
||||
var data uploadData
|
||||
data.SubmissionID = submissionID
|
||||
data.S = GetStatus(h)
|
||||
data.S.Title = "Upload -- " + data.S.Title
|
||||
data.S.Upload = true
|
||||
|
@ -109,7 +123,27 @@ func uploadHandler(h handler) {
|
|||
}
|
||||
|
||||
type uploadData struct {
|
||||
S Status
|
||||
S Status
|
||||
SubmissionID string
|
||||
}
|
||||
|
||||
func submissionHandler(h handler) {
|
||||
var data submissionData
|
||||
var err error
|
||||
|
||||
submissionID := mux.Vars(h.r)["id"]
|
||||
data.Submissions, err = h.db.GetSubmission(submissionID)
|
||||
if err != nil {
|
||||
log.Error("Can get submission ", submissionID, ": ", err)
|
||||
}
|
||||
data.S = GetStatus(h)
|
||||
data.S.Title = "Submission -- " + data.S.Title
|
||||
h.load("submission", data)
|
||||
}
|
||||
|
||||
type submissionData struct {
|
||||
S Status
|
||||
Submissions []database.Submission
|
||||
}
|
||||
|
||||
func openMultipartEpub(file multipart.File) (*epubgo.Epub, error) {
|
||||
|
|
Reference in a new issue